上機考試題集_第1頁
上機考試題集_第2頁
上機考試題集_第3頁
上機考試題集_第4頁
上機考試題集_第5頁
已閱讀5頁,還剩26頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、1.利用異或運算對輸入的文本進行加密解密輸出,用戶輸入一個文本(字符串,設不超過20個字符),然后輸入作為密鑰的字符,程序輸出加密及解密的字符串。#include <iostream>using namespace std;int main() char ch21, key;int i;cout << "請輸入字符串:"cin >> ch;cout << "請輸入作為密鑰的字符:"cin >> key;cout << "加密中" << endl;for

2、 (i = 0; chi != '0' i+) chi = chi key;cout << "加密后的字符串為:" << ch << endl;cout << "解密中" << endl;for (i = 0; chi != '0' i+) chi = chi key;cout << "解密后的字符串為" << ch;return 0;2.編寫一個程序,用戶輸入年份及月份兩個數據,程序輸出該月份的天數。(提示:對2月要考

3、慮是否閏年,閏年年份要么能被4整除且不能被100整除,要么能被400整除,除次之外都不是閏年)。#include <iostream>using namespace std;bool LeapYear(int y) if (y % 4 = 0 && y % 100 != 0) | y % 400 = 0) return true;else return false;int main() int y, m;cout << "請輸入年和月:"cin >> y >> m;if (m = 2) if (LeapYear

4、(y) cout << y << "年" << m << "月有29天" << endl;else cout << y << "年" << m << "月有28天" << endl;else if (m = 1 | m = 3 | m = 5 | m = 7 | m = 8 | m = 10 | m = 12) cout << y << "年" <

5、< m << "月有31天" << endl;else if (m<=12&&m>=1) cout << y << "年" << m << "月有30天" << endl;else cout << "不存在" << m << "月" << endl;return 0;3.某大橋按不同型號征收車輛過橋費:自行車免費,摩托車2元,小汽車

6、5元,大客車與貨車8元,貨柜車12元。編寫一個程序,按車輛的不同型號計算通過該大橋應征的過橋費。(提示:可以用整數對不同型號的車輛進行編碼)#include <iostream>using namespace std;int main() unsigned a, b;one:cout << "請選擇汽車的類型:" << endl;cout << " 1.自行車n 2.摩托車n 3.小汽車n"cout << " 4.大客車n 5.貨車n 6.貨柜車n"cin >>

7、a;switch (a) case 1:b = 0; break;case 2:b = 2; break;case 3:b = 5; break;case 4:case 5:b = 8; break;case 6:b = 12; break;default:cout << "請重新輸入nn" goto one;cout << "該車通過該大橋應征的過橋費為" << b << "元" << endl;return 0;4.輸入一位同學的考試成績,若是90100分,輸出“Exce

8、llent”,8089輸出“Very good”,7079輸出“Good”,6069輸出“Pass”,60分以下輸出“No Pass”。#include <iostream>using namespace std;int main() unsigned a;cin >> a;if (a >= 90 && a <= 100) cout << "Excellent" << endl;else if (a >= 80) cout << "Good"else if (a

9、 >= 70) cout << "Very good"else if (a >= 60) cout << "Good"else cout << "No Pass"return 0;5.旅行社的訂票量小于10張時,航空公司給予10%的折扣;訂票量大于或等于10張且小于20張時,航空公司給予15%的折扣;訂票量大于或等于20張且小于30張時,航空公司給予30%的折扣;訂票量大于或等于30張時,航空公司給予最高的45%的折扣。編程輸入訂票張數及單張機票票價,程序輸出折扣率及應付款額。#incl

10、ude <iostream>using namespace std;int main() unsigned n, p;double off;cout << "請輸入訂票張數及單張機票票價:"cin >> n >> p;if (n < 10) off = 0.9;else if (n >= 10 && n < 20) off = 0.85;else if (n >= 20 && n < 30) off = 0.7;else off = 0.55;cout <&l

