上机考试题集_第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

提交评论