1-1 平均數(shù):輸入3個(gè)整數(shù)浓利,輸出他們的平均值挤庇,保留3位小數(shù)
#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 ,輸出對(duì)應(yīng)的攝氏度 c贷掖,保留3位小數(shù)嫡秕。
提示: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 連續(xù)和:輸入正整數(shù) n,輸出1+2+3+...+n的值苹威。
提示:目標(biāo)是解決問題昆咽,而不是練習(xí)編程
#include <stdio.h>
int main(){
int n;
scanf("%d",&n);
printf("%d\n",(n*(1+n))/2);
return 0;
}
1-4 正弦和余弦:輸入正整數(shù) n(n<360),輸出n度的正弦余弦數(shù)值。
提示:用數(shù)學(xué)函數(shù)
#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元牙甫,若消費(fèi)滿300元掷酗,可打八五折。輸入購買衣服件數(shù)窟哺,輸出需要支付的金額(單位元)泻轰,保留兩位小數(shù)
#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 條邊長度值(均為正整數(shù)),判斷是否能為直角三角形的3個(gè)邊長且轨。如果可以糕殉,則輸出yes亩鬼,不能輸出no,如果無法構(gòu)成三角形阿蝶,則輸出 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 水仙花數(shù):輸出100~999中的所有水仙花數(shù)辛蚊,若3位數(shù)ABC滿足ABC=A3+B3+C^3「C 的 3 次方」
則稱其為水仙花數(shù),例如
153 = 13+53+3^3真仲,所以153是水仙花數(shù)
#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 韓信點(diǎn)兵:相傳韓信才智過人袋马,從不直接點(diǎn)清自己軍隊(duì)的人數(shù),只要讓士兵先后三人一排秸应、五人一排虑凛、七人一排的變換隊(duì)形,而他每次只掠過一眼隊(duì)伍尾排的人數(shù)就知道總?cè)藬?shù)了软啼。輸入包括多組數(shù)據(jù)桑谍,每組數(shù)據(jù)包含3個(gè)非負(fù)整數(shù)a,b,c(a<3,b<5,c<7)輸出總?cè)藬?shù)的最小值(或報(bào)告無解),已知總?cè)藬?shù)不小于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 倒三角形:輸入正整數(shù)n<=20,輸出一個(gè)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 子序列的和:輸入兩個(gè)正整數(shù)n<m<106
,輸出1/n2
+1/(n+1)2
+...+1/m2
锣披,保留5位小數(shù)。輸入包含多組數(shù)據(jù)贿条,結(jié)束標(biāo)記為 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>
// 此程序建議用 管道錄入數(shù)據(jù)來測(cè)試
// 因?yàn)殒I盤輸入的時(shí)間也會(huì)被計(jì)算在程序運(yùn)行的時(shí)間之內(nèi)
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 分?jǐn)?shù)化小數(shù):輸入正整數(shù)a,b,c輸出a/b的小數(shù)形式雹仿,精確到小數(shù)點(diǎn)后c位。a,b<=106
,c<=100.輸入包含多組數(shù)據(jù)整以,結(jié)束標(biāo)記為 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);
//前邊的*定義的是總的寬度胧辽,后邊的定義的是輸出的個(gè)數(shù) 分別對(duì)應(yīng)外面的參數(shù)m和n
printf("Case %d: %.*lf\n",++i,c,(double)a/b);
}
return 0;
}
2-6 排列:用1,2,3,...,9組成3個(gè)三位數(shù) abc,def,ghi 每個(gè)數(shù)字恰好使用一次,要求abc:def:ghi=1:2:3.按照"abc def ghi"格式輸出所有解悄蕾,每一行一個(gè)解。提示:不必太動(dòng)腦筋
#include <stdio.h>
void count(int num,int *addAll,int *mulAll){
int i = num/100; //百位數(shù)
int j = num/10%10; //十位數(shù)
int k = num%10; //個(gè)位數(shù)
(*addAll) += i+j+k;
(*mulAll) *= i*j*k;
return;
}
int main(){
//至少3位數(shù)础浮,百位數(shù)最少是1帆调,數(shù)字不得重復(fù)得最少為123
//同理 最大為987根據(jù)比例得 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. 海量數(shù)據(jù)的輸入輸出可以通過文件得到緩解
2. 程序的運(yùn)行時(shí)間并不是無法估計(jì)的 見本文 2-4