您好,欢迎访问三七文档
已知三角形的三条边长a、b和c,求三角形的面积。#includestdio.h#includemath.hvoidmain(){Inta=3,b=4,c=5;floats;floatl;l=(a+b+c)/2.0;s=sqrt(l*(l-a)*(l-b)*(l-c));printf(s=%f\n,s);}求解鸡兔同笼问题。已知笼子中鸡和兔的头数总共为h,脚数总共为f。问鸡和兔各有多少只?#includestdio.hvoidmain(){inth,f;intx;inty;scanf(%d%d,&h,&f);x=(f-2*h)/2;y=(4*h-f)/2;printf(x=%d,y=%d\n,x,y);}编写程序输入圆半径r和高h,求圆周长、圆面积、圆球表面积、圆球体积和圆柱体积#includestdio.hvoidmain(){intr,h;floatl,s1,s2,v1,v2;scanf(%d%d,&r,&h);l=3.14*2*r;s1=3.14*r*r;s2=4*3.14*r*r;v1=3.14*r*r*h;v2=4*3.14*r*r*r/3;printf(l=%f,s1=%f,s2=%f,v1=%f,v2=%f\n,l,s1,s2,v1,v2);}输入华氏温度值F,求出摄氏温度C,其公式为:#includestdio.hvoidmain(){intF;floatc;scanf(%d,&F);c=5.0/9*(F-32);printf(c=%f,c);}我国现有人口13亿,如果每年增长率r=0.8%,求多少年后我国人口超过26亿(根据公式,推导出年数n可以用公式计算)#includestdio.h#includemath.hvoidmain(){floatr=0.008;floatn;n=log10(2)/log10(1+r);printf(n=%f\n,n);}输入平面坐标系中两个点的坐标和,计算两点之间的距离#includestdio.h#includemath.hvoidmain(){intx1,y1,x2,y2;floatd;scanf(%d%d%d%d,&x1,&y1,&x2,&y2);d=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));printf(d=%f\n,d);}输入a、b值,如果ab,那么交换a和b,使得a≤b#includestdio.hvoidmain(){inta,b,t;scanf(%d%d,&a,&b);if(ab){t=a;a=b;b=t;}printf(a=%d,b=%d\n,a,b);}输入变量a、b和c,将它们按照从小到大的顺序排序后输出#includestdio.hvoidmain(){inta,b,c,t;scanf(%d%d%d,&a,&b,&c);if(ab){t=a;a=b;b=t;}if(ac){t=a;a=c;c=t;}if(bc){t=b;b=c;c=t;}printf(a=%d,b=%d,c=%d\n,a,b,c);}输入a、b值,输出其中较大的数#includestdio.hvoidmain(){inta,b,t;scanf(%d%d,&a,&b);if(ab)printf(max=%d\n,a);elseprintf(max=%d\n,b);}输入x,求函数的值#includestdio.hvoidmain(){intx,y;scanf(%d,&x);if(x1)y=x;if(x=1&&x10)y=2*x-1;if(x=10)y=x*x+2*x+2;printf(y=%d\n,y);}输入学生课程成绩mark,按照方法给出评分等级#includestdio.hvoidmain(){intmark;chargrade;scanf(%d,&mark);if(mark=90)grade='A';if(mark=80&&mark90)grade='B';if(mark=70&&mark80)grade='C';if(mark=60&&mark70)grade='D';if(mark60)grade='E';printf(grade=%c\n,grade);}给定整数,判定该数能否同时被6、9和14整除#includestdio.hvoidmain(){inta;scanf(%d,&a);if(a%6==9&&a%9==0&&a%14==0)printf(yes\n);elseprintf(no\n);}输入一个年号,判断该年号是否闰年。#includestdio.hvoidmain(){intyear;scanf(%d,&year);if((year%4==0&&year%100!=0)||(year%400==0))printf(yes\n);elseprintf(no\n);}水仙花数是指一个三位整数,该数三个数位的立方和等于该数本身,输入一个3位整数,判断该数是否水仙花数#includestdio.hvoidmain(){intm,a,b,c;scanf(%d,&m);a=m/100;b=(m%100)/10;c=m%10;if(a*a*a+b*b*b+c*c*c==m)printf(yes\n);elseprintf(no\n);}14.某服装店经营套装,也单件出售,针对单笔交易的促销政策为:(1)不少于50套,每套80元;(2)不足50套,每套90元;(3)只买上衣每件60元;(4)只买裤子每条45元;编写计算器,分别输入一笔交易中上衣和裤子数,计算应收款。#includestdio.hvoidmain(){intup,down,all,suit;scanf(%d%d,&up,&down);if(up==0)/*只买裤子*/all=down*45;if(down==0)/*只买上衣*/all=up*60;if(updown){if(down=50)all=down*80+(up-down)*60;elseall=down*90+(up-down)*60;}else{if(up=50)all=up*80+(down-up)*60;elseall=up*90+(down-up)*60;}printf(all=%d\n,all);}求s=10!,即求10的阶乘#includestdio.hmain(){longs=1;inti;for(i=1;i=10;i++)s=s*i;printf(s=%ld\n,s);}打印1-100中,所有能被4整除的整数#includestdio.hmain(){inti;for(i=1;i=100;i++)if(i%4==0)printf(%d\n,i);}计算分数序列的和:,直到最后项小于0.00001#includestdio.hmain(){inti=1;floats=0;while(1.0/i=0.00001){s=s+1.0/i;i++;}printf(s=%f\n,s);}利用公式,求圆周率,要求最后一项绝对值小于#includestdio.h#includemath.hmain(){floatpi=0;inti=1;intt=1;while(1.0/(2*i-1)=1e-6){pi=pi+t*1.0/(2*i-1);t=-t;i++;}pi=4*pi;printf(pi=%f\n,pi);}素数是这样的整数,它只能被1和它自己整除。输入一个整数m,判断该数是否素数#includestdio.hmain(){intm;inti;scanf(%d,&m);for(i=2;im;i++){if(m%i==0)break;}if(i==m)printf(%dissushu\n,m);elseprintf(%disnotsushu\n,m);}找出1到1000之间的所有素数#includestdio.hmain(){intm;inti;for(m=1;m=1000;m++){for(i=2;im;i++)if(m%i==0)break;if(i==m)printf(%d\n,m);}}求两个整数m和n的最大公约数和最小公倍数#includestdio.hmain(){intm,n,t;inti;intmax,min;scanf(%d%d,&m,&n);if(mn){t=m;m=n;n=t;}for(i=1;i=m;i++){if(m%i==0&&n%i==0)max=i;}for(i=n;i=m*n;i++){if(i%m==0&&i%n==0){min=i;break;}}printf(max=%d\nmin=%d\n,max,min);}百钱买百鸡问题。假定公鸡每只2元,母鸡每只3元,小鸡每只0.5元。现有100元,要求买100只鸡,编程求出公鸡只数x、母鸡只数y和小鸡只数z#includestdio.hmain(){intx,y,z;for(x=0;x=50;x++)for(y=0;y=33;y++)for(z=0;z=100;z=z+2)if(2*x+3*y+z/2==100&&x+y+z==100)printf(x=%d,y=%d,z=%d\n,x,y,z);}求#includestdio.hmain(){intx;longy=0;for(x=1;x=20;x++)y=y+2*x*x+3*x+1;printf(y=%ld\n,y);}我国人口为13亿,按照人口每年增加0.8%计算,多少年后我国的人口超过26亿#includestdio.hmain(){floatpeople=13;floatincrease=0.008;intyear=0;while(people=26){people=people*(1+increase);year++;}printf(year=%d\n,year);}求分数序列,前20项之和#includestdio.hmain(){inti;floata=2;floats=0;for(i=0;i20;i++){s=s+a;a=1+1/a;}printf(s=%f\n,s);}求,直到最后项小于#includestdio.hmain(){inti=1;intm=1;floats=0;while(1.0/i=1e-6){s=s+1.0/i;i=i+m;m++;}printf(s=%f\n,s);}求,直到n=1000。#includestdio.hmain(){intn=1;floatpi=2;while(n1000){pi=pi*(2*n*2*n)/((2*n-1)*(2*n+1));n++;}printf(pi=%f\n,pi);}编程计算1000内的所有完数。完数是指一个数恰好等于其因子之和,例如:6=1+2+3(提示:先设计求m所有因子的算法;再求因子之和,并判断m是否完数;最后求所有完数)#includestdio.hvoidmain(){inti,j,s;for(i=1;i=1000;i++){s=0;for(j=1;ji;j++)if(i%j==0)s=s+j;if(i==s)printf(%d\n,i);}}搬砖问题:36块砖36人搬,男搬4块,女搬3块,2个小儿抬1块,要求1次搬完。问需男、女和小儿各多少人。#includestdio.hvoidmain(){intx,y,z;for(x=0;x=9;x++)for(y=0;y=12;y++)for(z=0;z=36;z=z+2)if((x+y+z)==36&&(4*x+3*y+z/2)==36)printf(x=%d,y=%d,z=%d\n,x,y,z);}输入10个数,并反序输出。#includestdio.hvoidmain(){inti,a[10];for
本文标题:复习题及答案
链接地址:https://www.777doc.com/doc-2543254 .html