东软信息学院——C语言代码经典100例

经典c程序100例==71--80

【程序71】
题目:编写input()和output()函数输入,输出5个学生的数据记录。
1.程序分析:
2.程序源代码:
#define N 5
struct student
{ char num[6];
char name[8];
int score[4];
} stu[N];
input(stu)
struct student stu[];
{ int i,j;
for(i=0;i<N;i++)
{ printf("\n please input %d of %d\n",i+1,N);
printf("num: ");
scanf("%s",stu[i].num);
printf("name: ");
scanf("%s",stu[i].name);
for(j=0;j<3;j++)
{ printf("score %d.",j+1);
scanf("%d",&stu[i].score[j]);
}
printf("\n");
}
}
print(stu)
struct student stu[];
{ int i,j;
printf("\nNo. Name Sco1 Sco2 Sco3\n");
for(i=0;i<N;i++)
{ printf("%-6s%-10s",stu[i].num,stu[i].name);
for(j=0;j<3;j++)
printf("%-8d",stu[i].score[j]);
printf("\n");
}
}
main()
{
input();
print();
}
==============================================================
【程序72】
题目:创建一个链表。
1.程序分析:           
2.程序源代码:
/*creat a list*/
#include "stdlib.h"
#include "stdio.h"
struct list
{ int data;
struct list *next;
};
typedef struct list node;
typedef node *link;
void main()
{ link ptr,head;
int num,i;
ptr=(link)malloc(sizeof(node));
ptr=head;
printf("please input 5 numbers==>\n");
for(i=0;i<=4;i++)
{
scanf("%d",&num);
ptr->data=num;
ptr->next=(link)malloc(sizeof(node));
if(i==4) ptr->next=NULL;
else ptr=ptr->next;
}
ptr=head;
while(ptr!=NULL)
{ printf("The value is ==>%d\n",ptr->data);
ptr=ptr->next;
}
}
==============================================================
【程序73】
题目:反向输出一个链表。   
1.程序分析:
2.程序源代码:
/*reverse output a list*/
#include "stdlib.h"
#include "stdio.h"
struct list
{ int data;
struct list *next;
};
typedef struct list node;
typedef node *link;
void main()
{ link ptr,head,tail; 
int num,i;
tail=(link)malloc(sizeof(node));
tail->next=NULL;
ptr=tail;
printf("\nplease input 5 data==>\n");
for(i=0;i<=4;i++)
{
scanf("%d",&num);
ptr->data=num;
head=(link)malloc(sizeof(node));
head->next=ptr;
ptr=head;
}
ptr=ptr->next;
while(ptr!=NULL)
{ printf("The value is ==>%d\n",ptr->data);
ptr=ptr->next;
}}
==============================================================
【程序74】
题目:连接两个链表。
1.程序分析:
2.程序源代码:
#include "stdlib.h"
#include "stdio.h"
struct list
{ int data;
struct list *next;
};
typedef struct list node;
typedef node *link;
link delete_node(link pointer,link tmp)
{if (tmp==NULL) /*delete first node*/
return pointer->next;
else
{ if(tmp->next->next==NULL)/*delete last node*/
tmp->next=NULL;
else /*delete the other node*/
tmp->next=tmp->next->next;
return pointer;
}
}
void selection_sort(link pointer,int num)
{ link tmp,btmp;
int i,min;
for(i=0;i<num;i++)
{
tmp=pointer;
min=tmp->data;
btmp=NULL;
while(tmp->next)
{ if(min>tmp->next->data)
{min=tmp->next->data;
btmp=tmp;
}
tmp=tmp->next;
}
printf("\40: %d\n",min);
pointer=delete_node(pointer,btmp);
}
}
link create_list(int array[],int num)
{ link tmp1,tmp2,pointer;
int i;
pointer=(link)malloc(sizeof(node));
pointer->data=array[0];
tmp1=pointer;
for(i=1;i<num;i++)
{ tmp2=(link)malloc(sizeof(node));
tmp2->next=NULL;
tmp2->data=array[i];
tmp1->next=tmp2;
tmp1=tmp1->next;
}
return pointer;
}
link concatenate(link pointer1,link pointer2)
{ link tmp;
tmp=pointer1;
while(tmp->next)
tmp=tmp->next;
tmp->next=pointer2;
return pointer1;
}
void main(void)
{ int arr1[]={3,12,8,9,11};
link ptr;
ptr=create_list(arr1,5);
selection_sort(ptr,5);
}
==============================================================
【程序75】
题目:放松一下,算一道简单的题目。
1.程序分析:
2.程序源代码:
main()
{
int i,n;
for(i=1;i<5;i++)
{ n=0;
if(i!=1)
n=n+1;
if(i==3)
n=n+1;
if(i==4)
n=n+1;
if(i!=4)
n=n+1;
if(n==3)
printf("zhu hao shi de shi:%c",64+i);
}
}
==============================================================
【程序76】
题目:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数
1/1+1/3+...+1/n(利用指针函数)
1.程序分析:
2.程序源代码:
main()
#include "stdio.h"
main()
{
float peven(),podd(),dcall();
float sum;
int n;
while (1)
{
scanf("%d",&n);
if(n>1)
break;
}
if(n%2==0)
{
printf("Even=");
sum=dcall(peven,n);
}
else
{
printf("Odd=");
sum=dcall(podd,n);
}
printf("%f",sum);
}
float peven(int n)
{
float s;
int i;
s=1;
for(i=2;i<=n;i+=2)
s+=1/(float)i;
return(s);
}
float podd(n)
int n;
{
float s;
int i;
s=0;
for(i=1;i<=n;i+=2)
s+=1/(float)i;
return(s);
}
float dcall(fp,n)
float (*fp)();
int n;
{
float s;
s=(*fp)(n);
return(s);
}
==============================================================
【程序77】
题目:填空练习(指向指针的指针)
1.程序分析:     
2.程序源代码:
main()
{ char *s[]={"man","woman","girl","boy","sister"};
char **q;
int k;
for(k=0;k<5;k++)
{       ;/*这里填写什么语句*/
printf("%s\n",*q);
}
}
==============================================================
【程序78】
题目:找到年龄最大的人,并输出。请找出程序中有什么问题。
1.程序分析:
2.程序源代码:
#define N 4
#include "stdio.h"
static struct man
{ char name[20];
int age;
} person[N]={"li",18,"wang",19,"zhang",20,"sun",22};
main()
{struct man *q,*p;
int i,m=0;
p=person;
for (i=0;i<N;i++)
{if(m<p->age)
q=p++;
m=q->age;}
printf("%s,%d",(*q).name,(*q).age);
}
==============================================================
【程序79】
题目:字符串排序。
1.程序分析:
2.程序源代码:
main()
{
char *str1[20],*str2[20],*str3[20];
char swap();
printf("please input three strings\n");
scanf("%s",str1);
scanf("%s",str2);
scanf("%s",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);
printf("after being sorted\n");
printf("%s\n%s\n%s\n",str1,str2,str3);
}
char swap(p1,p2)
char *p1,*p2;
{
char *p[20];
strcpy(p,p1);strcpy(p1,p2);strcpy(p2,p);
}
==============================================================
【程序80】
题目:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,这只
   猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了
   一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,
   问海滩上原来最少有多少个桃子?
