2.
#define _USE_MATH_DEFINES #include <iostream> #include <iomanip> #include <cmath> using namespace std; int main() { float r, h, l, S, S1, V, V1; cin >> r >> h; l = 2 * M_PI * r; S = M_PI * r * r; S1 = 4 * M_PI * r * r; V = 4 / 3 * M_PI * r * r * r; V1 = S * h; cout << setiosflags(ios::fixed) << setprecision(2); cout << "圓周長 l = " << l << '\t' << "圓面積 S = " << S << endl; cout << "圓球表面積 S1 = " << S1 << '\t' << "圓球體積 V = " << V << endl; cout << "圓柱體積 V1 = " << V1 << endl; return 0; }
結果:
3.
#include <iostream> #include <iomanip> using namespace std; int main() { float F, c; cout << "華氏溫度 F = "; cin >> F; cout << setiosflags(ios::fixed) << setprecision(2); c = (5.0 / 9.0) * (F - 32); cout << "攝氏溫度c = " << c << endl; return 0; }
結果:
4.
c1,c2定義為字符型
#include <iostream> using namespace std; int main() { char c1, c2; cout << "請輸入兩個字符"; c1 = getchar(); c2 = getchar(); cout << "putchar 輸出結果"; putchar(c1); putchar('\t'); putchar(c2); putchar('\n'); cout << "cout輸出結果"; cout << c1 << '\t' << c2 << endl; return 0; }
結果:
c1,c2定義為整形
#include <iostream> using namespace std; int main() { int c1, c2; cout << "請輸入兩個字符"; c1 = getchar(); c2 = getchar(); cout << "putchar 輸出結果"; putchar(c1); putchar('\t'); putchar(c2); putchar('\n'); cout << "cout輸出結果"; cout << c1 << '\t' << c2 << endl; return 0; }
結果:
定義為整形即可輸出ASCII碼。
8.
0
1
1
0
1
9.
#include <iostream> using namespace std; int main() { int compare(int x, int y, int z); int a, b, c, temp, max; cout << "輸入三個整數a b c" << endl; cin >> a >> b >> c; //第一種方法 /*if ((a > b) && (a > c)) cout << "最大的數為a = " << a << endl; else if((b>a)&&(b>c)) cout << "最大的數為b = " << b << endl; else cout << "最大的數為c = " << c << endl;*/ //第二種方法 /*if(a<b) if(b<c) cout << "最大的數為c = " << c << endl; else cout << "最大的數為b = " << b << endl; else if(a<c) cout << "最大的數為c = " << c << endl; else cout << "最大的數為a = " << a << endl;*/ //第三種方法 /*temp = (a > b) ? a : b; max = (temp > c) ? temp : c; cout << "最大的數為 " << max << endl;*/ max = compare(a, b, c); cout << "最大的數為 " << max << endl; return 0; } int compare(int x, int y, int z) { int temp, max; temp = (x > y) ? x : y; max = (temp > z) ? temp : z; return max; }
結果:
10.
#include <iostream> using namespace std; int main() { int x, y; cout << "輸入 x = "; cin >> x; if (x < 1) y = x; else if (x >= 1 && x < 10) y = 2 * x - 1; else y = 3 * x - 11; cout << "輸出 y = " << y << endl; return 0;
結果:
11.
#include <iostream> using namespace std; int main() { int grade; while (1) { cout << "輸入學生成績grade = "; cin >> grade; if (grade < 0 || grade > 100) cout << "成績輸入錯誤,請重新輸入!" << endl; else switch ((int)grade / 10) { case 10: cout << grade << "分成績等級為A" << endl; break; case 9: cout << grade << "分成績等級為A" << endl; break; case 8: cout << grade << "分成績等級為B" << endl; break; case 7: cout << grade << "分成績等級為C" << endl; break; case 6: cout << grade << "分成績等級為D" << endl; break; default: cout << grade << "分成績等級為E" << endl; break; } } return 0; }
結果:
12.
#include <iostream> using namespace std; int main() { long int a; cout << "輸入正整數 a = "; while (1) { cin >> a; if (a > 99999) { cout << "輸入錯誤,請重新輸入!" << endl; continue; } else if (a / 10000 > 0) cout << "a 為5位數 " << a / 10000 + (a % 10000) / 1000 * 10 + (a % 1000) / 100 * 100 + (a % 100) / 10 * 1000 + (a % 10) * 10000 << endl; else if (a / 1000 > 0) cout << "a 為4位數 " << a / 1000 + (a % 1000) / 100 * 10 + (a % 100) / 10 * 100 + (a % 10) * 1000 << endl; else if (a / 100 > 0) cout << "a 為3位數 " << a / 100 + (a % 100) / 10 * 10 + (a % 10) * 100 << endl; else if (a / 10 > 0) cout << "a 為2位數 " << a / 10 + (a % 10) * 10 << endl; else cout << "a 為1位數 " << a << endl; } return 0; }
結果:
13.
#include<iostream> using namespace std; int main() { float i; int c; while (1) { /*cout << "輸入當月利潤為 i = "; cin >> i; if (i <= 10) cout << "應發獎金為 " << i*0.1 << "萬元" << endl; else if (i <= 20 && i >= 10) cout << "應發獎金為 " << 10 * 0.1 + (i - 10)*0.075 << "萬元" << endl; else if (i <= 40 && i >= 20) cout << "應發獎金為 " << 10 * 0.1 + 10 * 0.075 + (i - 20)*0.05 << "萬元" << endl; else if (i <= 60 && i >= 40) cout << "應發獎金為 " << 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + (i - 40)*0.03 << "萬元" << endl; else if (i <= 100 && i >= 60) cout << "應發獎金為 " << 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + (i - 60)*0.015 << "萬元" << endl; else cout << "應發獎金為 " << 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + 40 * 0.015 + (i - 100)*0.01 << "萬元" << endl;*/ cout << "輸入當月利潤為 i = "; cin >> i; c = i / 10; if (c > 10) c = 10; switch (c) { case 0: cout << "應發獎金為 " << i*0.1 << "萬元" << endl; break; case 1: cout << "應發獎金為 " << 10 * 0.1 + (i - 10)*0.075 << "萬元" << endl; break; case 2: case 3: cout << "應發獎金為 " << 10 * 0.1 + 10 * 0.075 + (i - 20)*0.05 << "萬元" << endl; break; case 4: case 5: cout << "應發獎金為 " << 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + (i - 40)*0.03 << "萬元" << endl; break; case 6: case 7: case 8: case 9: cout << "應發獎金為 " << 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + (i - 60)*0.015 << "萬元" << endl; break; case 10: cout << "應發獎金為 " << 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + 40 * 0.015 + (i - 100)*0.01 << "萬元" << endl; break; } } return 0;
結果:
14.
#include<iostream> using namespace std; int main() { void sort(int m, int j, int k, int l); int a, b, c, d; cout << "請輸入四個整數 "; cin >> a >> b >> c >> d; sort(a, b, c, d); return 0; } void sort(int m, int j, int k, int l) { int temp; if (m > j) { temp = m; m = j; j = temp; } if (m > k) { temp = m; m = k; k = temp; } if (m > l) { temp = m; m = l; l = temp; } if (j > k) { temp = j; j = k; k = temp; } if (j > l) { temp = j; j = l; l = temp; } if (k > l) { temp = k; k = l; l = temp; } cout << "從小到大順序輸出為:" << m << '\t' << j << '\t' << k << '\t' << l << '\t' << endl; }
結果:
15.
#include<iostream> using namespace std; int main() { int m, n, temp, p; while (1) { temp = 1; cout << "請輸入兩個正整數:"; cin >> m >> n; p = m * n; while (temp != 0) { if (m > n) { temp = m % n; m = n; n = temp; if (temp == 0) { cout << "m 和 n 的最大公約數為:" << m << endl; cout << "m 和 n 的最小公倍數為:" << p / m << endl; } } else { temp = n % m; n = m; m = temp; if (temp == 0) { cout << "m 和 n 的最大公約數為:" << n << endl; cout << "m 和 n 的最小公倍數為:" << p / n << endl; } } } } return 0; }
結果:
16.
#include <iostream> using namespace std; int main() { char c; int l = 0, s = 0, d = 0, o = 0; cout << "請輸入一行字符" << endl; while (1) { while ((c = getchar()) != '\n') { if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') l++; else if (c == ' ') s++; else if (c >= '0' && c <= '9') d++; else o++; } cout << "這一行字符中" << "英文字母" << l << "個" << endl; cout << "這一行字符中" << "空格" << s << "個" << endl; cout << "這一行字符中" << "數字" << d << "個" << endl; cout << "這一行字符中" << "其他字符" << o << "個" << endl; l = 0, s = 0, d = 0, o = 0; }return 0; }
結果:
17.
#include<iostream> using namespace std; int main() { int a, Sn = 0, n, i = 1, temp = 0; cout << "輸入 a 的值: "; cin >> a; cout << "輸入 n 的值: "; cin >> n; while (i <= n) { temp = temp + a; Sn = Sn + temp; a = a * 10; i++; } cout << "Sn = " << Sn << endl; return 0; }
結果:
18.
#include<iostream> using namespace std; int main() { int n = 1, S = 0, temp = 1; while (n <= 20) { temp = temp*n; S = S + temp; n++; } cout << S << endl; return 0; }
結果:
19.
#include<iostream> using namespace std; int main() { int a, b, i, j, k; for (a = 100; a < 1000; a++) { i = a / 100; j = (a % 100) / 10; k = a % 10; b = i*i*i + j*j*j + k*k*k; if (a == b) cout << a << " 是水仙花數" << endl; } return 0; }
結果:
20.
#include <iostream> using namespace std; int main() { int m, n, i; for (m = 2; m<1000; m++) { n = 0; for (i = 1; i<m; i++) if ((m%i) == 0) n = n + i; if (n == m) { cout << m << ",its factors are:" ; for (i = 1; i<m; i++) if (m%i == 0) cout << i << " "; cout << endl; } } return 0; }
結果:
21.
#include <iostream> using namespace std; int main() { int i, t, n = 20; double a = 2, b = 1, s = 0; for (i = 1; i <= n; i++) { s = s + a / b; t = a; a = a + b; // 將前一項分子與分母之和作為下一項的分子 b = t; // 將前一項的分子作為下一項的分母 } cout << "sum=" << s << endl; return 0; }
結果:
22.
#include <iostream> using namespace std; int main() { int n = 1, i; for (i = 1; i < 10; i++) { n = (n + 1) * 2; } cout << "桃子總數" << n << endl; return 0; }
結果:
23.
#include <iostream> #include <cmath> using namespace std; int main() { float a, x0, x1; cout << "enter a positive number: "; cin >> a; x0 = sqrt(double(a)); x1 = (x0 + a / x0) / 2; do { x0 = x1; x1 = (x0 + a / x0) / 2; } while (fabs(x0 - x1) >= 1e-5); cout << "The square root of " << a << " is " << x1 << endl; return 0; }
結果:
24.
#include <iostream> using namespace std; int main() { int i, k; for (i = 0; i <= 3; i++) // 輸出上面4行*號 { for (k = 0; k <= 2 * i; k++) cout << "*"; // 輸出*號 cout << endl; //輸出完一行*號后換行 } for (i = 0; i <= 2; i++) // 輸出下面3行*號 { for (k = 0; k <= 4 - 2 * i; k++) cout << "*"; // 輸出*號 cout << endl; // 輸出完一行*號后換行 } return 0; }
結果:
25.
#include <iostream> using namespace std; int main() { char i, j, k; /* i是a的對手;j是b的對手;k是c的對手*/ 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') cout << "A--" << i << " B--" << j << " C--" << k << endl; return 0; }
結果:
參考鏈接:http://blog.csdn.net/zhengxiaoyang995926/article/details/79491582