版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
C语言编程题库1、用选择法对n个数由小到大排序。#include<stdio.h>voidmain()(inta[100],n,i,j,t;scanf("%d",&n);for(i=0;i<n;i++)scanf("%d",&a[i]);for(i=l;i<n;i++)for(j=i;j<n;j++)if(a[i-l]>aU]){t=a[i-l];a[i-l]=a[j];a[j]=t;}for(i=0;i<n;i++)printf("%d",a[i]);printf("\n");}2、将键盘输入的12个数据存放到ー个4行3列的二维数组中,分别求出各行、各列以及表中所有数之和。#include<stdio.h>voidmain()(inta[5][4]={0},i,j;for(i=0;i<4;i++)for(j=0;j<3;j++)scanf("%d",&a[i][j]);for(i=0;i<4;i++)for(j=0;j<3;j++){a[i][3]+=a[i][j];a[4][j]+=a[i][j];a[4][3]+=a[i][j];}for(i=0;i<5;i++){for(j=0;j<4;j++)printf("%d\t",a[i][j]);printf("\n");)}3、判断一个字符串是否为回文串(回文串指正读反读都一样的字符串,如:字符串"abcl21cba")o#include<stdio.h>#include<string.h>voidmain()chara[30],b[30];inti,j;scanf("%s,a);i=strlen(a)-l;j=0;while(i>=0)bO++]=a[H;bO]='\0';if(strcmp(a,b)==O)printf(H%s是ー个回文串'nツa);elseprintf(n%s不是ー个回文串、n”,a);)#include<stdio.h>#include<string.h>voidmain()(chara[30];intij;scanf("%s”,a);i=strlen(a)-l;j=0;while(i>j)if(a[i—]!=a[j++])break;if(i<=j)printf(H%s是ー个回文串'nツa);elseprintf(n%s不是ー个回文串、n”,a);}4、输出杨辉三角形的前10行。。#include<stdio.h>voidmain()(inta[10][10]Jj;for(i=0;i<10;i++)a[i][0]=a[i][i]=l;for(i=2;i<10;i++)for(j=l;j<i;j++)a[i]U]=a[i-l][j]+a[i-l][j-l];for(i=0;i<10;i++){for(j=0;j<=i;j++)printf("%d't”,a。皿);printf("\n");)}5、把输入的十进制数以十六进制数的形式输出。#include<stdio.h>voidmain()(inta[100],x,i=0;scanf(”%d”,&x);while(x){a[i++]=x%16;x/=16;}i-;while(i>=0)(if(a[i]>9)printfC^c'VA'+aliJ-lO);elseprintf(”%d”,a[i]);i一;)printf("\n");)6、删去ー维数组中所有相同的数,使之只剩ー个。数组中的数已按由小到大的顺序排列,函数返回删除后数组中数据的个数。例如,若一维数组中的数据是: 2223445666677899101010 删除后,数组中的内容应该是: 2345678910c#include<stdio.h>voidmain()(inta[19]={2,2,2,3,4,4,5,6,6,6,6,7,7,8,9,9,10,10,10},n,i,j;n=19;for(i=17;i>=0;i-)if(a[i]==a[i+l])(for(j=i+l;j<n;j++)aO-l]=a[j];n-;)for(i=0;i<n;i++)printf(”%d、ピ,a[i]);printf("\n");7、求一个3*3矩阵对角线元素之和。#include<stdio.h>voidmain()(inta[3][3]/ij/sum=0;for(i=0;i<3;i++)for(j=0;j<3;j++)scanf("%d"z&a[i][j]);for(i=0;i<3;i++)sum+=a「巾];〃两条对角线sum+=a[i][i]+a[i][2-i]pnntf("%d\n"zsum);printf("\n");}8、给定一个ー维数组,将数组中所有指定值x全部删除,输出删除后内容。#include<stdio.h>voidmain(){inta[10]/x/n=10JJ;for(i=0;i<10;i++)scanf("%d,&a[i]);scanf("%d”,&x);for(i=9;i>=0;i-)if(a[i]==x)(for(j=i+l;j<n;j++)aD-l]=a[j];n一;)for(i=0;i<n;i++)printf(M%d\t",a[i]);printf("\n");}9、输入一个十进制整数,将其转换为R进制输出(R值为2.16)。#include<stdio.h>voidmain()(inta[100],x,r,i=0;scanf(“%d,%d“,&x,&r);while(x){a[i++]=x%r;x/=r;}i-;while(i>=0)if(a[i]>9)printf("%c";A'+a[i]-10);elseprintf(”%d”,a[i]);i一;)printf("\n");)不使用strcmp函数,编程实现2个字符串的比较。ッ#include<stdio.h>voidmain()(chara[30],b[30];inti=0;scanf("%s%s"zazb);while(a[i]==b[i]&&a[i]&&b[i])i++;if(a[i]==b[i])printf("%s等于%s\n”,a,b);elseif(a[i]<b[i])printf("%s小于%s\n,a,b);elseprintf("%s大于%s\n”,a,b);}*1、 通过键盘输入3个整数,输出最大值和最小值#include<stdio.h>voidmain()(inta,b,c,max,min;scanf(“%d%d%d,&a,&b,&c);max=min=a;if(b>max)max=b;if(c>max)max=c;if(b<min)min=b;if(c<min)min=b;printf("最大值为%d,最小值为%d\nツmax,min);}2、从键盘输入圆的半径r和一个整型数k,当k=l时,计算并输出圆的面积;当k=2时,计算并输出圆的周长。编程实现以上功能#include<stdio.h>voidmain()(intrzk;scanf("%d%d"/&rz&k);if(k==l)printf("圆的面积为%f\nツ3.1415926ケ・r);if(k==2)printf("圆的周长为%f\n”31415926*2*ij;}3、编程实现:输入整数a和b,若a2+b2大于100,则输出a2+b2百位以上的数字,否则输出两数之和。#include<stdio.h>voidmain()(inta,b;scanf("%d%d"/&a/&b);if(a*a+b*b>100)pnntf("%d\nnz(a*a+b*b)/100);elseprintf("%d\n”,a+b);}4、从键盘上输入两个整数,判断其大小关系并将结果输出。#include<stdio.h>voidmain()(inta,b;scanf("%d%d"z&az&b);if(a>b)printf("a>b\n");elseif(a<b)printf("a<b\n");elseprintf("a==b\n");}5、猜数游戏。先将要猜的整数存放在变量num中,当程序运行时,提示游戏者通过键盘输入自己所猜的数cai,若猜对了,即cai==num,屏幕显示"*・恭喜你猜中了!**",然后结束程序运行;如果猜错了,屏幕除了显示"**对不起,你猜错了!**"外,还显示是猜大了或猜小了的信息,以帮助游戏者继续往正确处猜。#include<stdio.h>voidmain()intnum=100zcai;scanf("%d"z&cai);if(cai==num)printf("**恭喜你猜中了!*・、n");elseif(cai<num)print"'**对不起,你猜错了!*・、n**你猜小了!*・、n");elseprintf(”**对不起,你猜错了!*・、n*・你猜大了!*・、n");6、将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。#include<stdio.h>voidmain(){inti,a,f=l;scanf("%d",&a);pnntf(n%d=l",a);for(i=2;i<=a;i++)while(a%i==0){a/=i;printf("*%d",i);}}7、求输入的十个整数中正数的个数及其平均值。#include<stdio.h>voidmain(){inti,a,n=0;floatsum=0;for(i=l;i<=10;i++){scanf("%d",&a);if(a>0){sum+=a;n++;}}printf("正整数的个数为%d,正整数的平均值为%f\n",n,sum/n);}8、某旅行团有男人、女人和小孩共30人,在纽约一家小饭馆里吃饭,该饭馆按人头收费,每个男人收3美元,每个女人收2美元,每个小孩收1美元,共收取50美元。共有多少组解?#include<stdio.h>voidmain(){inti,j,k;for(i=l;i<=10;i++)for(j=l;j<=15;j++)if(i*3+2*j+30-i-j==50)printf("男人%d人,女人%d人,小孩%(1A\n",i,j,30-i-j);)9、在输入的ー批正整数中求出最大者,输入。结束循环。#include<stdio.h>voidmain()intx,max;scanf("%dM,&x);max=x;while(x!=O){ scanf("%d,,/&x);if(x>max)max=x;}printf("最大值为%d\n",max);}输出所有的"水仙花数"。所谓的"水仙花数"是指ー个3位数,其各位数字的立方和等于该数本身。例如,153是ー个水仙花数,因为153=13+53+33#include<stdio.h>voidmain()(inti,nl,n2,n3;for(i=100;i<1000;i++){nl=i/100;n2=i/10%10;n3=i%10;if(nl*nl*nl+n2*n2*n2+n3*n3*n3==i)printf(,'%d\n"J);}}将从键盘输入的ー对数,由小到大排序输出,当输入ー对相等数时结束循环。#include<stdio.h>voidmain()(intnl,n2;scanf("%d%d"/&nl/&n2);while(nl!=n2)(if(nl>n2)printf("%dz%d\n"/n2/nl);elseprintf("%d/%d\n"/nl/n2);scanf("%d%d,'z&nl/&n2);}}12、使用循环语句实现如下图形的输出1123123451234567123456789*/#include<stdio.h>voidmain()intij;for(i=l;i<=5;i++){ for(j=l;j<=5-i;j++)printf("");for(j=l;j<=2*i-l;j++)printf(',%d"J);printf(H\n");)"1、 输入某年某月某日,判断这一天是这一年的第几天?#include<stdio.h>voidmain()(inty,m,d,sum=0;scanf("%d%d%d"/&y/&m/&d);jf(y%4==0&&y%100!=0||y%400==0)sum=l;switch(m)(casel:sum=d;case2:sum=d+31;case3:sum+=d+31+28;case4:sum+=d+31+28+31;case5:sum+=d+31+28+31+30;case6:sum+=d+31+28+31+30+31;case7:sum+=d+31+28+31+30+31+30;case8:sum+=d+31+28+31+30+31+30+31;case9:sum+=d+31+28+31+30+31+30+31+31;case10:sum+=d+31+28+31+30+31+30+31+31+30;casell:sum+=d+31+28+31+30+31+30+31+31+30+31jcase12:sum+=d+31+28+31+30+31+30+31+31+30+31+30;}printf("%d年%d月%d日是这一年的第%d天、n”,y,m,d,sum);}#include<stdio.h>voidmain(){inty,m,d,sum=0;scanf("%d%d%d",&y/&m/&d);switch(m)case12:sum+=30;casell:sum+=31;case10:sum+=30;case9:sum+=31;case8:sum+=31;case7:sum+=30;case6:sum+=31;case5:sum+=30;case4:sum+=31;case3:sum+=28;case2:sum+=31;casel:sum+=d;}jf((y%4==0&&y%100!=0||y%400==0)&&m>2)sum+=l;printf("%d年%d月%d日是这一年的第%d天、n”,y,m,d,sum);)2、编程判断输入的正整数是是否既是5又是7的整倍数。若是,则输出yes:否则输出no。#include<stdio.h>voidmain(){inty;scanf(“%d”,&y);if(y%5==0&&y%7==0)printf("yes\nH);elseprintf("no\nH);}3、简单计算器程序。用户从键盘输入两个整数和一个字符,字符必须是屮、」、メ、ズ其中的ー个,然后程序输出两数作相应运算的结果。输出计算结果。#include<stdio.h>voidmain(){inta,b;charc;scanf(“%d%c%d“,&a,&c,&b);switch(c)(case,+':printf("%d%c%d=%d\n"/a,c/b/a+b);break;case,-':printf("%d%c%d=%d\n"/a/c/b/a-b);break;case'*':printf("%d%c%d=%d\n",a/c/b/a*b);break;case7,:printf("%d%c%d=%f\n"/a/c,b/(float)a/b);break;}4、从键盘输入3个整数,按由小到大的顺序输出。#include<stdio.h>voidmain()(inta,b,c,t;scanf("%d%d%d",&az&bz&c);if(a>b){t=a;a=b;b=t;}if(b>c){t=c;c=b;b=t;}if(a>b){t=a;a=b;b=t;}printf("%d%d%d\n,a,b,c);}5、ー球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多髙?#include<stdio.h>voidmain()(inti;floatt=100,sum=100;for(i=l;i<10;i++)(t=t/2;sum+=t*2;}printf("%f%f\n",sum/t/2);}6、百钱买百鸡:ー百个铜钱买了一百只鸡,其中公鸡ー只5钱、母鸡ー只3钱,小鸡ー钱3只,问ー百只鸡中公鸡、母鸡、小鸡各多少?#include<stdio.h>voidmain()(inti,j,k;for(i=l;i<=20;i++)for(j=l;j<=33;j++)for(k=3;k<100;k+=3)if(5*i+3*j+k/3==100&&i+j+k==100)printf("%d%d%d\n",i,j,k);}7、输出半径为1到10的圆的面积,当面积大于:100时停止输出。#include<stdio.h>voidmain()inti;floatarea;for(i=l;i<=10;i++){area=3.1415926*i*i;if(area>100)break;printf("半径:%d,圆的面积:%f\nソ,area);)}8、猜数游戏:任意设置ー个整数,请用户从键盘上输入数据猜测设置的数是什么,告诉用户是猜大了还是小了。如果10次以内猜对答案,用户获胜。否则,告诉用户设置的数据是什么。#include<stdio.h>voidmain()(inti,s=100,cai;for(i=l;i<=10;i++){scanf(”%d”,&cai);if(cai==s)break;if(cai<s)printf(“猜小了、n");elseprintf("猜大了、n“);}if(i<=10)printf("恭喜你,猜对了!、バ);elseprintf("不好意思,你猜错了,正确答案是%d\n",s);}9、计算:s=l+12+123+1234+12345o#include<stdio.h>voidmain()(inti,s==O,t=O;for(i=l;i<=5;i++){t=t*10+i;s+=t;}printf(”%d、n”,s);}输出100以内能被3整除且个位数为6的所有整数#include<stdio.h>voidmain()inti,t=0;for(i=0;i<=9;i++){t=i*10+6;if(t%3==0)printf("%d\nソ);}}键盘上输入若干个学生的成绩,统计并输出最髙成绩和最低成绩,当输入负数时结束输入#include<stdio.h>voidmain()(inti,score,max,min;scanf("%d",&score);max=min=score;while(score>=0){scanf("%d",&score);if(score>max)max=score;if(score<min&&score>=0)min=score;}printf("最高成绩为:%d,最低成绩为:%d\nH,max,min);自然常数e的计算公式为e=l+l/l!+l/2!+l/3!+...+l/n!+..<〇请编制程序计算e的近似值,要求被舍弃的首项1/n!<0.000001o*/#include<stdio.h>voidmain(){floatn=l,e=l,t;t=l/n;while(t>=0.000001){e+=t;n++;t*=l/n;)printf("%.10f\n",e);}例2.1#include<stdio.h>main(){doublea,b,area;a=1.2; /・将矩形的两条边长分别赋给a和b*/b=3.6;area=a*b; /・计算矩形的面积并存储到变量area中・/printf("a=%f,b=%f,area=%f\n",a/b,area); /・输出矩形的两条边长和面积・/例2.2include"stdio.h"#definePI3.14159 /・定义符号名PI为3.141597main(){doubler,s;r=5.0;s=PI*r*r;printf(Ms=%f\n",s);}2.24#includestdio.h;main();/*mainfunction*/floatr,s;/*/*risradius*/,/*sisareaofcircular*/*/r=5.0;s=3.14159ケ・r;printfド%f\n",s)printf("%f\n",s)2.25main/*mainfunction*/{floata,b,c,v;/*a,b,caresides,visvolumeofcube*/a=2.0;b=3.0;c=4.0v=a*b*c;printf("%f\n",v)}3.2.1#include<stdio.h>main(){inti=2518;doublea=3.1415;printf("i=%d,a=%f,a*10=%e\n",i,a,a*10);例3.1#include<stdio.h>main(){intx,y,t;printf(HEnterx&y:\n");scanf(M%d%d,,/&x,&y);printf("x=%dy=%d\n",x,y);t=x;x=y;y=t;printf("x=%dy=%d\n",x,y);}例3.2#include<stdio.h>main(){doublex;printf("Enterx:\n");scanf(M%lfM/&x);printf("(l)x= x);x=x*100;x=x+0.5;x=(int)x;x=x/100;printf("(2)x=%f\n,,zx);}3.4#include<stdio.h>main(){intx=10,y=3;printfド%d\n",y=x/y);}3.16main{doublea,b,c,s,v;printf(inputa,b,c:\n);scanf("%d%d%d”,a,b,c);s=a*b; /*计算长方形面积・/v=a*b*c; /*计算长方体体积・/printfド%d%d%dウa,b,c);printfドs=%f\n/;sjv=%d\n»;例4.1#include<stdio.h>main(){intx,y;printf("Enterx&y:\n");scanf("%d%d"/&x,&y);printf("x,y:%d%d\n",x,y);if(x>y)printf("x=%d\n",x);if(y>x)printf("y=%d\n"/y);if(x==y)printf("x==y%d:\n"zx);}例4.2#include<stdio.h>main(){inta,b,c,t;printf("inputa,b,c:”);scanf("%d%d%d"z&az&bz&c);printf("a=%dzb=%dzc=%d\n"zazbzc);if(a>b) /・如果a比b大,则进行交换,把小的数放入a中・/{t=a;a=b;b=t;}if(a>c) /・如果a比c大,则进行交换,,把小的数放入a中・/{t=a;a=c;c=t;} /・至此a、b、c中最小的数已放入a中・/if(b>c) /・如果b比c大,则进行交换,把小的数放入b中・/{t=b;b=c;c=t;} /・至此a、b、c中的数已按由小到大顺序放好*/printf("%d,%d,%d\n",a,b,c);}例4.3#include<stdio.h>main(){intxzy;printf("Enterx&y:");scanf(H%d%d"z&xz&y);printf("xzy:%d%d\nHzxzy);if(x>y)printf(Hmax=x=%d\n"zx);elseprintf("max=y=%d\n"zy);printf(”**end**\n“);}例4.4#include<stdio.h>main(){intn;printf("inputn:");scanf(“%d“,&n);if(n%3==0) /*判断n能否被3整除・/printf("n=%dYES\n",n);elseprintf(”n=%dNO\n",n);例4.5#include<stdio.h>main(){intg;printf(HEnterg:scanf(”%d”,&g);printf("g=%d:\g);if(g>=90)printf("A\n");elseif(g>=80)printf("B\n");elseif(g>=70)printf("C\nM);elseif(g>=60)printf("D\n");elseprintf("E\nn);例4.6#include<stdio.h>main(){intg;printf("Enteramark:");scanf("%d",&g);/*g中存放学生的成绩ッprintf("g=%d:nzg);switch(g/10){case10:case9:printf("A\n");case8:printf("B\n");case7:printf(HC\n");case6:printf("D\n");default:printf("E\n");}#include<stdio.h>main(){intg;printf("Enteramark:");scanf(&g);/*g中存放学生的成绩ッprintf("g=%d:M/g);switch(g/10){case10:case9:printf("A\n");break;case8:printf("B\nH);break;case7:printf("C\n");break;case6:printf("D\n");break;default:printf("E\n");}}4.5#include<stdio.h>main(){inta=2,b=-l,c=2;if(a<b)if(b<0)c=0;elsec+=1;printfド%d\n”,c);}4.6#include<stdio.h>main(){intw=4,x=3,y=2,z=l;printfぐ’%d\n",(w<x?w:z<y?z:x));}4.7#include<stdio.h>main(){inta,b,s;scanf("%d%d”,&a,&b);s=a;if(a<b)s=b;s*=s;printf("%d\n,:s);4.9#include<stdio.h>main(){intk=-3;if(k<=O)printf(//****\nw)elseprimf("&&&&、&');4.17#include<stdio.h>main(){inta=100;if(a>100)printf("%d\n”,a>100);elseprintf("%d\n",a<=100);例5.1#include<stdio.h>main(){intizsum;i=0;sum=0; /*i和sum的初值为0*/while(sum<10000)/・当sum小于!0000时执行循环体・/{sum+=i*i;/*sum累加1的平方・/i++; /・在循环体中每累加一次后,i增1ツ}printf("n=%dsum=%d\n",i-lzsum);例5.2#include<stdio.h>include<math.h>/・调用fabs函数时要求包含math.h文件37main(){ints;floatn,t,pi;t=1.0; /*t中存放每项的值,初值为1*/pi=0; /*pi中存放所求打的值,初值为0*/n=1.0; /*n中存放每项分母・/s=l; ドs为符号位,其值在1和-1之间变化・/while(fabs(t)>=le-6){pi=pi+1;n+=2.0;/*改变符号*/t=s/n;}pi=pi*4;printf("pi=%f\n,,,pi);}例5.3#include<stdio.h>#include<math.h>main(){doublexl,x2;xl=0.0;x2=cos(xl);while(fabs(x2-xl)>le-6){xl=x2;x2=cos(xl);}printf(,,x=%f\n"/x2);例5.4#include<stdio.h>main(){intflzf2,f;fl=0;f2=l;do{f=fl+f2;fl=f2;f2=f;}while(f2<=1000);printf("F=%d\n",f2);例5.5#include<stdio.h>main(){inti,sum;sum=0; /*sum的初值为〇・/for(i=l;i<=100;i++) /・当i小于或等于100时执行循环体57sum+=i; /・在循环体中累加i一次ッprintf("sum=%d\n",sum);例5.6#include<stdio.h>main(){doubler,s,Pi=3.1416;for(r=0.5;r<=2.5;r+=0.5){s=Pi*r*r; /*计算圆面积s的值・/printf("r=%3.1fs=r,s);}例5.7#include<stdio.h>main(){intk,i,j;for(i=0;i<4;i++){for(k=l;k<=i;k++)printf("");for(j=0;j<7-i*2;j++)printf("*M);printf(n\n");}例5.8#include<stdio.h>main(){intk,i,tag;for(i=2;i<=100;i++){tag=0;for(k=2;k<i;k++)if(i%k==0)tag=l;if(tag==0)printf(”%d,”,i);}}#include<stdio.h>#include<math.h>main(){intk,i,tag;printf("2,");for(i=3;i<=100;i+=2){tag=0;for(k=2;tag==0&&k<sqrt(i);k++)if(i%k==0)tag=l;if(tag==0)printf("%d,”,i);})例5.9#include<stdio.h>main(){inti,s;s=0;for(i=1;;i++){s=s+i;if(s>5000)break;}printf("s=%d,i=%d\n",s,i);例5.10#include<stdio.h>main(){intk=0,s=0,i;for(i=1;i<=5;i++){s=s+i;if(s>5){printf("****i=%d,s=%d,k=%d\n",i,s,k); /*1#输出语句・/continue;}k=k+s;printf("i=%d,s=%d,k=%d\n",i,s,k);/*2#输出语句・/}例5.11#include<stdio.h>main(){intx,max;printf("Enter-ltoend:\n");do{printf(HEnterx:");scanf("%d",&x);}while(x<0&&x!=-1);max=x;while(x!=-1){printf("Enterx:");scanf("%dM/&x);if(x>0&&x>max)max=x;/*max始终存放大于零的最大值・/}if(max!=-1)printff'max=%d\n",max);}例5.12#include<stdio.h>#include<math.h>main(){floata,xO,xl;printf("\nlnputa:");scanf("%f",&a);if(a<0)printf("error!\n");/・不能求负数的平方根・/else{x0=a/2;xl=(x0+a/xO)/2;do{xO=xl;xl=(x0+a/xO)/2;)while(fabs(xO-xl)>le-6);printf("sqrt(%f)=%f标准sqrt(%f)=%f\n",a,xl,a,sqrt(a));}}5.5#include<stdio.h>main(){int\,sum;for(i=1;i<6;i++)sum+=sum;printf("%d\n",sum);}5.6#include<stdio.h>main(){inty=10;for(;y>0;y-)if(y%3==0){printf("%d",-y);continue;}}5.8#include<stdio.h>main(){inti;for(i=1;i<=5;i++){if(i%2)printf(〃*");elsecontinue;printf("#");}printf("$\n");}5.10#include<stdio.h>main(){intx,i;for(i=l;i<=100;i++){x=i;if(++x%2==0)if(++x%3==0)if(++x%7==0)printf("%d”,x);}printf("\n");}5.13#include<stdio.h>main(){intx=2;while(X-);printf("%d\n",x);5.16#include<stdio.h>main(){floatx,amax,amin;scanf("%r,&x);amax=x;amin=x;while([1]){if(x>amax)amax=x;if([2])amin=x;scanf("%f〃,&x);)printf("\namax=%f\namin二%f\n”,amax,amin);}例6.1#include<stdio.h>main(){charch;inti;for(i=0;i<26;i++){ch=i+65;if(i%2==0)printf("\n");printf("c=%cASCII=%d",ch,ch);}putchar(\n);)例6.2#include<stdio.h>main()(printf("PressEntertocontinue\nH);while(getchar()!=*\n');}例6.3#include<stdio.h>main(){chare;while((c=getchar())!='\n'){if(c>='a'&&c<='z')c=c-'a'+'A';putchar(c);}putchar('\n');例6.4#include<stdio.h>#include<ctype.h>main(){longn=O;charch;while((ch=getchar())!='!') /・未遇到结束标志时循环继续・/if(isspace(ch))n++;printf("n=%ld\n,n);例6.5#include<stdio.h>#include<ctype.h>main(){chare;c=getchar();while(c!ゴ@リ/・未遇到结束标志时循环继续・/{if(isalpha(c))/・调用函数判断c中是否为字母*/{c=tolower(c);/・调用函数把大写字母转换成小写字母・/c=(c-'a,+l)%26+'a';)putchar(c);c=getchar();}printf(n\nM);}6.7#include<stdio.h>main(){charcl=A/c2=Y;printfド%d,%d\n”,cl,c2);}6.8#include<stdio.h>main(){charx=A;x=(x>=A&&x<=Z)?(x+32):x;printf("%c\n",x);6.9#include<stdio.h>main(){charch;ch=A+5-3;printf("%d,%c\n”,ch,ch);}6.10#include<stdio.h>main(){intc;while((c=getchar())!=\n){switch(c-2){case0:putchar(c+4);:putchar(c+4);break;:putchar(c+3);:putchar(c+2);break;}}printf(〃\n");}例7.2#include<stdio.h>voidtry(int,int,int);/*说明函数try为无值型,含有3个int类型的形参ッmain(){intx=2,y=3,z=0;printf("⑴x=%dy=%dz=%d\n",x,y,z);try(x,y,z);printf("(4)x=%dy=%dz=%d\n",x,y,z);}voidtry(intx,inty,intz){printf("(2)x=%dy=%dz=%d\n",x,y,z);z=x+y;X=X*X;y=y*y;printf("⑶x=%dy=%dz=%d\n",x,y,z);例7.3#include<stdio.h>voidswap(intzint);/・函数说明语句・/main(){intx=10,y=20;printf("⑴x=%dy=%d\n",x,y);swap(x,y);printf("⑷x=%dy=%d\n",x,y);voidswap(inta,intb){intt;printf("(2)a=%db=%d\n",a,b);t=a;a=b;b=t;printf("⑶a=%db=%d\n",a,b);}例7.4#include<stdio.h>intisprime(int); /*isprime函数的说明语句・/main(){intx;printf("Enteraintegernumber:");scanf("%d",&x); /*从键盘输入ー个整数・/if(isprime(x))printf("%disprime\n",x); /*当函数返回1时,输出”是素数“ツelseprintf("%disnotprime\n",x);/*当函数返回〇时,输出"不是素数”*/}intisprime(inta){inti;for(i=2;i<=a/2;i++)if(aintisprime(inta){inti;for(i=2;i<=a/2;i++)if(a%i==0)return0;return1;/*a一旦能被某个数整除,即不是素数,返回。*//*a不能被2到a/2之间任何ー个数整除,返回1*/例7.5/♦/♦isprime函数的说明语句・//♦/♦even函数的说明语句・/{inta;printf("Enteraevennumber:");scanf("%d"z&a);if(a%2==0)even(a);elseprintf("The%disntevennumber'd,a);}voideven(intx) /・定义even函数*/{inti;for(i=2;i<=x/2;i++)if(isprime(i))if(isprime(x-i)){printf("%d=%d+%d\n",x,i,x-i);return;}}intisprime(inta)/*isprime函数定义与例フ.4同,在此省略・/{……}例7.6#include<stdio.h>intsum(int);/*sum函数的说明语句・/intf(int);/*f函数的说明语句・/main(){inta,b;printf("Enteraintegernumber:n); /・输入ー个整数・/scanf("%d",&a);b=sum(a); /・调用sum函数求f(x)的累加和・/printf("a=%dsum=%d\n",a,b);}intsum(intn) /・定义sum函数*/{intx,s=0;for(x=0;x<=n;x++)s+=f(x);returns; /・返回累加和57}intf(intx) /・定义f(x)函数・/{returnx*x+l;}例7.7#include<stdio.h>#include<math.h>doubleinteg(doublea,doubleb){doubles,x,h;intn=100,i;h=fabs(b-a)/n; /*fabs()为求绝对值函数・/s=(sin(a)+sin(b))/2.0;for(i=1;i<=n-1;i++){x=a+i*h;s=s+sin(x);}s=s*h;returns;}main(){doubles;s=integ(0.0,1.5);printf("s=%f\n"/s);例7.8#include<stdio.h>#include<ctype.h>charmyupper(charch){if(ch>=a&&ch<=z)ch=ch-32;returnch;}main(){charc;while((c=getchar())!='@*){c=myupper(c);putchar(c);})例7.9#include<stdio.h>Longcountch();/*countch函数说明语句・/main(){longn;n=countch();printf("n=%ld\n",n);longcountch(){longcn;for(cn=0;getchar()!='@';cn++);returncn;}例7.10#include<stdio.h>#defineIN1 /・标志在一个单词的内部・/#defineOUTO /・标志在ー个单词的外部・/intcountword(); /・函数说明语句・/main(){intn;n=countword();pnntf("n=%d\n",n);}intcountword(){intc,nw,state;state=OUT; /・在单词的外部57nw=0;while((c=getchar())!=@){if(c=="||c==,\n,||c==,\t,)state=OUT; /・读入的字符是分隔符,在ー个单词的外部ッelseinstate==OUT) /・如果遇到单词的第一个字符・/{state=IN; /・状态变量置成IN:在单词的内部・/nw++; /・单词个数增1*/})returnnw;}7.4#include<stdio.h>intfunc(inta,intb){intc;c=a+b;returnc;}main(){intx=6,y=7,z=8,r;r=func((x-ウy++,x+yレz—);printf("%d\n",r))7.5#include<stdio.h>intf(int,int);main(){inti=2,p;p=f(i,i+1);printf(〃%d\n”,p);}intf(inta,intb){intc;c=a;if(a>b)c=1;elseif(a==b)c=0;elsec=-1;return(c);}7.6#include<stdio.h>intfun(inta,intb,intc){c=a*b;}main(){intc;fun(2,3,c);printf("%d\n",c);}7.7#include<stdio.h>doublef(intn){inti;doubles;s=1.0;for(i=1;i<=n;i++)s+=1.0/i;returns;)main(){inti,m=3;doublea=0.0;for(i=0;i<m;i++)a+=f(i);printf("%f\n",a);7.8#include<stdio.h>unsignedfun6(unsignednum){unsignedk=1;do{k・二num%1〇;num/=10;}while(num);returnk;}main(){unsignedn=26;printfド%d\n”,fun6(n));}7.9#include<stdio.h>doublesub(doublex,doubley,doublez){y-=1.0;z=z+x;returnz;}main(){doublea=2.5,b=9.0;printfウsub(b-a,a,a));}7.10#include<stdio.h>intfun2(inta,intb){intc;c=(a*b)%3;returnc;}intfunl(inta,intb){intc;a+=a;b+=b;c=fun2(a,b);returnc*c;)main(){intx=11,y=19;printf("%d\n",fun1(x,y));}例8.1
include<stdio.h>main(){inta,b,min,*pa,*pb,*pmin;/・输入的值依次放入/・输入的值依次放入pa、pb所指存储单元中・//・通过输出,验证a、b中的值・//・假定a中的值最小,将其赋给min*//・若b中的值比a小,将其赋给min*//・输出小值・/printf("a=%db=%d\n",a,b);*pmin=*pa;if(*pa>*pb)*pmin=*pb;printf("min=%d\n",min);}例8.2#include<stdio.h>intmyadd(int*a,int*b){intsum;sum=*a+*b;returnsum;}main(){intx,y,z;printf("Enterx,yゴ');scanf("%d%d",&x,&y);z=myadd(&x,&y);printf("%d+%d=%d\n",x,y,z);例8.3#include<stdio.h>voidswap(int*,int*);main(){intx=30,y=20;printf("⑴x=%dy=%d\n",x,y);swap(&x,&y);printf("(4)x=%dy=%d\n",x,y);}voidswap(int*a,int*b){intt;print"。)a=%db=%d\n",*a,*b);t=*a;*a=*b;*b=t;printf("(3)a=%db=%d\n",*a,*b);例8.4#include<stdio.h>voidswap(int*xl,int*x2){intt;t=*xl;*xl=*x2;*x2=t;}voidorder(int*azint*b){if(*a>*b)swap(a,b);}main(){intx,y;printf(nEnterx,y:");scanf("%d%d"/&xz&y);printf(Hx=%dy=%d\n"/x/y);order(&x,&y);printf(Hx=%dy=%d\n"/x,y);例8.5#include<stdio.h>int*fun(int*zint*); /・函数说明语句・/main(){int*pjJ;printf("Entertwonumber:");scanf("%d%d"/&i,&j);p=fun(&L&j); /*p将得到i或j的地址・/printf("i=%dj=%d,*p=%d\n"/j,*p);}int*fun(int*a,int*b){if(*a>*b)returna;returnb;}8.2#include<stdio.h>main(){printf("%d\n〃,NULL);}83#include<stdio.h>voidsub(intx,intyzint*z){*z=y-x;}main(){inta,b,c;sub(10,5,&a);sub(7,a,&b);sub(a,b,&c);printf("%d,%d,%d\n”,a,b,c);8.4#include<stdio.h>main(){intk=2,m=4,n=6,*pk=&k,*pm=&m,*p;♦(p=&n)=*pk*(*pm);printf(〃%d\n”,n);8.8#include<stdio.h>voidprtv(int*x){printf(〃%d\n”,++*x);}main(){inta=25;prtv(&a);}8.9#include<stdio.h>main(){int**k,*a,b=100;a=&b;k=&a;printf(〃%d\n”,**k);}8.10#include<stdio.h>voidfun(float*azfloat*b){floatw;*a=*a+*a;w=*a;*a=*b;*b=w;}main(){floatx=2.0zy=3.0,*px=&x,*py=&y;fun(px,py);printf(//%2.0f/%2.0f\n/:x,y);}8.11#include<stdio.h>voidsub(doublex,double*y,double*z){*y=*y-1.0;*z=*z+x;}main(){doublea=2.5,b=9.0,*pa,*pb;pa=&a;pb=&b;sub(b-a,pa,pa);printf("%f\n",a);8.14#include<stdio.h>intast(intx,inty,int*cp,int*dp){*cp=x+y;*dp=x-y;}main(){intc,d;ast(4,3,&c,&d);printf("%d%d\n”,c,d);例9.1#include<stdio.h>#defineM30main(){ints[M],i,k=l;for(i=0;i<M;i++){s[i]=k;k+=2;} /*给s数组元素依次赋1、3ヽ…*/printf("\nSequenceOutput:\n");/・按从前到后的顺序输出・/for(i=0;i<M;i++){printf("%4d"/s[i]);if((i+l)%10==0)printf("\n");/・利用i控制换行符的输出ッ}printf("\nlnvertOutput:\n");/・按从后到前的顺序输出・/for(i=M-1;i>=0;i-) /・下标值从大到小・/printf("%3d%c",s[i],(i%10==0)?'\n':"); /・利用条件表达式来决定输出换行符还是输出空格・/printf("\nM);例9.2#include<stdio.h>#defineM100voidarrout(int*zint);/・函数说明语句,此函数用以输出数组中的值・/intarrin(int♦); /・函数说明语句,此函数用以给数组输入数据・/main(){ints[M]zk;k=arrin(s); /*k得到输入数据的个数・/arrout(s,k);}intarrin(int*a){inti,x;i=0;scanf("%d",&x);while(x>=0){*(a+i)=x;i++;scanf(“%d”,&x);}returni;}voidarrout(int*a,intn){inti;for(i=0;i<n;i++)printf(((i+l)%5==0)?"%4d\n":"%4d",*(a+i));/・根据i的值来确定使用不同的格式串・/printf("\n");}例9.3#include<stdio.h>#defineM10#defineB4voidsetstar(char*,int);voidarrout(char*,int);main(){charc[M]={A/B,C,D,E,F,G,H,I,J};setstar(&c[4],M-B);arrout(c,M);voidsetstar(char*azintn){inti;for(i=0;i<n;i++)*(a+i)=*;}voidarrout(char*a,intn){inti;for(i=0;i<n;i++)printf("%c"za[i]);printf(H\n");}#include<stdio.h>#defineN10int*fun(inta[N]zintn){intb[N];returnb;main(){intw[N]z*p;p=fun(wzN);}例9.4#include<stdio.h>#include"stdlib.h"#defineSIZE15#defineN3voidgetrand(int*zint); /・三个函数说明语句・/voidgetave(int*zint*zint);voidpriarr(int*zint);main(){intx[SIZE],w[SIZE/N]={0}; /*w数组中置初值〇,准备存放5个和值・/getrand(x,SIZE);/・产生15个随机数放入x数组中・/printf("Output%drandomnumbers:\n",SIZE);priarr(x,SIZE); /・输出15个随机数ッgetave(x,w,SIZE);/*每3个数求一个和数放入w数组・/printf(nOutput5sumnumbers:\n");priarr(wySIZE/N);}/・输出5个和数・/voidgetrand(int*azintn){inti;for(i=0;i<n;i++)a[i]=rand()%50;}voidgetave(int*a,int*b,intn){inti,j,sum;for(sum二〇」ニ〇/=0;i<=n;i++){sum+=a[i];if((i+l)%3==0){b[j]=sum;sum=0;j++{sum+=a[i];if((i+l)%3==0){b[j]=sum;sum=0;j++;)/・每累加3个数进行一次处理・//*和值放入w数组中・//・置〇,准备重新累加・//・下标增1*/voidpriarr(int*a,intn){inti;for(i=0;i<n;i++){printf(”%5d,a[i]);if((i+l)%5=0)printf("\n");}printf("\nn);)例9.5#include<stdio.h>#defineNUM8voidinvert(int*,int); /・函数说明语句・/voidpriout(int*,int); /・函数说明语句・/main(){/・用置初值的方法给a数组放入数据・/inta[NUM]={10,20,30,40,50,60,70,80);printf("〇utputprimarydata:"); /・输出原始数据・/priout(a,NUM);invert(a,NUM);printf("Outputtheinversedata:"); /・输出颠倒后的数据・/priout(a,NUM);voidpriout(ints[],intn){inti;for(i=0;i<n;i++)printf("%4d",s[i]);printf("\nn);}voidinvert(int*a,intn){inti,j,t;i=0;j=n-l;while(i<j)/*i是最前元素的下标,j是最后元素的下标*/当i大于或等于j时,对调完成・/{t=a[i];a[i]=a[j];a[j]=t;i++;j-;}/・下标为i和j的两个元素中的值对调ッ/*!向后移ー个位置,j向前移ー个位置*/例9.6#include<stdio.h>#include<stdlib.h>#defineM50#defineN10voidgetdata(int*s){inti;for(i=0;i<M;i++)s[i]=rand()%10;)voidstat(int*a,int*c){inti;for(i=0;i<N;i++)c[i]=0;for(i=0;i<M;i++)c[a[i]]++;)ドc数组元素置初值0ッ/・用c数组的元素对各整数进行统计・/voidoutdata(int*c){inti;for(i=0;i<N;i++)printf(H%d:%d\n",i,c[i]);main(){inta[M],c[N];getdata(a);stat(a,c);printf("Outputtheresult:\n");outdata(c);例9.7#include<stdio.h>#defineNUM30intarrin(int*a){intizn;Do /・当指定数组元素个数超出范围时,重新输入*/{printf(HEnternumberofelements,0<=n<&d:",NUM);scanf("%d",&n);}while((n<1)||(n>=NUM));/・注意:输入的n个整数,放入a[0]至a[n・l]中,a[n]在查找时用作辅助空间・/printf("Enter%dintegernumbers:\n",n);for(i=0;i<n;i++)scanf("%d",a+i);returnn;intsearch(int*a,intx,intn){inti,p; /・查找开始・/i=0; /・从a[0]元素开始查找・/a[n]=x; /・把待查找的数放入a[n)中・/while(x!=a[i])i++;/・只要x值等于当前a[i]的值,则退出循环,否则i后移一位ッif(i==n)p=-1;/・没找到・/elsep=i; /*找到・/returnp;}main(){inta[NUM],x,n,p;/・暂定数组含30个元素,下标的上限为29*/n=arrin(a);printf("Enterthenumbertosearch:x=");scanf("%d",&x);p=search(a,x,n);if(p!=-l)printf("%dindexis:%d\n",x,p); /・输出x所在下标ッelseprintf("%disnotfound!\n",x); /・输出没找到信息・/)例9.8#include<stdio.h>#defineNUM10 /・假定数组含有10个元素・/intarrdel(int*,int,int);/*函数说明语句・/voidarrout(int*,int); /*函数说明语句・/intgetindex(intn);main() /・用置初值的方法给数组置数・/{intn,d,a[NUM卜{21,22,23,24,25,26,27,28,29,30};n=NUM;printf("Outputprimarydata:\n"); /・输出原始数据・/arrout(a,n);d=getindex(n);n=arrdel(a,n,d);printf("Outputthedataafterdelete:\nn); /*输出删除后的数据・/arrout(a,n);}voidarrout(intw[],intm){intk;for(k=0;k<m;k++)printf("%dH,w[k]);printf("\n");}intarrdel(int*w,intn,intk){inti;/*以下循环进行如图9.4所示的删除操作・/for(i=k;i<n-l;i++)w[i]=w[i+l];n-; /・删除后数据个数减1*/returnn;}getindex(intn){inti;Do /・当指定的下标值落在数据所在下标范围内时,退出循环,返回该下标值・/{printf("\nEntertheindex[0<=i<%dJ:,n); /*输入待删元素的下标ッscanf(H%d",&i);}while(i<0||i>n-l);returni;例9.9#include<stdio.h>#defineNUM6voidarrso
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论