1-1 平均數:輸入3個整數,輸出他們的平均值,保留3位小數
#include <stdio.h> int main() { int a,b,c; scanf("%d%d%d",&a,&b,&c); double d=(double)(a+b+c); printf("%.3lf\n",d/3.0); return 0; }
1-2 溫度:輸入華氏溫度 f ,輸出對應的攝氏度 c,保留3位小數。提示:c=5(f-32)/9
#include <stdio.h> int main() { double f; double c; scanf("%lf",&f); c = 5*(f-32)/9.0; printf("%.3lf\n",c); return 0; }
1-3 連續和:輸入正整數 n,輸出1+2+3+...+n的值。提示:目標是解決問題,而不是練習編程
#include <stdio.h> int main() { int n; scanf("%d",&n); printf("%d\n",(n*(1+n))/2); return 0; }
1-4 正弦和余弦:輸入正整數 n(n<360),輸出n度的正弦余弦數值。提示:用數學函數
#include <stdio.h> #include <math.h> #define PI acos(-1.0) int main() { int n; scanf("%d",&n); printf("%lf\n",sin((PI*n)/180)); printf("%lf\n",cos((PI*n)/180)); return 0; }
1-5 打折:一件衣服 95元,若消費滿300元,可打八五折。輸入購買衣服件數,輸出需要支付的金額(單位元),保留兩位小數
#include <stdio.h> int main() { int n; double a; scanf("%d",&n); a=n*95.0; if(a<300) printf("%.2lf\n",a); else printf("%.2lf\n",a*0.85); return 0; }
1-6 三角形:輸入三角形 3 條邊長度值(均為正整數),判斷是否能為直角三角形的3個邊長。如果可以,則輸出yes,不能輸出no,如果無法構成三角形,則輸出 not a triangle
#include <stdio.h> int main() { int a,b,c,max; scanf("%d%d%d",&a,&b,&c); max = a>b?a:b; max = max>c?max:c; if (max > a+b+c-max) { printf("not a triangle\n"); return 0; } if (a*a == b*b+c*c || b*b == a*a+c*c || c*c == a*a+b*b ) printf("yes\n"); else printf("no\n"); return 0; }
1-7 年份:輸入年份,判斷是否為潤年,如果是,輸出yes,否,輸出no。提示:簡單的除以4是不夠的
#include <stdio.h> int main() { int n; scanf("%d",&n); if(n%4==0 && n%100!=0 || n%400==0) printf("yes\n"); else printf("no\n"); return 0; }
2-1 水仙花數:輸出100~999中的所有水仙花數,若3位數ABC滿足ABC=A3+B3+C3則稱其為水仙花數,例如 153 = 13+53+33,所以153是水仙花數
#include <stdio.h> int main() { int a,b,c; for(int i=100;i<=999;i++) { a=i/100; b=i/10%10; c=i%10; if(i==a*a*a+b*b*b+c*c*c) printf("%d\n",i); } return 0; }
2-2 韓信點兵:相傳韓信才智過人,從不直接點清自己軍隊的人數,只要讓士兵先后三人一排、五人一排、七人一排的變換隊形,而他每次只掠過一眼隊伍尾排的人數就知道總人數了。輸入包括多組數據,每組數據包含3個非負整數a,b,c(a<3,b<5,c<7)輸出總人數的最小值(或報告無解),已知總人數不小於10,不超過100
樣例輸入:
2 1 6
2 1 3
樣例輸出:
Case 1: 41
Case 2: No answer
#include <stdio.h> int main() { int i,a,b,c,count=0; while(scanf("%d%d%d",&a,&b,&c)==3 && a>0 && b>0 && c>0 && a<3 && b<5 && c<7) { for(i=10;i<=100;i++) { if(i%3==a && i%5==b && i%7==c) { printf("Case %d: %d\n",++count,i); break; } } if(i==101) printf("Case %d: No answer\n",++count); } return 0; }
2-3 倒三角形:輸入正整數n<=20,輸出一個n層倒三角形
#include <stdio.h> int main() { int i,j,k,n; scanf("%d",&n); for(i=n;i>0;i--) { for(k=0;k<n-i;k++) printf(" "); for(j=0;j<2*i-1;j++) printf("#"); printf("\n"); } return 0; }
2-4 子序列的和:輸入兩個正整數n<m<106,輸出1/n2+1/(n+1)2+...+1/m2,保留5位小數。輸入包含多組數據,結束標記為 n=m=0
提示:本題有陷阱
樣例輸入:
2 4
65536 655360
0 0
樣例輸出:
Case 1: 0.42361
Case 2: 0.00001
#include <stdio.h> #include <math.h> #include <time.h> //此程序建議用 管道錄入數據來測試 //因為鍵盤輸入的時間也會被計算在程序運行的時間之內 int main() { int n,m,count=0; double sum=0; while (scanf("%d%d",&n,&m)==2 && m && n<m && m<pow(10.0,6.0)) { if(m>46340) m=46340; for (int j=n; j<=m; j++) { sum += 1.0/(j*j); //if(j%10==0)
// printf("Time used=%.5f n=%d 1/n=%.5lf sum=%.5lf\n", (double)clock()/CLOCKS_PER_SEC, j, 1.0/(j*j), sum); } printf("Case %d: %.5lf",++count,sum); } return 0; }
2-5 分數化小數:輸入正整數a,b,c輸出a/b的小數形式,精確到小數點后c位。a,b<=106,c<=100.輸入包含多組數據,結束標記為 a=b=c=0
樣例輸入:
1 6 4
0 0 0
樣例輸出:
Case 1: 0.1667
#include <stdio.h> #include <math.h> int main() { int a,b,c,i=0; while (scanf("%d%d%d",&a,&b,&c)==3 && a && b && b<=pow(10.0,6.0) && c && c<=100) { //printf("%*.*s\n",m,n,ch); //前邊的*定義的是總的寬度,后邊的定義的是輸出的個數 分別對應外面的參數m和n printf("Case %d: %.*lf\n",++i,c,(double)a/b); } return 0; }
2-6 排列:用1,2,3,...,9組成3個三位數 abc,def,ghi 每個數字恰好使用一次,要求abc:def:ghi=1:2:3.按照"abc def ghi"格式輸出所有解,每一行一個解。提示:不必太動腦筋
#include <stdio.h> void count(int num,int *addAll,int *mulAll){ int i = num/100; //百位數 int j = num/10%10; //十位數 int k = num%10; //個位數 (*addAll) += i+j+k; (*mulAll) *= i*j*k; return; } int main() { //至少3位數,百位數最少是1,數字不得重復得最少為123 //同理 最大為987根據比例得 i最大為987/3 for(int i = 123; i <=987/3; i++) { int addAll=0; int mulAll=1; count( i, &addAll, &mulAll); count(i*2, &addAll, &mulAll); count(i*3, &addAll, &mulAll); //1-9的和只能是 9*10/2 //1-9的積只能是 2*3*4*5*6*7*8*9 if(addAll == 9*10/2 && mulAll == 2*3*4*5*6*7*8*9) printf("%d %d %d\n", i, i*2, i*3); } return 0; }
注意:
1. 海量數據的輸入輸出可以通過文件得到緩解
2. 程序的運行時間並不是無法估計的 見本文 2-4