1.程序分析:
2.程序源代码:
main()
{int i,m,j,k,count;
for(i=4;i<10000;i+=4)
{ count=0;
m=i;
for(k=0;k<5;k++)
{
j=i/4*5+1;
i=j;
if(j%4==0)
count++;
else
break;
}
i=m;
if(count==4)
{printf("%d\n",count);
break;}
}
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
学习编程从借鉴开始,C语言经典算法一百例提供了大量经典算法可供参考 【程序1】 题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 【程序2】 题目:企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数? 【程序3】 题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少? 【程序4】 题目:输入某年某月某日,判断这一天是这一年的第几天? 【程序5】 题目:输入三个整数x,y,z,请把这三个数由小到大输出。 【程序6】 题目:用*号输出字母C的图案。 【程序7】 题目:输出特殊图案,请在c环境中运行, 【程序8】 题目:输出9*9口诀。 【程序9】 题目:要求输出国际象棋棋盘。 【程序10】 题目:打印楼梯,同时在楼梯上方打印两个笑脸。 ....... 共有近一百例,囊括了大部分的C语言编程算法,其中附有程序分析,程序源代码,对C语言的学习有着极大的帮助。
适用于初学者    经典c程序100例==11--20 【程序11】 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月    后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1.程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21.... 2.程序源代码: #include "stdio.h" #include "conio.h" main() { long f1,f2; int i; f1=f2=1; for(i=1;i<=20;i++) { printf("%12ld %12ld",f1,f2); if(i%2==0) printf("\n"); /*控制输出,每行四个*/ f1=f1+f2; /*前两个月加起来赋值给第三个月*/ f2=f1+f2; /*前两个月加起来赋值给第三个月*/ } getch(); } ============================================================== 【程序12】 题目:判断101-200之间有多少个素数,并输出所有素数。 1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,       则表明此数不是素数,反之是素数。        2.程序源代码: #include "stdio.h" #include "conio.h" #include "math.h" main() { int m,i,k,h=0,leap=1; printf("\n"); for(m=101;m<=200;m++) { k=sqrt(m+1); for(i=2;i<=k;i++) if(m%i==0) { leap=0; break; } if(leap) { printf("%-4d",m); h++; if(h%10==0) printf("\n"); } leap=1; } printf("\nThe total is %d",h); getch(); } ============================================================== 【程序13】 题目:打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数    本身。例如:153是一个“水仙花数”,因为153=1的三次方+5的三次方+3的三次方。 1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。 2.程序源代码: #include "stdio.h" #include "conio.h" main() { int i,j,k,n; printf("'water flower'number is:"); for(n=100;n<1000;n++) { i=n/100;/*分解出百位*/ j=n/10%10;/*分解出十位*/ k=n%10;/*分解出个位*/ if(i*100+j*10+k==i*i*i+j*j*j+k*k*k) printf("%-5d",n); } getch(); } ============================================================== 【程序14】 题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。 程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成: (1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。 (2)如果n<>k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你n,  重复执行第一步。 (3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。 2.程序源代码: /* zheng int is divided yinshu*/ #include "stdio.h" #include "conio.h" main() { int n,i; printf("\nplease input a number:\n"); scanf("%d",&n); printf("%d=",n); for(i=2;i<=n;i++) while(n!=i) { if(n%i==0) { printf("%d*",i); n=n/i; } else break; } printf("%d",n); getch(); } ============================================================== 【程序15】 题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,    60分以下的用C表示。 1.程序分析:(a>b)?a:b这是条件运算符的基本例子。 2.程序源代码: #include "stdio.h" #include "conio.h" main() { int score; char grade; printf("please input a score\n"); scanf("%d",&score); grade=score>=90?'A':(score>=60?'B':'C'); printf("%d belongs to %c",score,grade); getch(); } ============================================================== 【程序16】 题目:输入两个正整数m和n,求其最大公约数和最小公倍数。 1.程序分析:利用辗除法。 2.程序源代码: #include "stdio.h" #include "conio.h" main() { int a,b,num1,num2,temp; printf("please input two numbers:\n"); scanf("%d,%d",&num1,&num2); if(num1<num2)/*交换两个数,使大数放在num1上*/ { temp=num1; num1=num2; num2=temp; } a=num1;b=num2; while(b!=0)/*利用辗除法,直到b为0为止*/ { temp=a%b; a=b; b=temp; } printf("gongyueshu:%d\n",a); printf("gongbeishu:%d\n",num1*num2/a); getch(); } ============================================================== 【程序17】 题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 1.程序分析:利用while语句,条件为输入的字符不为'\n'.        2.程序源代码: #include "stdio.h" #include "conio.h" main() { char c; int letters=0,space=0,digit=0,others=0; printf("please input some characters\n"); while((c=getchar())!='\n') { if(c>='a'&&c<='z'||c>='A'&&c<='Z') letters++; else if(c==' ') space++; else if(c>='0'&&c<='9') digit++; else others++; } printf("all in all:char=%d space=%d digit=%d others=%d\n",letters, space,digit,others); getch(); } ============================================================== 【程序18】 题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时    共有5个数相加),几个数相加有键盘控制。 1.程序分析:关键是计算出每一项的值。 2.程序源代码: #include "stdio.h" #include "conio.h" main() { int a,n,count=1; long int sn=0,tn=0; printf("please input a and n\n"); scanf("%d,%d",&a,&n); printf("a=%d,n=%d\n",a,n); while(count<=n) { tn=tn+a; sn=sn+tn; a=a*10; ++count; } printf("a+aa+...=%ld\n",sn); getch(); } ============================================================== 【程序19】 题目:一个数如果恰好等于它的因子之和,这个数就称为“完数”。例如6=1+2+3.编程    找出1000以内的所有完数。 1. 程序分析:请参照程序<--上页程序14. 2.程序源代码: #include "stdio.h" #include "conio.h" main() { static int k[10]; int i,j,n,s; for(j=2;j<1000;j++) { n=-1; s=j; for(i=1;i<j;i++) { if((j%i)==0) { n++; s=s-i; k[n]=i; } } if(s==0) { printf("%d is a wanshu",j); for(i=0;i<n;i++) printf("%d,",k[i]); printf("%d\n",k[n]); } } getch(); } ============================================================== 【程序20】 题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在    第10次落地时,共经过多少米?第10次反弹多高? 1.程序分析:见下面注释 2.程序源代码: #include "stdio.h" #include "stdio.h" main() { float sn=100.0,hn=sn/2; int n; for(n=2;n<=10;n++) { sn=sn+2*hn;/*第n次落地时共经过的米数*/ hn=hn/2; /*第n次反跳高度*/ } printf("the total of road is %f\n",sn); printf("the tenth is %f meter\n",hn); getch(); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值