C语言经典编程八Word格式.docx
《C语言经典编程八Word格式.docx》由会员分享,可在线阅读,更多相关《C语言经典编程八Word格式.docx(19页珍藏版)》请在冰点文库上搜索。
}
\n"
}
}
print(stu)
printf("
\nNo.NameSco1Sco2Sco3\n"
for(i=0;
i{printf("
%-6s%-10s"
stu[i].num,stu[i].name);
for(j=0;
%-8d"
stu[i].score[j]);
printf("
main()
{
input();
print();
============================================================
【程序2】
创建一个链表。
/*creatalist*/
#include"
stdlib.h"
stdio.h"
structlist
{intdata;
structlist*next;
};
typedefstructlistnode;
typedefnode*link;
voidmain()
{linkptr,head;
intnum,i;
ptr=(link)malloc(sizeof(node));
ptr=head;
pleaseinput5numbers==>
i<
=4;
i++)
scanf("
num);
ptr->
data=num;
next=(link)malloc(sizeof(node));
if(i==4)ptr->
next=NULL;
elseptr=ptr->
next;
while(ptr!
=NULL)
{printf("
Thevalueis==>
%d\n"
ptr->
data);
ptr=ptr->
【程序3】
反向输出一个链表。
/*reverseoutputalist*/
structlist*next;
{linkptr,head,tail;
intnum,i;
tail=(link)malloc(sizeof(node));
tail->
ptr=tail;
\npleaseinput5data==>
{
ptr->
head=(link)malloc(sizeof(node));
head->
next=ptr;
ptr=head;
ptr=ptr->
}}
【程序4】
连接两个链表。
linkdelete_node(linkpointer,linktmp)
{if(tmp==NULL)/*deletefirstnode*/
returnpointer->
else
{if(tmp->
next->
next==NULL)/*deletelastnode*/
tmp->
else/*deletetheothernode*/
next=tmp->
returnpointer;
voidselection_sort(linkpointer,intnum)
{linktmp,btmp;
inti,min;
i {
tmp=pointer;
min=tmp->
data;
btmp=NULL;
while(tmp->
next)
{if(min>
tmp->
data)
{min=tmp->
btmp=tmp;
tmp=tmp->
\40:
%d\n"
min);
pointer=delete_node(pointer,btmp);
linkcreate_list(intarray[],intnum)
{linktmp1,tmp2,pointer;
inti;
pointer=(link)malloc(sizeof(node));
pointer->
data=array[0];
tmp1=pointer;
for(i=1;
i{tmp2=(link)malloc(sizeof(node));
tmp2->
data=array[i];
tmp1->
next=tmp2;
tmp1=tmp1->
returnpointer;
linkconcatenate(linkpointer1,linkpointer2)
{linktmp;
tmp=pointer1;
while(tmp->
next=pointer2;
returnpointer1;
voidmain(void)
{intarr1[]={3,12,8,9,11};
linkptr;
ptr=create_list(arr1,5);
selection_sort(ptr,5);
【程序5】
放松一下,算一道简单的题目。
{
inti,n;
5;
{n=0;
if(i!
=1)
n=n+1;
if(i==3)
if(i==4)
=4)
if(n==3)
zhuhaoshideshi:
%c"
64+i);
【程序6】
编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n(利用指针函数)
floatpeven(),podd(),dcall();
floatsum;
intn;
while
(1)
n);
if(n>
1)
break;
if(n%2==0)
Even="
sum=dcall(peven,n);
Odd="
sum=dcall(podd,n);
}
%f"
sum);
floatpeven(intn)
floats;
s=1;
for(i=2;
=n;
i+=2)
s+=1/(float)i;
return(s);
floatpodd(n)
s=0;
floatdcall(fp,n)
float(*fp)();
s=(*fp)(n);
【程序7】
填空练习(指向指针的指针)
{char*s[]={"
man"
"
woman"
girl"
boy"
sister"
char**q;
intk;
for(k=0;
k<
k++)
{ ;
/*这里填写什么语句*/
%s\n"
*q);
【程序8】
找到年龄最大的人,并输出。
请找出程序中有什么问题。
#defineN4
staticstructman
{charname[20];
intage;
}person[N]={"
li"
18,"
wang"
19,"
zhang"
20,"
sun"
22};
{structman*q,*p;
inti,m=0;
p=person;
for(i=0;
i{if(mage)
q=p++;
m=q->
age;
%s,%d"
(*q).name,(*q).age);
【程序9】
字符串排序。
char*str1[20],*str2[20],*str3[20];
charswap();
pleaseinputthreestrings\n"
scanf("
str1);
str2);
str3);
if(strcmp(str1,str2)>
0)swap(str1,str2);
if(strcmp(str1,str3)>
0)swap(str1,str3);
if(strcmp(str2,str3)>
0)swap(str2,str3);
afterbeingsorted\n"
%s\n%s\n%s\n"
str1,str2,str3);
charswap(p1,p2)
char*p1,*p2;
char*p[20];
strcpy(p,p1);
strcpy(p1,p2);
strcpy(p2,p);
【程序10】
海滩上有一堆桃子,五只猴子来分。
第一只猴子把这堆桃子凭据分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。
第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?
{inti,m,j,k,count;
for(i=4;
10000;
i+=4)
{count=0;
m=i;
j=i/4*5+1;
i=j;
if(j%4==0)
count++;
else
i=m;
if(count==4)
{printf("
count);
【程序11】
猴子吃桃问题:
猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个。
以后每天早上都吃了前一天剩下的一半零一个。
到第10天早上想再吃时,见只剩下一个桃子了。
求第一天共摘了多少。
采取逆向思维的方法,从后往前推断。
#include<
stdio.h>
intn=1,d=1;
for(d;
d<
=9;
d++)
n=(n+1)*2;
第一天共摘了%d个桃子"
n);
intday,x1,x2;
day=9;
x2=1;
while(day>
0)
{x1=(x2+1)*2;
/*第一天的桃子数是第2天桃子数加1后的2倍*/
x2=x1;
day--;
thetotalis%d\n"
x1);
【程序12】
两个乒乓球队进行比赛,各出三人。
甲队为a,b,c三人,乙队为x,y,z三人。
已抽签决定比赛名单。
有人向队员打听比赛的名单。
a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。
判断素数的方法:
用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数。
chari,j,k;
/*i是a的对手,j是b的对手,k是c的对手*/
for(i='
x'
;
='
z'
for(j='
=j)
for(k='
{if(i!
=k&
&
j!
=k)
{if(i!
k!
)
printf("
orderisa--%c\tb--%c\tc--%c\n"
i,j,k);
}
【程序13】
打印出如下图案(菱形)
*
***
******
********
先把图形分成两部分来看待,前四行一个规律,后三行一个规律,利用双重for循环,第一层控制行,第二层控制列。
inti,j,k;
=3;
=2-i;
for(k=0;
=2*i;
*"
=2;
=i;
=4-2*i;
【程序14】
有一分数序列:
2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。
请抓住分子与分母的变化规律。
intn,t,number=20;
floata=2,b=1,s=0;
for(n=1;
n<
=number;
n++)
s=s+a/b;
t=a;
a=a+b;
b=t;
/*这部分是程序的关键,请读者猜猜t的作用*/
sumis%9.6f\n"
s);
【程序15】
求1+2!
+3!
+...+20!
的和
此程序只是把累加变成了累乘。
floatn,s=0,t=1;
=20;
t*=n;
s+=t;
1+2!
...+20!
=%e\n"