《c语言编程题.pdf》由会员分享,可在线阅读,更多相关《c语言编程题.pdf(6页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、疯狂的路哥,安全工程之绝版/写这些程序哥容易么/5.5 有一个函数写一段程序,输入 x,输出 y 值/作者:付路路#includestdio.hvoid main()int x,y;printf(输入 x:n);scanf(%d,&x);if(x 1)y=x;printf(x=%3d,y=x=%dn,x,y);else if(x 10)y=2*x-1;printf(x=%3d,y=2*x-1=%dn,x,y);elsey=3*x-11;printf(x=%3d,y=3x-1=%dn,x,y);/*5.7给定一个不多于 5 位的正整数,要求:求它是几位数;分别打印出每一位数字;按逆序打印出各位数
2、字。例如原数为 321,应输出 123。*/你路哥的#include main()long int num;int indiv,ten,hundred,thousand,ten_thousand,place;/*分别代表个位、十位、百位、千位、万*/printf(请输入一个整数(099999):n);scanf(%ld,&num);if(num9999)place=5;elseif(num999)place=4;elseif(num99)place=3;elseif(num9)place=2;elseplace=1;printf(place=%dn,place);ten_thousand=nu
3、m/10000;疯狂的路哥,安全工程之绝版thousand=num/1000%10;hundred=num/100%10;ten=num%100/10;indiv=num%10;switch(place)case 5:printf(%d,%d,%d,%d,%d,ten_thousand,thousand,hundred,ten,indiv);printf(n 反序数字为;);printf(%d%d%d%d%dn,indiv,ten,hundred,thousand,ten_thousand);break;case 4:printf(%d,%d,%d,%d,thousand,hundred,te
4、n,indiv);printf(n 反序数字为:);printf(%d%d%d%dn,indiv,ten,hundred,thousand);break;case 3:printf(%d,%d,%d,hundred,ten,indiv);printf(n 反序数字为:);printf(%d%d%dn,indiv,ten,hundred);break;case 2:printf(%d,%d,ten,indiv);printf(n 反序数字为:);printf(%d%dn,indiv,ten);break;case 1:printf(%d,indiv);printf(n 反序数字为:);print
5、f(%dn,indiv);break;/*6.6 打印出所有 水仙花数,所谓 水仙花数 是指一个三位数,其各位数字立方和等于该本身。例如:153 是一个水仙花数,因为 153=13+53+33。*/kao 没意思#includestdio.h#includemath.hvoid main()int x=100,a,b,c;while(x=100&x1000)a=0.01*x;b=10*(0.01*x-a);c=x-100*a-10*b;if(x=(pow(a,3)+pow(b,3)+pow(c,3)printf(%5dn,x);x+;疯狂的路哥,安全工程之绝版/输出结果是153 370 371
6、 407/*6.10 猴子吃桃问题。猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个。第二天早上又将剩下的桃子吃掉一半,又多吃一个。以后每天早上都吃了前一天剩下的一半零一个。到第 10 天早上想再吃时,见只剩下一个桃子了。求第一天共摘多少桃子。*/#includestdio.hvoid main()int i=1,sum=0;for(;i=10;sum=2*sum+1,i+);printf(sum=%dn,sum);/*7.6 打印出以下杨辉三角形(要求打印出 10 行)。11112113311464115101051*/#includestdio.hmain()static i
7、nt m,n,k,b1515;b01=1;for(m=1;m15;m+)for(n=1;n=65&str0i=97&str0i=48&str0i=57)l+;else if(str0i=32)m+;else n+;printf(Daxie Xiaoxie Shuzi Kongge Qitan);printf(%5d%7d%5d%6d%4dn,j,k,l,m,n);/*8.3 写一个判断素数的函数,在主函数输入一个整数,输出是否是素数的消息。*/#includestdio.hpsushu(int m)int i=2,t;for(;i=m;i+)if(m%i=0&im)break;if(m-i=0
8、)t=1;else t=0;return m;main()int a,s;printf(enter sushu is n);scanf(%d,&a);s=psushu(a);if(s=1)printf(a is sushun);else printf(s is not sushun);/*8.4 写一个函数,使给定的一个二维数组()转置,即行列互换。*/这是哥写的#includestdio.hvoid main()int a33,b33,i,j;printf(请输入一个一个%d*%d 的矩阵nn,3,3);for(i=0;i=2;i+)疯狂的路哥,安全工程之绝版for(j=0;j=2;j+)s
9、canf(%d,&aij);bji=aij;printf(n);for(i=0;i=2;i+)for(j=0;j=2;j+)printf(%d,bij);printf(n);/*8.4 写一个函数,使给定的一个二维数组()转置,即行列互换。指针实现*/这是哥写的#includestdio.hvoid main()void zhuanhuanhanshu(int*p);int a33;int*p,i,j;printf(输入一个%d*%d 的矩阵nn,3,3);for(i=0;i 3;i+)for(j=0;j 3;j+)scanf(%d,&aij);p=&a00;zhuanhuanhanshu(p);printf(转换之后的矩阵是:n);for(i=0;i 3;i+)for(j=0;j 3;j+)printf(%d,aij);printf(n);void zhuanhuanhanshu(int*p)疯狂的路哥,安全工程之绝版int i,j,temp;for(i=0;i 3;i+)for(j=i;j*p2)t=*p1;*p1=*p2;*p2=t;if(*p1*p3)t=*p1;*p1=*p3;*p3=t;if(*p2*p3)t=*p2;*p2=*p3;*p3=t;printf(%d,%d,%dn,*p1,*p2,*p3);
限制150内