11、t; "折扣率為" << 100 * (1 - off) << "%,應付款額為" << off * n * p << endl;return 0;6.用戶輸入一個整數流(輸入1000表示數據輸入結束),如4 1 13 0 6 -5 1 -1 7 -12 19 0 100 編寫程序統計輸入流中-1、0和+1的個數。方法一:采用scanf函數#include <iostream>using namespace std;/#define scanf scanf_s/如果使用VS2017,請刪除上一行

12、的注釋int main() int a, b = 0, c = 0, d = 0;while (scanf("%d", &a), a != 1000) if (a = -1) b+;else if (a = 0) c+;else if (a = 1) d+;cout << "輸入流中-1的個數為" << b<< ",0的個數為" << c << ",1的個數為" << d << endl;return 0;方法二:采用輸入流

13、#include <iostream>using namespace std;int main() int a, b = 0, c = 0, d = 0;while (1) cin >> a;if (a = 1000) break;if (a = -1) b+;else if (a = 0) c+;else if (a = 1) d+;cout << "輸入流中-1的個數為" << b<< ",0的個數為" << c << ",1的個數為" <&

14、lt; d << endl;return 0;7.編寫一個程序,求一個自然數中含有多少個2的因子。如,6含1個2的因子,8含3個2的因子,11不含2的因子。(提示,程序應檢查用戶輸入的合法性)。#include <iostream>using namespace std;int main() int n, a, i = 0;one:cin >> a;n = a;if (a < 0) cout << "錯誤,請重新輸入:"goto one;else while (a % 2 = 0) i+;a = a / 2;cout &

15、lt;< n << "共有" << i << "個2的因子" << endl;return 0;8.編寫一個程序解決愛因斯坦臺階問題:有人走以臺階,若以每步走2級則最后剩1級;若每步走3級則最后剩2級;若以每步走4級則最后剩3級;若以每步走5級則最后剩4級;若以每步走6級則最后剩5級;若以每步走7級則最后剛好不剩。問臺階共有幾級?方法一:采用while語句#include <iostream>using namespace std;int main() int i = 7;while (!

16、(i % 2 = 1 && i % 3 = 2 && i % 4 = 3 && i % 5 = 4 && i % 6 = 5 && i % 7 = 0) i+;cout << "臺階最少有" << i << "級" << endl;return 0;方法二:采用for語句#include <iostream>using namespace std;int main() int i;for (i = 7; i+) if

17、 (i % 2 = 1 && i % 3 = 2 && i % 4 = 3 && i % 5 = 4 && i % 6 = 5 && i % 7 = 0) break;cout << "臺階最少有" << i << "級" << endl;return 0;9.公雞5元1只,母雞3元1只,小雞1元3只,花了100元錢買100只雞,問公雞、母雞、小雞各多少只?#include <iostream>using names

18、pace std;int main() int gj, mj;for (gj = 0; gj <= 20; gj+) for (mj = 0; mj <= 33; mj+) if (100 - mj - gj) % 3 = 0 && 3 * mj + 5 * gj + (100 - mj - gj) / 3 = 100)cout << "公雞的數量是" << gj<< ",母雞的數量是" << mj<< ",小雞的數量是" << 100

19、 - mj - gj << endl;return 0;10.編程實現解決下述問題的算法:一位顧客在購物時,如果買4個蘋果剩下4角錢如果買5個蘋果則缺5角錢,請問,該顧客帶了多少錢?多少錢可以買一個蘋果?#include <iostream>using namespace std;int main() int i;for (i = 1; i+) if (4 * i + 4 = 5 * i - 5) cout << "一個蘋果" << 4 * i + 4 << "角,買了" << i

20、<< "個蘋果。" << endl;break;return 0;11.編寫程序計算100之內可以被13整除的自然數之和。#include <iostream>using namespace std;int main() int i;for (i = 0; i < 100; i+) if (i % 13 = 0) cout << i << ' 'return 0;12.鍵盤輸入m和n(10<m<n32000),求出mn間所有素數且按每行8個數形式輸出。#include <io

