3.1 if語句
else總是和離它最近的if配對。
3.2 switch語句
表達式的值必須是整數類型(int, char...)。
常量表達式必須是整數類型的常量(int, char...),不能包含變量。
3.3 for循環
循環控制變量定義在“表達式1”中,則只在for語句內部起作用,可以不用擔心循環控制變量重名。
循環結構里的“表達式1”和“表達式3”都可以是用逗號連接的若干個表達式。
“表達式1”“表達式2”“表達式3”都可以不寫,但是“;”必須保留。
3.4 whie循環和do while循環
作業
1.奇偶數判斷
Description:給定一個整數,判斷該數是奇數還是偶數。
Input:輸入僅一行,一個大於零的正整數n。
Output:輸出僅一行,如果n是奇數,輸出odd;如果n是偶數,輸出even。
Sample Input:5
Sample Output:odd
1 #include <cstdio>
2
3 int main() 4 { 5 int a; 6 scanf("%d", &a); 7
8 if(a%2 == 0) printf("even\n"); 9 else printf("odd\n"); 10
11 return 0; 12 }
2.求一元二次方程的根
Description:利用公式x1 = (-b + sqrt(b*b-4*a*c))/(2*a), x2 = (-b - sqrt(b*b-4*a*c))/(2*a)求一元二次方程ax2+ bx + c =0的根,其中a不等於0。
Input:輸入一行,包含三個浮點數a, b, c(它們之間以一個空格分開),分別表示方程ax2 + bx + c =0的系數。
Output:
輸出一行,表示方程的解。
若b2 = 4 * a * c,則兩個實根相等,則輸出形式為:x1=x2=...。
若b2 > 4 * a * c,則兩個實根不等,則輸出形式為:x1=...;x2 = ...,其中x1>x2。
若b2 < 4 * a * c,則有兩個虛根,則輸出:x1=實部+虛部i; x2=實部-虛部i,即x1的虛部系數大於等於x2的虛部系數,實部為0時不可省略。實部 = -b / (2*a), 虛部 = sqrt(4*a*c-b*b) / (2*a)
所有實數部分要求精確到小數點后5位,數字、符號之間沒有空格。
Sample Input:(1)1.0 2.0 8.0 (2)1 0 1
Sample Output:(1)x1=-1.00000+2.64575i;x2=-1.00000-2.64575i (2)x1=0.00000+1.00000i;x2=0.00000-1.00000i
1 #include <cstdio>
2 #include <cmath>
3
4 #define EPS 1e-7
5
6 int main() 7 { 8 double a, b, c; 9 scanf("%lf %lf %lf", &a, &b, &c); 10
11 double tmp = b*b-4*a*c; 12 if(tmp<EPS && tmp>-EPS) 13 printf("x1=x2=%.5f\n", (-b)/(2*a)+EPS); 14 else if(tmp > EPS) { 15 double x1 = (-b+sqrt(tmp))/(2*a); 16 double x2 = (-b-sqrt(tmp))/(2*a)+EPS; 17 if(x1-x2 > EPS) 18 printf("x1=%.5f;x2=%.5f\n", x1+EPS, x2+EPS); 19 else
20 printf("x1=%.5f;x2=%.5f\n", x2+EPS, x1+EPS); 21 } 22 else { 23 printf("x1=%.5f+%.5fi;x2=%.5f-%.5fi\n", (-b)/(2*a)+EPS, sqrt(-tmp)/(2*a)+EPS, (-b)/(2*a)+EPS, sqrt(-tmp)/(2*a)+EPS); 24 } 25
26 return 0; 27 }
3.點和正方形的關系
Description:有一個正方形,四個角的坐標(x,y)分別是(1,-1),(1,1),(-1,-1),(-1,1),x是橫軸,y是縱軸。寫一個程序,判斷一個給定的點是否在這個正方形內(包括正方形邊界)。
Input:輸入一行,包括兩個整數x、y,以一個空格分開,表示坐標(x,y)。
Output:輸出一行,如果點在正方形內,則輸出yes,否則輸出no。
Sample Input:1 1
Sample Output:yes
1 #include <cstdio>
2
3 int main() 4 { 5 int x, y; 6 scanf("%d %d", &x, &y); 7
8 if(x>=-1 && x<=1 && y>=-1 && y<=1) 9 printf("yes\n"); 10 else
11 printf("no\n"); 12
13 return 0; 14 }
4.蘋果和蟲子
Description:你買了一箱n個蘋果,很不幸的是買完時箱子里混進了一條蟲子。蟲子每x小時能吃掉一個蘋果,假設蟲子在吃完一個蘋果之前不會吃另一個,那么經過y小時你還有多少個完整的蘋果?
Input:輸入僅一行,包括n,x和y(均為整數)。
Output:輸出也僅一行,剩下的蘋果個數
Sample Input:10 4 9
Sample Output:7
1 #include <cstdio>
2
3 int main() 4 { 5 int n, x, y; 6 scanf("%d %d %d", &n, &x, &y); 7
8 int tmp = y/x; 9 if(y%x != 0) 10 tmp++; 11 if(n >= tmp) 12 printf("%d\n", n-tmp); 13 else
14 printf("0\n"); 15
16 return 0; 17 }
5.簡單計算器
Description:一個最簡單的計算器,支持+, -, *, / 四種運算。僅需考慮輸入輸出為整數的情況,數據和運算結果不會超過int表示的范圍。
Input:輸入只有一行,共有三個參數,其中第1、2個參數為整數,第3個參數為操作符(+,-,*,/)。
Output:
輸出只有一行,一個整數,為運算結果。然而:
1. 如果出現除數為0的情況,則輸出:Divided by zero!
2. 如果出現無效的操作符(即不為 +, -, *, / 之一),則輸出:Invalid operator!
Sample Input:1 2 +
Sample Output:3
1 #include <cstdio>
2
3 int main() 4 { 5 int x, y; 6 char c; 7 scanf("%d %d %c", &x, &y, &c); 8
9 if(c!='+' && c!='-' &&c!='*' &&c!='/') 10 printf("Invalid operator!\n"); 11 else if(c == '+') { 12 printf("%d\n", x+y); 13 } else if(c == '-') { 14 printf("%d\n", x-y); 15 } else if(c == '*') { 16 printf("%d\n", x*y); 17 } else { 18 if(y == 0) 19 printf("Divided by zero!\n"); 20 else
21 printf("%d\n", x/y); 22 } 23
24 return 0; 25 }
6.求整數的和與均值
Description:讀入n(1 <= n <= 10000)個整數,求它們的和與均值。
Input:輸入第一行是一個整數n,表示有n個整數。第2~n+1行每行包含1個整數。每個整數的絕對值均不超過10000。
Output:輸出一行,先輸出和,再輸出平均值(保留到小數點后5位),兩個數間用單個空格分隔。
Sample Input:
4
344
222
343
222
Sample Output:1131 282.75000
1 #include <cstdio>
2
3 int main() 4 { 5 int n, sum = 0; 6 scanf("%d", &n); 7 for(int i=0; i<n; i++) { 8 int tmp; 9 scanf("%d", &tmp); 10 sum += tmp; 11 } 12
13 printf("%d %.5f\n", sum, (double)sum/n); 14
15 return 0; 16 }
7.整數序列的元素最大跨度值
Description:給定一個長度為n的非負整數序列,請計算序列的最大跨度值(最大跨度值 = 最大值減去最小值)。
Input:一共2行,第一行為序列的個數n(1 <= n <= 1000),第二行為序列的n個不超過1000的非負整數,整數之間以一個空格分隔。
Output:輸出一行,表示序列的最大跨度值。
Sample Input:
6
3 0 8 7 5 9
Sample Output:9
1 #include <cstdio>
2
3 int main() 4 { 5 int n, minN = 1001, maxN = -1; 6 scanf("%d", &n); 7 for(int i=0; i<n; i++) { 8 int tmp; 9 scanf("%d", &tmp); 10 if(tmp < minN) 11 minN = tmp; 12 if(tmp > maxN) 13 maxN = tmp; 14 } 15
16 printf("%d\n", maxN-minN); 17
18 return 0; 19 }
8.奧運獎牌計數
Description:2008年北京奧運會,A國的運動員參與了n天的決賽項目(1≤n≤17)。現在要統計一下A國所獲得的金、銀、銅牌數目及總獎牌數。
Input:輸入n+1行,第1行是A國參與決賽項目的天數n,其后n行,每一行是該國某一天獲得的金、銀、銅牌數目,以一個空格分開。
Output:輸出1行,包括4個整數,為A國所獲得的金、銀、銅牌總數及總獎牌數,以一個空格分開。
Sample Input:
3
1 0 3
3 1 0
0 3 0
Sample Output:4 4 3 11
#include <cstdio>
int main() { int n, a, b, c, gold = 0, silver = 0, copper = 0; scanf("%d", &n); for(int i=0; i<n; i++) { scanf("%d %d %d", &a, &b, &c); gold += a; silver += b; copper += c; } printf("%d %d %d %d\n", gold, silver, copper, gold+silver+copper); return 0; }
9.乘方計算
Description:給出一個整數a和一個正整數n,求乘方an。
Input:一行,包含兩個整數a和n。-1000000 <= a <= 1000000,1 <= n <= 10000。
Output:一個整數,即乘方結果。題目保證最終結果的絕對值不超過1000000。
Sample Input:2 3
Sample Output:8
1 #include <cstdio>
2
3 int main() 4 { 5 int a, n; 6 scanf("%d %d", &a, &n); 7 int num = a; 8 for(int i=1; i<n; i++) { 9 num *= a; 10 } 11
12 printf("%d\n", num); 13
14 return 0; 15 }
a.雞尾酒療法
Description:
雞尾酒療法,原指“高效抗逆轉錄病毒治療”(HAART),由美籍華裔科學家何大一於1996年提出,是通過三種或三種以上的抗病毒葯物聯合使用來治療艾 滋病。該療法的應用可以減少單一用葯產生的抗葯性,最大限度地抑制病毒的復制,使被破壞的機體免疫功能部分甚至全部恢復,從而延緩病程進展,延長患者生 命,提高生活質量。人們在雞尾酒療法的基礎上又提出了很多種改進的療法。為了驗證這些治療方法是否在療效上比雞尾酒療法更好,可用通過臨床對照實驗的方式 進行。假設雞尾酒療法的有效率為x,新療法的有效率為y,如果y-x大於5%,則效果更好,如果x-y大於5%,則效果更差,否則稱為效果差不多。下面給 出n組臨床對照實驗,其中第一組采用雞尾酒療法,其他n-1組為各種不同的改進療法。請寫程序判定各種改進療法效果如何。
Input:
第一行為整數n( 1 < n <= 20);
其余n行每行兩個整數,第一個整數是臨床實驗的總病例數(小於等於10000),第二個療效有效的病例數。
這n行數據中,第一行為雞尾酒療法的數據,其余各行為各種改進療法的數據。
Output:有n-1行輸出,分別表示對應改進療法的效果:如果效果更好,輸出better;如果效果更差,輸出worse;否則輸出same
Sample Input:
5
125 99
112 89
145 99
99 97
123 98
Sample Output:
same
worse
better
same
1 #include <cstdio>
2
3 #define eps 1e-6
4
5 int main() 6 { 7 int n; 8 int total, valid; 9 scanf("%d", &n); 10 scanf("%d %d", &total, &valid); 11 double x = (double)valid/total; 12 while(--n) { 13 scanf("%d %d", &total, &valid); 14 double y = (double)valid/total; 15 if(y-x-0.05 > eps) 16 printf("better\n"); 17 else if(x-y-0.05 > eps) 18 printf("worse\n"); 19 else
20 printf("same\n"); 21 } 22
23 return 0; 24 }