资源描述:
《算法竞赛入门经典第二版习题答案.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、求int的上限与下限#include//运行时间长,请等待.intmain(){intmin,max;FILE*fin,*fout;fin=fopen("minofint.out","wb");fout=fopen("maxofint.out","wb");for(min=-1;min<0;){min--;}fprintf(fin,"%d",min+1);for(max=1;max>0;){max++;}fprintf(fout,"%d",max-1);fclose(fin);fclose(fou
2、t);return0;}1-1#includeintmain(){inta,b,c;scanf("%d%d%d",&a,&b,&c);doubleaverage;average=(a+b+c)/3.0;//一定要将int型转为浮点型printf("Average=%.3lf",average);system("pause");return0;}1-2#includeintmain(){doublef,c;printf("请输入华氏温度f");scanf("%lf",&f);c=(f-
3、32)*5/9;printf("摄氏温度c=%.3lf",c);system("pause");return0;}1-3#includeintmain(){intn;scanf("%d",&n);printf("%d",(1+n)*n/2);system("pause");return0;}1-4#include#includeintmain(){constdoublepi=4.0*atan(1.0);intn;scanf("%d",&n);while(n>=36
4、0){printf("请输入小于360°的角");scanf("%d",&n);}printf("正弦:%lf余弦:%lf",sin(n*pi/180),cos(n*pi/180));system("pause");return0;}1-5#include#includeintmain(){doublex1,y1,x2,y2;printf("请输入点A的坐标");scanf("%lf%lf",&x1,&y1);printf("请输入点B的坐标");scanf("%lf%lf
5、",&x2,&y2);doubled;d=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));printf("%.3lf",d);system("pause");return0;}1-6#includeintmain(){inta;scanf("%d",&a);if(a%2==0)printf("该数是偶数");elseprintf("该数非偶数");system("pause");return0;}1-7#includeintmain(){constinta
6、=95;intn;printf("你要买多少件衣服");scanf("%d",&n);if(a*n>=300)printf("需要%.2lf元",a*n*0.85);elseprintf("需要%.2lf元",(double)a*n);//由于输出是小数%.2lf,故一定要将int型转化为浮点型system("pause");return0;}1-8#include#includeintmain(){doublea;scanf("%lf",&a);if(a>0)printf(
7、"%.2lf",a);elseprintf("%.2lf",-a);system("pause");return0;}1-9(方法一)#includeintmain(){inta,b,c,max,min,middle;scanf("%d%d%d",&a,&b,&c);while(a<0
8、
9、b<0
10、
11、c<0){printf("三边必须都是大于零的正整数");scanf("%d%d%d",&a,&b,&c);}min=a;if(a>b)min=b;if(a>c)min=c;max=a;if(a
12、;if(amax)printf("yes");elseprintf("no");system("pause");return0;}1-9(方法二)#includeintmain(){inta,b,c