21、stream>using namespace std;bool Is_Not_Prime_Number(int a) int b = 0;if (a = 0) return 1;if (a = 1) return 1;else for (int i = 2; i < a;) if (a%i = 0) a = a / i; b+; else i+;return b;int main() int m, n, i, j = 0;cout << "請輸入m,n的值:"cin >> m >> n;for (i = m; i <=

22、n; i+) if (!Is_Not_Prime_Number(i) cout << i << ' 'j+;if (j % 8 = 0) cout << endl;return 0;13.編寫程序打印乘法口訣表。#include <iostream>using namespace std;int main() int i, j;for (i = 1; i < 10; i+) for (j = 1; j <= i; j+) cout << i << '*' << j &

23、lt;< '=' << i * j << ' 'cout << endl;return 0;14.編程實現求解最大公約數的歐幾里德算法,用戶輸入兩個任意正整數,程序輸出他們的最大公約數。算法如下:步驟1:如果p < q,則交換p和q。步驟2:令r是p / q 的余數。步驟3:如果r = 0,則令g = q并終止;否則令p = q, q = r并轉向步驟2 #include <iostream>using namespace std;int fun(int p, int q) int t;if (p &l

24、t; q) t = q;q = p;p = t;while (t = p % q, t != 0) p = q;q = t;return q;int main() int p, q;cout << "請輸入兩個整數:"cin >> p >> q;cout << fun(p, q) << endl;return 0;15.求不超過正整數n的2的最大冪值,如輸入17,程序應輸出4(24=16<17)。#include <iostream>#include <cmath>using name

25、space std;int main() int n, i;cout << "請輸入整數:"cin >> n;for (i = 0; i+) if (pow(2, i) > n) cout << i - 1;break;return 0;16.有關專家十分關注珠江漁業資源的問題。目前珠江中大約有8000萬條魚,平均每年以3.5%的速度減少。請編寫一個程序,計算在多少年之后魚的數目下降到目前的一半?多少年后下降到目前的十分之一?(提示注意整數類型的取值范圍)。#include <iostream>#include <

26、cmath>using namespace std;int main() const double n = 8000, r = 0.035;double t = n;int i = 0;while (t >= n / 2) t = t * (1 - r);i+;cout << i << "年后魚的數目下降到目前的一半,"while (t >= n / 10) t = t * (1 - r);i+;cout << i << "年后下降到目前的十分之一" << endl;retur

27、n 0;17.編程求解一元二次方程ax2+bx+c=0的根。要求:設計完備的測試數據集,考慮a, b, c各種取值對根的影響。#include <iostream>#include <cmath>using namespace std;double Delta(double a, double b, double c);void Soq1(double a, double b, double c);void Soq2(double a, double b, double c);void Soq3();void Soq4(double b, double c);int m

28、ain() double a, b, c;cout << "請輸入a,b,c的值:"cin >> a >> b >> c;if (a = 0) if (b = 0) if (c = 0) cout << "該方程有無窮多解。" << endl;else Soq3();else Soq4(b, c);else if (Delta(a, b, c) > 0) Soq1(a, b, c);else if (Delta(a, b, c) = 0) Soq2(a, b, c);else

