??xml version="1.0" encoding="utf-8" standalone="yes"?>
#include
"
stdio.h
"
#include
"
stdlib.h
"
typedef
struct
{
int
number;
int
dicper;
}
ElemType;
typedef
struct
node
{
ElemType data;
struct
node
*
next;
}
Node,
*
LinkList;
void
Initiatte(LinkList
&
L)
{
if
((L
=
(LinkList)malloc(
sizeof
(Node)))
==
NULL)exit(
1
);
L
->
next
=
L;
}
int
Insert(LinkList
&
L,
int
i,ElemType x)
{
LinkList q,p;
p
=
L
->
next;
int
j
=
1
;
while
(p
!=
L
&&
j
<
i
-
1
)
{
p
=
p
->
next;
j
++
;
}
if
(j
!=
i
-
1
&&
i
!=
1
)
{
printf(
"
插入位置参数错!
"
);
return
0
;
}
q
=
(LinkList)malloc(
sizeof
(Node));
if
(
!
q)exit(
1
);
q
->
data
=
x;
q
->
next
=
p
->
next;
p
->
next
=
q;
return
1
;
}
int
IsNotEmpty(LinkList L)
{
if
(L
->
next
==
L)
return
0
;
else
return
1
;
}
int
Delete(LinkList
&
p)
{
LinkList q;
q
=
p
->
next;
p
->
next
=
p
->
next
->
next;
free(q);
return
1
;
}
void
output(LinkList
&
L,
int
m)
{
LinkList pre,q;
pre
=
L;
q
=
L
->
next;
while
(IsNotEmpty(L)
==
1
)
{
for
(
int
i
=
1
;i
<
m;i
++
)
{
pre;
q
=
q
->
next;
if
(q
==
L)
{
pre
=
q;
q
=
q
->
next;
}
}
printf(
"
%d**
"
,q
->
data.number);
m
=
q
->
data.dicper;
q
=
q
->
next;
if
(q
==
L)q
=
q
->
next;
Delete(pre);
}
}
int
main()
{
int
n;
printf(
"
误入h数的|\n
"
);
scanf(
"
%d
"
,
&
n);
ElemType
*
P;
LinkList L;
Initiatte(L);
int
i,m;
P
=
new
ElemType [n];
for
( i
=
0
;i
<
n;i
++
)
{
P[i].number
=
i
+
1
;
printf(
"
W?d个h输入密码
"
,i
+
1
);
printf(
"
\n
"
);
scanf(
"
%d
"
,
&
P[i].dicper);
}
for
(i
=
1
;i
<=
n;i
++
)
Insert(L,i,P[i
-
1
]);
printf(
"
对m的数D行初始化:
"
);
scanf(
"
%d
"
,
&
m);
printf(
"
l果输出:
"
);
output(L,m);
delete [] P;
printf(
"
\n
"
);
return
0
;
}
12
]]>
问题描述
Q约瑟夫(Joseph)问题的一U描q是Q编号ؓ1Q?span lang="EN-US">2Q?。。。,n?span lang="EN-US">n个h按顺旉方向围坐一圈,每h持有一个密码(正整敎ͼ。一开始Q选一个正整数作ؓ报数上限?span lang="EN-US">mQ从W一个h开始按时针方向自1开始顺序报敎ͼ报到m时停止报数。报?span lang="EN-US">m的h出列Q将他的密码作ؓ新的m|从他在顺旉方向上的下一个h开始重C1报数Q如此下去,直到所有h全部出列为止。试设计一个程序求出出列顺序?br />
q是我们数据l构W一个实验题目。大部分都用C语言写。我用C++写的Q写C半编译通过了,run的时候出了问题。还没有写完Q只把用用到的类写了出来。第一ơ这样写Q不知道那里除了问题?br />
代码Q?br />
2. 由此可见Qthis在成员函数的开始前构造的Q在成员的结束后清除?br />q个生命周期同Q一个函数的参数是一LQ没有Q何区别?br />当调用一个类的成员函数时Q编译器类的指针作为函数的this参数传递进厅R如Q?br />A a;
a.func(10);
此处Q编译器会~译成: A::func(&a, 10);
嗯,看v来和静态函数没差别Q对吗?不过Q区别还是有的。编译器通常会对this指针做一些优化的Q因此,this指针的传递效率比较高Q-如vc通常是通过ecx寄存器来传递this参数?/p>
3. 回答
#1:this指针是什么时候创建的?
this在成员函数的开始执行前构造的Q在成员的执行结束后清除?br />#2:this指针存放在何? ??全局变量,q是其他?
this指针会因~译器不同,而放|的位置不同。可能是栈,也可能是寄存器,甚至全局变量?br />#3:this指针如何传递给cM函数?l定?q是在函数参数的首参数就是this指针.那么this指针又是如何扑ֈcd例后函数?
this是通过函数参数的首参数来传递的。this指针是在调用之前生成的。类实例后的函数Q没有这个说法。类在实例化Ӟ只分配类中的变量I间Qƈ没有为函数分配空间。自从类的函数定义完成后Q它在那儿Q不会跑的?br />#4:this指针如何讉KcM变量??
如果不是c,而是l构的话Q那么,如何通过l构指针来访问结构中的变量呢Q如果你明白q一点的话,那就很好理解q个问题了?br />在C++中,cdl构是只有一个区别的Q类的成员默认是privateQ而结构是public?br />this是类的指针,如果换成l构Q那this是l构的指针了?/p>
#5:我们只有获得一个对象后,才能通过对象使用this指针,如果我们知道一个对象this指针的位|可以直接用吗?
this指针只有在成员函C才有定义。因此,你获得一个对象后Q也不能通过对象使用this指针。所以,我们也无法知道一个对象的this指针的位|(只有在成员函数里才有this指针的位|)。当Ӟ在成员函数里Q你是可以知道this指针的位|的Q可?amp;this获得)Q也可以直接使用的?br />#6:每个cȝ译后,是否创徏一个类中函数表保存函数指针,以便用来调用函数?
普通的cd敎ͼ不论是成员函敎ͼq是静态函敎ͼQ都不会创徏一个函数表来保存函数指针的。只有虚函数才会被放到函数表中?br />但是Q既使是虚函敎ͼ如果~译器能明确知道调用的是哪个函数Q编译器׃会通过函数表中的指针来间接调用Q而是会直接调用该函数?/p>
# 7:q些~译器如何做到的?8:能否模拟实现?
知道原理后,q两个问题就很容易理解了?br />其实Q模拟实现this的调用,在很多场合下Q很多h都做q?br />例如Q系l回调函数。系l回调函数有很多Q如定时Q线E啊什么的?/p>
举一个线E的例子Q?br />class A{
int n;
public:
static void run(void* pThis){
A* this_ = (A*)pThis;
this_->process();
}
void process(){}
};
main(){
A a;
_beginthread( A::run, 0, &a );
}
q里是定义一个静态函数来模拟成员函数?/p>
也有许多C语言写的E序Q模拟了cȝ实现。如freetype库等{?br />其实Q有用过C语言的hQ大多都模拟q。只是当时没有明的概念|了?br />如:
typedef struct student{
int age;
int no;
int scores;
}Student;
void initStudent(Student* pstudent);
void addScore(Student* pstudent, int score);
...
如果你把 pstudentҎthisQ那׃样了?/p>
它相当于Q?br />class Student{
public:
int age; int no; int scores;
void initStudent();
void addScore(int score);
}
const帔R可以有物理存攄I间Q因此是可以取地址?/p>
///this指针是在创徏对象前创?
this指针攑֜栈上,在编译时dl确?
q且当一个对象创建后,q且q行整个E序q行期间只有一个this指针.
做题的意?/span>
By EmilMatthew
我可以有相当的理由,认ؓ自己是一个喜爱学习的人,但是Q我亦不得不承认Q我的这份对学习的热爱是有所偏失的,自然也就无法享受到学习的全然乐趣。尤其在做到一些稍隄问题的时候,镉K是久M下,于是心生疑问Q我q等热情的投入学习,怎ؓ何还无法L克难题,体会一些学U精妙之处?/font>
实际Q这是一个谬误,dNQ除了有 PASSION Q有知识上的U篏Q更为关键的Q需要的是一U思维能力。而这U思维能力Q对于理工科的学习而言Q做题是个必不可的环节。有 PASSION Q喜Ƣ看书是我的现状Q这是好事情Q但q绝不是_有了q些׃定会有好的思维能力Q虽然好?/span> PASSION 可以L发一定的思维力,但好的思维力却是必要有训l的q一环节的,否则Q就会像一层窗LQ一捅就_l不h隑ֺQ有深度问题的考验?/span>
自从q了大学后,实际上我对考试的热情是逐渐下降的,对什么ؓ考试而准备,忙的不可开焦之cȝ事甚Zѝ的,C大学qؓ考试而学习却实没什么意思,但是由厌恶考试而顺带着产生厌恶做题的情l就是个很不好的l果了。其实我的学习热情一直是高涨的,其是大一感受了程序设计的妙之后Q更是曾l一度ؓ之废寝忘食,再加上我对物理及数学那点或发自内心的或出于敬仰的喜爱Q我对去解决一些有实际背景的问题还是充满热情的Q每当能发现自己要能发现些或创造什么小成果的时候,更是令h心潮澎湃。但是,我对解题Q也是做传l的书面上的试题却一直提不v_Q尽多数的题目自己是解得下来的Q但是每当面寚w题时Q我仍旧是力不从心的。而且Q我也发玎ͼ在自q书的时候,很多最关键的证明,推导都相当的吃力。比如自动机的一些理论,较难的证明看几眼开始晕了;再如法分析理论斚w的证明,很多时候也都是望而却步。究其原因,我想Q最关键的一点,是q其中的一个整体思\令h无法把握。而这思\Q恰恰是思维q程的一个体玎ͼ光有知识的积累是q远不够的,解题是思维能力训练的一个至关重的环节。高中是也许׃q度的应试对解题心生厌倦了Q但是到了大学里Q如果你惛_得深入的话对解题仍旧不能有丝毫的松懈?/span>
MQ更多的时候是侧重吸收与理解,对于理工U类的书c而言Q读书是否读的透还与一个h的思维能力有着相当的关联。在仅知道向量及U性代数的基础上看张量的概念,有的人看的不知所云,有的Z知半?/span> ( 我就属于q种 ) Q有的h一看就懂了Q再如一些力学中的分析及演算Q对于学的不扎实的h来说Q可能把各个步骤分解了看都懂是怎么回事Q可以综合到一P很隄Z所以然了。究其原因,在读书能力基本一致的前提下,关键是看你在看到这L一个概念阐q的时候,你在思维上能对它做出怎样的反应。而这U思维上反应的高下Q和M一P也是要有一个积累的q程的,而这U过E的U篏Q在我看来,很大一部分是通过做题来提高的?/span>
思维的过E有归纳Q推理,演绎Q发散等许多方式Q我们对某个问题的解x意识和无意识中都用到了这些思维q程。无论是实际的工E问题,抑或是抽象出来的问题Q如习题Q思维能力L臛_重要的作用。当Ӟ知识的积累在解决问题的过E中亦是举轻重的。用唯物论的观点来看Q二者的关系是相辅相承的。工E的Q实际的问题相对要复杂和冗繁Q但未必都很艰深Q而习题中的抽象问题规模相对小Q对问题的剖析能辑ֈ相当的深度。所以,M一个有理性思考的人都可以看到Q在学校把解决问题的基础打好了,无论是以后做研究q是L入到生实践中去Q不说游刃有余,最LQ在l过一定的适应后,是完全可以掌控的。而通过习题在思维层上的训l,更是可以使你在理工科 ---- q样一个富于理性化分析Q精巧构建,高度技巧化的领域中辑ֈ融会贯通,触类旁通的作用。所以,以前我放弃或轻视做题的行为是何等的可W!
之所以对解题提不起太多兴,其中很大的一部分原因是由于多数情况下我们的解题处于一U被动的局面。也是_被老师“追着赶”,在这样一U情l下解题Q一者难有好的心情,二者目的以完成dZQ虽然能学到一些东西,但终I有些情非得已的意味。想有效而又快乐的解题,个h认ؓ最好的法子是d出击Q自己去N来做Q不必多找,找一两本较好出版C或在某一斚w特别有实力的学校出的书是肯定不会错的。然后,是自己的解题,回顾与体会了。至于解题的多少只能是因异了,q和你个人的实际情况有很大关联,但有一点,是解题一定要投入Q一定要重视自己做的q些题的从哪个角度去考查思维力以及对知识的运用、掌握,否则Q妄想以多做题来辑ֈ思维飞跃的想法是徒劳的。个为,q学习的d性一旦被调动h之后Q你马上会感受到解题也竟然和看书一P有着许多收获的喜悦与惊奇。而从解题中可以直接得到的最为重要的好处在于Q一来你Ҏ门课E本w的体系和知识结构加׃理解和掌握;二来你的思维能力体系又得C再一ơ的加强Q遇到新问题的时候(哪怕和原来的领域不相关Q,你的z跃的思\也必定会l你带来解决问题的更多、更好的启示?/span>
所以,作ؓ一个理工科学生的你Q无论是以后惛_U研Q还是想快投入生实践Q思维能力是极光要的Q而做题,则是其中的一条必׃路。当你真正投入时Q你会发玎ͼ做题原来也可以这般的享受?/span>
SOFTWARE PIONEER PETER NAUR WINS ACM'S TURING AWARD![]() Dane's Creative Genius Revolutionized Computer Language Design |
![]() New York, March 01, 2006 -- The Association for Computing Machinery (ACM) has named Peter Naur the winner of the 2005 A.M. Turing Award. The award is for Naur's pioneering work on defining the Algol 60 programming language. Algol 60 is the model for many later programming languages, including those that are indispensable software engineering tools today. The Turing Award, considered the "Nobel Prize of Computing" was first awarded in 1966, and is named for British mathematician Alan M. Turing. It carries a $100,000 prize, with financial support provided by Intel Corporation. ![]() Dr. Naur was editor in 1960 of the hugely influential "Report on the Algorithmic Language Algol 60." He is recognized for the report's elegance, uniformity and coherence, and credited as an important contributor to the language's power and simplicity. The report made pioneering use of what later became known as Backus-Naur Form (BNF) to define the syntax of programs. BNF is now the standard way to define a computer language. Naur is also cited for his contribution to compiler design and to the art and practice of computer programming. ![]() "Dr. Naur's ALGOL 60 embodied the notion of elegant simplicity for algorithmic expression," said Justin Rattner, Intel senior fellow and Chief Technology Officer. "Over the years, programming languages have become bloated with features and functions that have made them more difficult to learn and less effective. This award should encourage future language designers who are addressing today's biggest programming challenges, such as general-purpose, multi-threaded computation, to achieve that same level of elegance and simplicity that was the hallmark of ALGOL 60." ![]() Contributions Signal Birth of Computing Science ![]() In 2002, former Turing Award winner Edsger Dijkstra characterized the development of Algol 60 as "an absolute miracle" that signaled the birth of what he called "computing science" because it showed the first ways in which automatic computing could and should become a topic of academic concern. The development of Algol 60 was the result of an exceptionally talented group of people, including several who were later named Turing Award winners. ![]() Dr. Naur's contribution to Algol 60, was seminal. John Backus, another former Turing Award winner, acknowledged Naur as the driving intellectual force behind the definition of Algol 60. He commented that Naur's editing of the Algol report and his comprehensive preparation for the January 1960 meeting in which Algol was presented "was the stuff that really made Algol 60 the language that it is, and it wouldn't have even come about, had he not done that." ![]() Before publication of the Algol 60 Report, computer languages were informally defined by their prose manuals and the compiler code itself. The report, with its use of BNF to define the syntax, and carefully chosen prose to define the semantics, was concise, powerful, and unambiguous. ![]() The 17-page Algol 60 Report presented the complete definition of an elegant, transparent language designed for communication among humans as well as with computers. It was deliberately independent of the properties of any particular computer. The new language was a major challenge to compiler writers. Dr. Naur went on to co-author the GIER Algol Compiler (for the transistorized electronic computer developed in Denmark known as GIER), one of the first compilers to deal fully and correctly with the language's powerful procedure mechanism. ![]() "Dr. Naur's contribution was a watershed in the computing field, and transformed the way we define programming languages," said James Gray of Microsoft Research, and Chair of the 2005 Turing Committee. "Many of the programming constructs we take for granted today were introduced in the Algol Report, which introduced a concise block-structured language that improved the way we express algorithms." ![]() Dr. Naur was instrumental in establishing software engineering as a discipline. He made pioneering contributions to methodologies for writing correct programs through his work on assertions that enable programmers to state their assumptions, and on structured programming. "His work, though formal and precise, displays an exceptional understanding of the limits and uses of formalism and precision," said Gray. Through these activities, and his development of an influential computer science curriculum, Dr. Naur contributed fundamental components of today's computing knowledge and skills. ![]() Early Experience in Practical Calculations and Applications ![]() Dr. Naur began his scientific pursuits as an astronomer, where he was involved in computations of the orbits of comets and minor planets. He obtained a magister of science degree (the equivalent of a master's degree) from Copenhagen University in 1949. He later returned there to earn a doctorate in astronomy in 1957. During the 1950-51 academic year, Dr. Naur studied astronomy at King's College in Cambridge, U.K., and came to the U.S. to further his work in the field. This work involved using early computers (starting with EDSAC, the world's first practical stored program electronic computer) for his astronomical calculations. In 1953, he returned to Denmark and served as a scientific assistant at Copenhagen Observatory. ![]() In 1959, he joined the staff of the compiler design group at Regnecentralen, the first Danish computer company. There he organized the Algol Bulletin and was editor of the 13-person international Algol 60 team's report that defined Algol 60. He became a professor at the Copenhagen University Institute of Datalogy in 1969, retiring in 1998. ![]() Dr. Naur was awarded the G. A. Hagemann Gold Medal from the Danish Technical University in 1963, the Jens Rosenkjaer Prize from the Danish Radio in 1966, and the Computer Pioneer Award from the Institute of Electrical and Electronics Engineers in 1986. ACM will present the Turing Award at the annual ACM Awards Banquet on May 20, 2006, at the Westin St. Francis Hotel in San Francisco, CA. |
![]() |
About the A.M. Turing Award![]() The A.M. Turing Award was named for Alan M. Turing, the British mathematician who articulated the mathematical foundation and limits of computing, and who was a key contributor to the Allied cryptanalysis of the German Enigma cipher during World War II. Since its inception, the Turing Award has honored the computer scientists and engineers who created the systems and underlying theoretical foundations that have propelled the information technology industry. For additional information, please see the A. M. Turing Awards site. About ACM ![]() ACM, the Association for Computing Machinery (http://www.acm.org), is an educational and scientific society uniting the world's computing educators, researchers and professionals to inspire dialogue, share resources and address the field's challenges. ACM strengthens the profession's collective voice through strong leadership, promotion of the highest standards, and recognition of technical excellence. ACM supports the professional growth of its members by providing opportunities for life-long learning, career development, and professional networking. |