C语言编程题精选-.pdf





《C语言编程题精选-.pdf》由会员分享,可在线阅读,更多相关《C语言编程题精选-.pdf(45页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、1 参考答案1、编程实现对键盘输入的英文名句子进行加密。用加密方法为,当内容为英文字母时其在26 字母中的其后三个字母代替该字母,若为其它字符时不变。#include main()char c;printf(please input a string:);while(c=getchar()!=n)if(c=A&c=a&c=X&c=x&c=z)c=(c+3)-26;printf(%c,c);printf(n);return 0;2、编程实现将任意的十进制整数转换成R 进制数(R 在 2-16 之间)。#include#include void main()int n,R,i=0,a16=0;ch
2、ar str16=0123456789ABCDEF;clrscr();printf(please input an integer number:n);scanf(%d,&n);printf(please input R:n);scanf(%d,&R);if(n0)printf(%c,strai);getche();3、从键盘输入一指定金额(以元为单位,如345.78),然后显示支付该金额的各种面额人民币数量,要求显示100 元、50 元、10元、5 元、2 元、1 元、1 角、5 分、1 分各多少张。#include main()int a1,a2,a3,a4,a5,a6,a7,a8,a9;
3、float f;a1=a2=a3=a4=a5=a6=a7=a8=a9=0;printf(please input a float number:);scanf(%f,&f);while(f100.0)f=f-100.0;a1+;while(f50.0)f=f-50.0;3 a2+;while(f10.0)f=f-10.0;a3+;while(f5.0)f=f-5.0;a4+;while(f1.0)f=f-1.0;a5+;while(f0.5)f=f-0.5;a6+;while(f0.1)f=f-0.1;a7+;while(f0.05)f=f-0.05;a8+;while(f0.01)f=f-0
4、.01;4 a9+;printf(a1=%d,a2=%d,a3=%d,a4=%d,a5=%d,a6=%d,a7=%d,a8=%d,a9=%dn,a1,a2,a3,a4,a5,a6,a7,a8,a9);return 0;4、随机产生 20 个10,50 的正整数存放到数组中,并求数组中的所有元素最大值、最小值、平均值及各元素之和。#include#include void main()int a21,i,ran,max,min,sum,average;clrscr();for(i=1;i=20;i+)while(ran=random(51)/10=0)/ensure ran between 20
5、 and 50;ai=ran;max=min=a1;/initialize here sum=0;for(i=1;i=20;i+)printf(a%d=%dn,i,ai);sum+=ai;if(maxai)min=ai;5 average=sum/20;printf(nsum=%d,max=%d,min=%d,average=%d n,sum,max,min,average);puts(nany key to exit!);getche();5、编程在一个已知的字符串中查找最长单词,假定字符串中只含字母和空格,空格用来分隔不同单词。#include#include void main()cha
6、r string80,*p;int i=0,max=0;clrscr();printf(please input a string:n);gets(string);/printf(n%sn,string);p=string;while(*p!=0)if(*p=)if(max=i)max=i;i=0;else 6 i+;p+;if(max=i)max=i;printf(nmax_length of the string is:%d n,max);getche();1、模拟 n 个人参加选举的过程,并输出选举结果:假设候选人有四人,分别用A、B、C、D 表示,当选某候选人时直接输入其编号(编号由计
7、算机随机产生),若输入的不是 A、B、C、D则视为无效票,选举结束后按得票数从高到低输出候选人编号和所得票数。#include#include#include#include void main()int i,n,A,B,C,D,abandon,ran;/abandon stand for the ones who abandon clrscr();A=B=C=D=abandon=0;printf(Enter the number of persons:);scanf(%d,&n);/n persons to select from candidate a,b,c and d.randomiz
8、e();for(i=1;i=B&A=C&A=D&A!=-1)printf(A=%d,A);A=-1;if(B=A&B=C&B=D&B!=-1)printf(B=%d,B);B=-1;if(C=A&C=B&C=D&C!=-1)printf(C=%d,C);C=-1;8 if(D=A&D=B&D=C&D!=-1)printf(D=%d,D);D=-1;printf(abandon=%d,nn,abandon);getch();8、已知 abc+cba=1333,其中 a,b,c 均为一位数,编程求出满足条件的 a,b,c 所有组合。#include#include void main()int n
9、um,a,b,c;clrscr();printf(Press any key to calculate!nn);getch();for(num=101;num1000;num+)a=num/100;b=num%100/10;c=num%10;if(num+c*100+b*10+a=1333)printf(n%d+%d=1333n,num,c*100+b*10+a);while(!kbhit();9 2、编制一个完成两个数的四则运算程序。如:用户输入34+56则输出结果 90.00。要求运算结果保留两位小数,用户输入时一次将两个数和操作符输入。#include#include void main
10、()float num1,num2;char s;clrscr();printf(Enter a statement please:);scanf(%f%c%f,&num1,&s,&num2);switch(s)case+:printf(%.2f%c%.2f=%.2f,num1,s,num2,num1+num2);break;case-:printf(%.2f%c%.2f=%.2f,num1,s,num2,num1-num2);break;case*:printf(%.2f%c%.2f=%.2f,num1,s,num2,num1*num2);break;case/:printf(%.2f%c%
11、.2f=%.2f,num1,s,num2,num1/num2);break;default:printf(Input Error!);getch();3、输入一个五位以内的正整数,(1)判断它是一个几位数;(2)请按序输出其各位数字;(3)逆序输出其各位数字。如输入:56439,输出:5 位数10 5,6,4,3,9 9,3,4,6,5#include#include long power(int n)int i;long result=1;for(i=1;i1)j=power(count);printf(%d,n/j);11 n=n%j;printf(%dn,n);n=num;while(t
12、mpcount-1)/statement A printf(%d,n%10);n=n/10;printf(%dn,n);getch();4、编写子函数:(1)用冒泡法将一个数组排成升序的函数-SUB1;(2)在升序数组中插入一个数,并且保持该数组仍为升序数组的函数-SUB2。主函数:输入任意 10 个正整数给数组;调用 SUB1 对数组进行排序;从键盘输入一个正整数,调用SUB2将其插入该数组。#include#include void main()int i,k,a12=0;/a0 for no use void sub1(int b),sub2(int b,int k);clrscr();
13、printf(Please input 10 numbers:);for(i=1;i=10;i+)scanf(%d,&ai);getchar();sub1(a);12 for(i=1;i=10;i+)printf(na%d=%dn,i,ai);printf(nnplease input a number to be inserted into the array:);scanf(%d,&k);sub2(a,k);for(i=1;i=11;i+)printf(na%d=%dn,i,ai);puts(nAny key to exit!);getch();void sub1(b)int b;int
14、i,j,t;for(i=1;i10;i+)/the first one is always the smallest for(j=i;jbj)t=bi;bi=bj;bj=t;void sub2(int b,int k)int i;for(i=10;i=1;i-)13 if(kbi)bi+1=bi;else bi+1=k;break;5、编写函数:(1)用选择法将数组排成降序的函数-SUB1;(2)用折半查找法查找某数是否在给定的数组当中的函数-SUB2。主函数:输入任意 10 个正整数给数组,调用 SUB1对数组进行排序,从键盘输入一个正整数,调用SUB2 在数组中进行查找,找到后输出“OK”
15、,没有找到则输出“NO FOUND!”。#include#include void main()int i,key,a11=0,sub1(),sub2();printf(please input 10 number:);for(i=1;i=10;i+)scanf(%d,&ai);getchar();sub1(a);for(i=0;i=10;i+)printf(a%d=%d,i,ai);printf(n please input a key number:);scanf(%d,&key);sub2(a,key,1,10);14 getch();int sub1(int b)int t,i,j,p
16、ost;for(i=1;i10;i+)post=i;for(j=i+1;jbj)post=j;if(post!=i)t=bi;bi=bpost;bpost=t;return 0;int sub2(int c,int k,int n0,int n1)int i=n0,j=n1,m;m=(i+j)/2;while(i=j)if(kcm)15 i=m+1;if(k=cm)break;m=(i+j)/2;if(k=cm)printf(OK!n);else printf(NO FOUND!n);return 0;6、编写一个程序,输入两个包含5 个元素的数组,先将两个数组升序排列,然后将这两个数组合并成
17、一个升序数组。#include#include void main()int i,j,k,a6=0,b6=0,c11=0,sub1();clrscr();printf(nplease input 5 int numbers to array1:);for(i=1;i=5;i+)/a0 for no use scanf(%d,&ai);getchar();sub1(a,5);printf(nplease input 5 int numbers to array2:);for(i=1;i=5;i+)/b0 for no use scanf(%d,&bi);getchar();sub1(b,5);1
18、6 printf(nthe sorted array a is:nn);for(i=1;i=5;i+)printf(a%d=%d,i,ai);printf(n);printf(nthe sorted array b is:nn);for(i=1;i=5;i+)printf(b%d=%d,i,bi);k=i=j=1;while(i=5&j=5)if(aibj)ck+=ai+;/c0 for no use else ck+=bj+;if(ij)/appending the rest ones in array a for(;i=5;i+)ck+=ai;else/appending the rest
19、 ones in array b for(;j=5;j+)ck+=bj;printf(nn);printf(nthe merged array c is:nn);for(k=1;k=10;k+)if(k=6)printf(n);printf(c%d=%d,k,ck);while(!kbhit();17 int sub1(int b,int n)int t,i,j,post;for(i=1;in;i+)post=i;for(j=i+1;jbj)post=j;if(post!=i)j j t=bi;bi=bpost;bpost=t;return 0;7、耶稣有 13 个门徒,其中有一个就是出卖耶稣
20、的叛徒,请用排除法找出这位叛徒:13 人围坐一圈,从第一个开始报号:1,2,3,1,2,3,凡是报到“3”就退出圈子,最后留在圈内的人就是出卖耶稣的叛徒,请找出它原来的序号。/*/approach one#define N 13#include#include struct person 18 int number;/its order in the original circle int nextp;/record its next person;struct person linkN+1;/link0 for no use void main()int i,count,next;/coun
21、t for 12 persons,and/next for the person not out of circle yet clrscr();for(i=1;i=N;i+)linki.number=i;/numbering each person if(i=N)linki.nextp=1;else linki.nextp=i+1;/numbering each next person printf(nThe sequence out of the circle is:n);for(next=1,count=1;countN;count+)/count until 12 persons i=1
22、;while(i!=3)/i counts 1,2,3 do/skip the ones whose numbers are zero next=linknext.nextp;while(linknext.number=0);/end of do i+;19 printf(%3d,linknext.number);linknext.number=0;/indicate out of circle already do/start from the ones whose numbers are not zero next time next=linknext.nextp;while(linkne
23、xt.number=0);printf(nnThe betrayer of them is:);for(i=1;i=N;i+)if(linki.number)printf(%3dn,linki.number);getch();*/approach two using cyclic list#define N 13#define LEN sizeof(struct person)#include#include#include#include/struct person/permit struct placed here/int number;/struct person*next;/;void
24、 main()20 int i,count;struct person/or permit struct placed here also/int number;struct person*next;struct person*head,*p1,*p2;clrscr();head=p2=NULL;for(i=1;inumber=i;if(head=NULL)head=p1;else p2-next=p1;p2=p1;p2-next=head;printf(nthe sequence out of the circle is:n);for(count=1;countnext;21 i+;p2=h
25、ead;printf(%3d,p2-number);p1-next=head=p2-next;free(p2);printf(nThe betrayer of them is:n%3d,head-number);getch();15、按如下图形打印杨辉三角形的前10 行。其特点是两个腰上的数都为 1,其它位置上的每一个数是它上一行相邻两个整数之和。1 11 12 1 13 3 1 14 6 4 1#include#include#define N 10 void main()int i,j,k,aNN;clrscr();for(i=0;iN;i+)/initialize aNN ai0=1;a
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 语言 编程 精选

限制150内