29、Soq3();return 0;double Delta(double a, double b, double c) return b * b - 4 * a*c;void Soq1(double a, double b, double c) double Soq1, Soq2;Soq1 = (-b + sqrt(Delta(a, b, c) / (2 * a);Soq2 = (-b - sqrt(Delta(a, b, c) / (2 * a);cout << "該方程存在兩個不等的解,其值分別為" << Soq1 << "和

30、" << Soq2 << endl;void Soq2(double a, double b, double c) double Soq;Soq = -b / (2 * a);cout << "該方程存在兩個相等的解,其值為" << Soq << endl;void Soq3() cout << "該方程無解。" << endl;void Soq4(double b, double c) double Soq;Soq = -c / b;cout <<

31、 "該方程只有一解,解的值為:" << Soq << endl;18.編寫一個程序,輸入全班同學某門課考試成績,計算平均成績并找出其中最高分與最低分。(提示:批量數據通常不事先規定輸入的數據數量,而是以一個特殊的標志作為輸入結束。程序根據結束標志統計人數)/約定1000作為結束標志#include <iostream>using namespace std;/#define scanf scanf_s/如果使用VS2017,刪除上面的注釋號int main() int a, s = 0, i = 0, max = 0, min = 0xf

32、fff;while (scanf("%d", &a), a != 1000) s += a;i+;if (a > max) max = a;if (a < min) min = a;cout << "共輸入了" << i << "個學生的成績。" << endl;cout << "其中的最大值為" << max << ",最小值為" << min << endl;cou

33、t << "成績的平均值為" << double(s) / double(i) << endl;return 0;19.編一程序模擬整數加、減、乘、除四則運算。當你在鍵盤上輸入5+6后,程序將輸出=11,當你在鍵盤上輸入11*7后,程序將輸出=77。#include <iostream>#include <string>using namespace std;int main() string a, a1, a2;cin >> a;int i;for (i = 0; ai != '+'

34、&& ai != '-' && ai != '*' && ai != '/' i+);a1 = a.substr(0, i);a2 = a.substr(i + 1, a.length() - 1);switch (ai)case '+':cout << '=' << atoi(a1.c_str() + atoi(a2.c_str(); break;case '-':cout << '=' <&

35、lt; atoi(a1.c_str() - atoi(a2.c_str(); break;case '*':cout << '=' << atoi(a1.c_str() * atoi(a2.c_str(); break;case '/':cout << '=' << atof(a1.c_str() / atof(a2.c_str(); break;default :break;return 0;20.把一張1元鈔票換成1分、2分和5分的硬幣,每種至少有1枚,問有多少種換法?#incl

36、ude <iostream>using namespace std;int main() int a, b, c, n = 0;for (a = 1; a < 100; a+) for (b = 1; b < 50; b+) for (c = 1; c < 20; c+)if (a + b * 2 + c * 5 = 100) n+;cout << "共" << n << "種取法。" << endl;return 0;21.求自然對數底(e)的近似值。e的近似值計算公式為:當

37、余項rn<時停止計算。設=1e-8#include <iostream>using namespace std;int fac(int n) if (n = 0) return 1;else return fac(n-1)*n;int main() double e = 1;int n = 1;while (1.0/double(fac(n) >= 0.00000001) e += 1.0/double(fac(n+);cout << e;return 0;22. !#include <iostream>using namespace std;i

38、nt main() int a = 1, n = 1, s = 1;do n = (a + 1)*n;s = s + n;a+; while (a < 7);cout << "S=1!+2!+.+7!=" << s << endl;return 0;23.××××#include <iostream>using namespace std;int main() int a = 1, s = 0;do s = a * (a + 1) + s;a = a + 2; while (a &

39、lt;= 39);cout << "S=1*2+3*4+.+39*40=" << s << endl;return 0;24.Y=X(-1)n+1的值,精確到10-6。#include <iostream>#include <cmath>using namespace std;int main() const double TINY_VALUE = 1e-10;cout << "請輸入X的值:"double X, Y=0;cin >> X;double t = X;int

40、 n = 1;do Y += t;n+;t = -t * X*X / (2 * n - 1) / (2 * n - 2); while (fabs(t) > TINY_VALUE);cout << "Y=" << Y;return 0;25.編制一個程序,讀入一個正整數,并反向輸出。例如,讀入123,輸出是321。#include <iostream>#include <string>using namespace std;/#define itoa _itoa/#define _itoa _itoa_s/若使用VS20

41、17,刪除上面兩行的注釋int main() int a, b, i = 0;cin >> a;char str255;itoa(a,str,10);for (b = 0; strb != '0' b+); char c;while (i < b / 2) c = stri;stri = strb - i - 1;strb - i - 1 = c;i+;a = atoi(str);cout << a;return 0;26.水仙花數問題:水仙花數是一種三位數,它的值等于每個數字的立方和。例如,153=13+53+33。編程輸出小于999的水仙花數。

42、#include <iostream>using namespace std;int main() int a, b, c, i;for (i = 100; i < 1000; i+) a = i % 10;b = (i / 10) % 10;c = i / 100;if (i = a*a*a + b*b*b + c*c*c) cout << i << ' 'return 0;27.求一整數的等差數列,該數列滿足下述條件:頭4項數的和值為26,積值為880。(提示:該數列公差為正整數,否則數列將出現負數;該數列的首項必須小于5,且其公差

43、也小于5,否則頭四項數的和將大于26。)#include <iostream>using namespace std;int main() int a4 = 4, d = 4, i;while (a0-,1) for (; d > 0; a0-, d-) for (i = 1; i < 4; i+) ai = ai - 1 + d;if (a0 * a1 * a2 * a3 = 880 && a0 + a1 + a2 + a3 = 26) cout << "這個數列的首項為" << a0 << &q

44、uot;,公差為" << d << ",前4項為"for (i = 0; i < 4; i+)cout << ai << ' ' return 0;28.完數問題:若有一數,其值等于它的因子之和,則該數稱為完數。例如,6的因子為1、2、3,而6=1+2+3,故6是完數。編程輸出1000之內的所有完數及其因子。#include <iostream>using namespace std;int main()int s, i, j, k, a100;for (i = 1; i <=

45、 1000; i+) k = 0;s = 0;for (j = 1; j < i; j+) if (i%j = 0) ak = j;k+;s = s + j;if (s = i) cout << i << "是完數,因子為"for (j = 0; j < k; j+) cout << aj << ' 'cout << endl;return 0;29.100匹馬馱100擔貨,大馬一匹馱3擔,中馬一匹馱2擔,小馬2匹馱1擔。試編程計算大、中、小馬的數目。#include <iostr

46、eam>using namespace std;int main() int big, middle;for (middle = 0; middle <= 50; middle+) for (big = 0; big <= 33; big+) if (100 - middle - big) % 2 = 0 && 3 * big + 2 * middle + (100 - middle - big) / 2 = 100)cout << "大馬的數量是" << big<< ",中馬的數量是"

47、; << middle<< ",小馬的數量是" << 100 - middle - big << endl;return 0;30.編程產生出1到10以內的所有數對<i,j>并輸出,其中i>j。#include <iostream>using namespace std;int main() int i, j, k = 0;for (j = 1; j <= 10; j+) for (i = j; i <= 10; i+) cout << '<' <

48、;< i << ',' << j << "> "k+;if (!(k % 10) cout << endl;return 0;31.編程求出1000以內的所有符合如下條件的數:其高位數字小于低位數字。如12,238等。但21,548不符合條件。#include <iostream>#include <iomanip>using namespace std;int main() int a;for (a = 0; a <= 1000; a+) if (a < 10)

49、 continue;else if (a >= 10 && a < 100) int b = a % 10, c = a / 10;if (c < b) cout << setw(5) << a;else if (a >= 100 && a < 1000) int b = a % 10, c = (a / 10) % 10, d = a / 100;if (d < c&&c < b) cout << setw(5) << a;return 0;32.求任一整

50、數N的標準分解式,即素數因子之積。例如16=2*2*2*2, 15=3*5。#include <iostream>using namespace std;int main()int N, n;cout << "Please input N:"cin >> N;n = N;cout << "N="for (int i = 2; i <= N / 2;) if (N%i = 0) cout << i << '*'N = N / i;else i+;if (n = N

51、) cout << N << "*1" << endl;else cout << N << endl;return 0;33.斐波那契(Fibonacci)數列問題:Fibonacci數列遞歸定義為:x0=0,x1=1,xi+1=xi+xi-1, i=2,3,即從第二項開始,數列中的每一個元素等于前面兩個元素之和。編程輸出前20項Fibonacci數。(提示可以用遞歸或迭代兩種方式編程)方法一:運用數列的迭代賦值#include <iostream>using namespace std;int mai

52、n() int a20 = 0,1 , i;for (i = 2; i < 20; i+) ai = ai - 1 + ai - 2;for (i = 0; i < 20; i+) cout << ai << ' 'return 0;方法二:函數的遞歸調用#include <iostream>using namespace std;int Fibonacci(int n) switch (n) case 0:return 0;case 1:return 1;default:return Fibonacci(n - 1) + Fib

53、onacci(n - 2);int main() int i;for (i = 0; i < 20; i+) cout << Fibonacci(i) << ' 'return 0;方法三:變量迭代法#include <iostream>using namespace std;int Fibonacci(int n) if (n = 0) return 0;else if (n = 1) return 1;int a = 0, b = 1, s = 0;while (n >= 2) s = a + b;a = b;b = s;n-

54、;return s;int main() int i;for (i = 0; i < 20; i+) cout << Fibonacci(i) << ' 'return 0;34.打印下面圖形。 1 1 3 1 1 3 5 3 1 1 3 5 7 5 3 1 1 3 5 7 9 7 5 3 1 1 3 21 3 1#include <iostream>#include <iomanip>using namespace std;int main()int i, j, k, l;for (i = 1; i <= 11; i

55、+) for (j = 1; j <= 4 * (11 - i); j+) cout << ' 'for (k = 1; k <= 2 * i - 1; k += 2) cout << setw(4) << k;for (l = 2 * (i - 1) - 1; l >= 1; l -= 2) cout << setw(4) << l;cout << endl;return 0;35.打印如下圖形A B C D EB C D E AC D E A BD E A B CE A B C D#i

56、nclude <iostream>using namespace std;int main() char a55 = 'A' ;int i, j;for (i = 1; i < 5; i+) a0i = a0i - 1 + 1;for (i = 1; i < 5; i+) for (j = 0; j < 4; j+) aij = ai - 1j + 1;ai4 = ai - 10;for (i = 0; i < 5; i+) for (j = 0; j < 5; j+) cout << aij << '

57、'cout << endl;return 0;36.正讀和反讀都一樣的數稱為回文數。編寫程序輸入一個整數max_num,輸出從0到max_num中用二進制表示和十進制表示都是回文數的整數。定義一個函數is_circle_num()判斷一個數(number)在某個進制(radius)下是否為回文數。例如,整數313就是該程序輸出的一個數,因為它的二進制表示為10011001。#include <iostream>using namespace std;bool is_circle_num(int, int);int main() int max_num, i;co

58、ut << "請輸入數組的長度:"cin >> max_num;for (i = 0; i <= max_num; i+)if (is_circle_num(i, 10) && is_circle_num(i, 2)cout << i << ' 'return 0;bool is_circle_num(int number, int radius) int a100, i = 0, j = number;while (j >= radius) ai = j % radius;j /=

59、 radius;i+;ai = j % radius;for (j = 0; j < (i+1) / 2; j+) if (aj != ai - j) return false;return true;37.編寫一個遞歸函數:將一個整數轉換為相應的字符串并輸出,函數原型可聲明為:void int2str(int number)。#include <iostream>using namespace std;void int2str(int number);int main() int a;cin >> a;int2str(a);return 0;void int2s

60、tr(int number) if (number >= 10) int2str(number / 10);cout << number % 10;38.用函數實現將一個以字符串形式表示的十六進制數轉換為一個十進制整數。例如,輸入”A2”轉換為162。#include <iostream>#include <string>using namespace std;int fun(string a);int main() string a;cout << "請輸入十六進制數的值:"cin >> a;cout &l

61、t;< "輸出的值為" << fun(a) << endl;return 0;int fun(string a) int c = 0;for (int b = 0; ab != '0' b+) if (ab >= 'A' && ab <= 'F')c = 16 * c + ab - 'A' + 10;else if (ab >= '0' && ab <= '9')c = 16 * c + ab - '

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論