第2季:循环
第1题
Title 完全数
Time
Limit
1000MS
Memor
y Limit
10000KB
Descrip tion 一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如,6的因子为1、2、3,而6=1+2+3,因此6是"完数"。请编写程序,找出1000之内的所有完数。
Input
Output 每行按格式输出其因子:6=1+2+3 Sample
Input
Sample Output 6=1+2+3
28=1+2+4+7+14
496=1+2+4+8+16+31+62+124+248
1.完全数
#include
{int m,i,j,s;
for(m=6;m<1000;m++)
{s=1;
for(i=2;i if(m%i==0) s=s+i; if(m-s==0) {printf("%5d its fastors are 1 ",m);for(j=2;j } } 第2题 Title 迭代求根 Time 1000MS Limit 10000KB Memor y Limit Descrip tion Input 输入a为实型 Output 输出根为实型,保留五位小数。Sample 2 Sample 1.41421 Output 2.迭代求根 #include #include int main() { float x0,x1,a; scanf("%f",&a); x1=a/2; do { x0=x1;x1=(x0+a/x0)/2; } while(fabs(x0-x1)>=0.00001); printf("%.5f\n",x1); return 0; } 第3题 Title 二分求根Time 1000MS Memor y Limit 10000KB Descrip tion 请编写程序,用二分法求下面方程在(-10,10)之间的根: Input 输入区间数据为实型、用空格隔开输出均。 Output 输出根为实型,保留两位小数。Sample Input -10 10 Sample Output 2.00 3.二分求根 #include #include int main() { float x0,x1,x2,f0,f1,f2; printf("请输入x1,x2的值:"); scanf("%f%f",&x1,&x2); f1=2*x1*x1*x1-4*x1*x1+3*x1-6; f2=2*x2*x2*x2-4*x2*x2+3*x2-6; do { x0=(x1+x2)/2; f0=2*x0*x0*x0-4*x0*x0+3*x0-6; if(f0*f1<0) {x2=x0;f2=f0;} else { x1=x0; f1=f0; } } while(fabs(f0)>=1e-5); printf("方程的根为:%.2f\n",x0); return 0; } 第4题 Title VOL大学乒乓球比赛Time 1000MS Limit 10000KB Memor y Limit Descrip tion VOL大学有两个乒乓球队进行比赛,各出3人。甲队为A、B、C三人,乙队为X、Y、Z三人,已抽签决定比赛名单,有人向队员打听比赛的名单,A说他不和X比,C说他不和X、Z比,请编写程序找出3对赛手的名单。 Input Output 输出赛手名单,一行一对。Sample Input Sample Output A=Z B=X C=Y 4.VOL大学乒乓球比赛#include int main() { char i,j,k; for(i='X';i<='Z';i++) for(j='X';j<='Z';j++) { if(i!=j) for(k='X';k<='Z';k++) { if(i!=k&&j!=k) { if(i!='X'&&k!='X'&&k!='Z') printf("A=%c\nB=%c\nC=%c\n",i,j,k); } } } return 0; } 第5题 Title 两个整数之间所有的素数Time Limit 1000MS Memor y Limit 10000KB Descrip tion 编写程序,求任意两个整数之间所有的素数。 Input 输入两个整数,用空格间隔。注意输入的两个整数谁大谁小是任意的。 Output 输出数据占一行,用空格间隔。Sample 100 130 Input Sample 101 103 107 109 113 127 Output 5.两个整数之间所有的素数 #include #include int main() { int m,n,a,i,t; scanf("%d%d",&m,&n); if(m>n) {t=m; m=n; n=t;} for(a=m;a<=n;a++) { for(i=2;i<=sqrt(a);i++) if(a%i==0) break; if(i>sqrt(a)) printf("%d ",a); } printf("\n"); return 0; } 第6题 Title 奇特的分数数列 1000MS Time Limit 10000KB Memor y Limit Descrip 有一个分数数列: tion 求出这个数列前20项之和。Input Output 输出保留六位小数。Sample Input 32.660261 Sample Output 6.奇特的分数数列 #include int main() { int i,x,y,a; float sum=0; x=2; y=1; for(i=1;i<=20;i++) { sum+=(float)(x)/y; // printf("%d/%d\n",x,y); a=x; x=x+y; y=a; } printf("%.6f\n",sum); return 0; } 第7题 Title 你会存钱吗?Time Limit 1000MS Memor y Limit 10000KB Descrip tion 假设银行整存整取存款不同期限的月息利率分别为: 利息=本金*月息利率*12*存款年限。 现在某人手中有2000元钱,请通过计算 选择一种存钱方案,使得钱存入银行20 年后得到的利息最多(假定银行对超过存 款期限的那一部分时间不付利息)。Input Output 输出第1行为8年、5年、3年、2年、1年的存款方案(各利率存款次数),用空 格隔开。输出第2行为最大收益。Sample Input Sample Output 0 4 0 0 0 8841.01 7.你会存钱吗? #include #include int main() { int i8,i5,i3,i2,i1,n8,n5,n3,n2,n1; float max=0,term; for(i8=0;i8<3;i8++) /*穷举所有可能的存款方式*/ for(i5=0;i5<=(20-8*i8)/5;i5++) for(i3=0;i3<=(20-8*i8-5*i5)/3;i3++) for(i2=0;i2<=(20-8*i8-5*i5-3*i3)/2;i2++) { i1=20-8*i8-5*i5-3*i3-2*i2; term=2000.0*pow((double)(1+0.0063*12), (double)i1)*pow((double)(1+2*0.0063*12), (double)i2)*pow((double)(1+3*0.0069*12),(double)i3)*pow((double)(1+5 *0.0075*12),(double)i5)*pow((double)(1+8*0.0084*12),(double)i8);/*计算到期时的本利合计*/ /////这句话不怎么理解请问下这句该怎么理解 if(term>max){ max=term; n1=i1; n2=i2; n3=i3; n5=i5; n8=i8; } } printf("%d %d %d %d %d\n",n8,n5,n3,n2,n1); printf("%.2f\n",max);/*输出存款方式*/ return 0; } 第8题 Title 整数位数 Time Limit 1000MS Memor y Limit 10000KB Descrip tion 编写程序输入一个数,输出其整数部分的位数(例如输入123.4则输出3,输入- 0.6则输出0)。Input 输入为实型。Output 输出为整型。 123.4 Sample Input Sample 3 Output 8.整数位数 #include int main() { int x,n=0; float c=2.0; scanf("%f",&c); x=int(c); while(x!=0) {n++; x=x/10; } printf("%d\n",n); return 0; } 第9题 Title 1000MS Time Limit 10000KB Memor y Limit Descrip 编写程序利用下面公式计算π的近似值:tion Input Output 输出π为实型,保留六位小数。Sample Input 3.141591 Sample Output 9.计算π的近似值 #include #include int main() { int a=1,f=1; double p=0; while(fabs(1.0/a)>=1e-6) { p=p+1.0/a*f; a=a+2; f=-f; } printf("pi=%.6lf\n",4*p); return 0; } 第10题 Title 级数和Time Limit 1000MS Memor y Limit 10000KB Descrip tion 编写程序求下面级数前n项的和,其中n 从键盘上输入。 Input 输入n为整型。 Output 输出和为实型,保留六位小数。Sample Input 5 Sample 0.338462 Output 10.级数和 #include #include int main() { int i,n; double s=0; scanf("%d",&n); for(i=1;i<=n;i++) s=s+pow(-1,i-1)*pow(2,i)/(pow(2,i)+pow(-1,i))/(pow(2,i+1)+pow(-1,i+1)) ; printf("s=%.6lf\n",s); return 0; } 第11题 Title 区间内素数 1000MS Time Limit 10000KB Memor y Limit Descrip tion 编写程序计算500~800区间内素数的个数cnt,并按所求素数的值从大到小的顺序,再计算其间隔减、加之和,即第1个素数-第2个素数+第3个素数-第4个素数+第5个素数……的值sum。 Input Output 一行内输出cnt和sum,用空格隔开。Sample Input Sample Output 44 130 11.区间内素数 #include #include int main() { int i,n; double s=0; scanf("%d",&n); for(i=1;i<=n;i++) s=s+pow(-1,i-1)*pow(2,i)/(pow(2,i)+pow(-1,i))/(pow(2,i+1)+pow(-1,i+1)) ; printf("s=%.6lf\n",s); return 0; } 第12题 Title 自然数立方的乐趣Time Limit 1000MS Memor y Limit 10000KB Descrip tion 编写程序验证下列结论:任何一个自然数n的立方都等于n个连续奇数之和。例如: 要求程序对每个输入的自然数计算并输出相应的连续奇数。 Input 输入自然数n为整数。 Output 输出n个连续奇数之和,格式如Sample Output显示。 Sample Input 5 Sample Output 5*5*5=125=21+23+25+27+29 12.自然数立方的乐趣 #include int main() { int n,i,j,s,k; scanf("%d",&n); for(i=1;i<=n*n*n;i=i+2) { s=0;k=0; for(j=i;j<=n*n*n;j=j+2) {s=s+j; k++; if(s==n*n*n) break; } if(s==n*n*n && k==n) break; } printf("%d*%d*%d=%d=%d",n,n,n,n*n*n,i); for(i=i+2;i<=j;i=i+2) printf("+%d",i); return 0; } 第13题 Title 最次方数 Time Limit 1000MS Memor y Limit 10000KB Descrip tion 编写程序求一个整数的任意次方的最后三位数。即: Input 输入均为整型,首先为x,其后为a,用空格隔开。 Output 输出为整型。 Sample Input 13 13 Sample Output 253 13.最次方数 #include int main() { int x,a,i,t; scanf("%d%d",&x,&a); t=1;