一、课内实验题(共10小题,100分)
题型得分 | 100 |
-
【描述】
比较两个整数之间的大于、小于、等于、不等于关系。
【输入】
输入在一行中给出2个整数a和b。
【输出】
分行输出整数a和b之间的大于、小于、等于、不等于关系。
【输入示例】5 3
【输出示例】true false false true
【来源】
《程序设计基础——以C++为例》第2章实验1。 (10分)我的答案:
#include<iostream> using namespace std; int main() { int a, b; cin >> a >> b; if (a > b) { cout << "true" << endl; cout << "false"<<endl; cout << "false" << endl; cout << "true" ; } else if (a < b) { cout << "false" << endl; cout << "true" << endl; cout << "false" << endl; cout << "true" ; } else{ cout << "false" << endl; cout << "false" << endl; cout << "true" << endl; cout << "false" ; } }
题目得分 10 -
【描述】
计算三个整数的和、平均值、最小值和最大值。
可以使用条件运算符,也可以使用算法库中min和max函数求最小值和最大值。
【输入】
输入在一行中给出整数a、b、c。
【输出】
分行输出整数a、b、c的和、平均值、最小值和最大值。
【输入示例】5 3 -1
【输出示例】7 2.33333 -1 5
【来源】
《程序设计基础——以C++为例》第2章实验2强化练习。 (10分)我的答案:
#include<iostream> using namespace std; int main() { int a, b,c; cin >> a >> b >> c; cout << a + b + c << endl; cout << (double)((a + b + c) / 3.0) << endl; cout << min(a, min(b, c)) << endl; cout << max(a, max(b, c)); }
题目得分 10 -
【描述】
输入一个四位正整数,将该整数每一位上的数字加9,然后除以10取余,作为该位上的新数字,最后将千位和十位上的数字互换,百位和个位上的数字互换,组成变换后的新四位正整数并输出。题目保证转换后的数的千位不会为0。
【输入】
输入一个四位正整数。
【输出】
输出变换后的新四位正整数。
【输入示例】1257
【输出示例】4601
【来源】
《程序设计基础——以C++为例》第2章实验4强化练习。 (10分)我的答案:
#include<iostream> using namespace std; int main() { int a, b[5]; cin >> a; for (int i = 0;i < 4;i++) { b[i] = a % 10; a = a / 10; } for (int i = 0;i < 4;i++) { b[i] = (b[i] + 9) % 10; } cout << b[1] << b[0] << b[3] << b[2]; }
题目得分 10 -
【描述】
输入一个整数,检查它是否能同时被2和3整除,是否被2或3整除,是否被2或3整除且只被其一整除。
【输入】
输入一个整数。
【输出】
分行输出该整数是否能同时被2和3整除,是否被2或3整除,是否被2或3整除且只被其一整除。见输出示例。
【输入示例】18
【输出示例】18 divisible by 2 and 3? true 18 divisible by 2 or 3? true 18 divisible by 2 or 3, but not both? false
【来源】
《程序设计基础——以C++为例》第2章实验6。 (10分)我的答案:
#include<iostream> using namespace std; int main() { int a; cin >> a; if (a % 2 == 0 && a % 3 == 0) { cout << a << " divisible by 2 and 3? true" << endl; cout << a << " divisible by 2 or 3? true" << endl; cout << a << " divisible by 2 or 3, but not both? false" << endl; } else if ((a % 2 == 0 && a % 3 != 0) || (a % 2 != 0 && a % 3 == 0)) { cout << a << " divisible by 2 and 3? false" << endl; cout << a << " divisible by 2 or 3? true" << endl; cout << a << " divisible by 2 or 3, but not both? true" << endl; } /*else if (a % 2 == 0 || a % 3 == 0) { cout << a << " divisible by 2 and 3? false"; cout << a << " divisible by 2 or 3? true"; cout << a << " divisible by 2 or 3, but not both? flase"; }*/ else if (a % 2 != 0 && a % 3 != 0) { cout << a << " divisible by 2 and 3? false" << endl; cout << a << " divisible by 2 or 3? flase" << endl; cout << a << " divisible by 2 or 3, but not both? flase" << endl; } }
题目得分 10 -
【描述】
输入一个整数a(a不为-1),求如下表达式的值。
【输入】
输入一个整数a。
【输出】
输出表达式的值。
【输入示例】2
【输出示例】2.2555
【提示】
求三角函数cos的值可以使用数学库中的cos(x)函数,x为弧度值。求平方根可以使用数学库中的sqrt(x)函数。
【来源】
《程序设计基础——以C++为例》第2章实验15。 (10分)我的答案:
#include<iostream> #include<cmath> #include<iomanip> using namespace std; #define PI 3.1415926 int main() { int a; cin >> a; cout << (double)(cos(50.0/180*PI) + sqrt(37.5)) / (double)(a + 1); }
题目得分 10 -
【描述】
编写程序,读取投资总额、年利率和年数,然后使用如下公式计算未来投资金额。
【输入】
一行中给出投资总额、年利率和年数,其间以空格分隔。
【输出】
一行中输出未来投资金额。
【输入示例】1000 3.25 1
【输出示例】1032.99
【提示】
可以使用数学库中的pow函数来计算a的b次幂。
年利率转换为月利率,年数转换为月数。 (10分)我的答案:
#include<iostream> #include<cmath> #include<iomanip> using namespace std; #define PI 3.1415926 int main() { double fV, iA, mR, nOM; cin >> iA >> mR >> nOM; cout <<(double)iA * pow((1 + mR*0.01/12.0), nOM*12); }
题目得分 10 -
【描述】
编写程序,根据火车的出发时间和达到时间计算整个旅途所用的时间。
【输入】
在一行中给出两个正整数,其间以空格分隔,分别表示火车的出发时间和到达时间。每个时间的格式为两位小时数(00~23)和两位分钟数(00~59),假设出发和到达在同一天内。
【输出】
在一行中输出该旅途所用的时间,格式为“hh:mm”,其中hh为小时数、mm为分钟数。
【输入示例】1201 1530
【输出示例】3:29
(10分)我的答案:
#include<iostream> #include<cmath> #include<iomanip> using namespace std; #define PI 3.1415926 int main() { int go, to, a[5], b[5]; cin >> go >> to; for (int i = 0;i < 2;i++) { a[i] = go % 100; go /= 100; b[i] = to % 100; to /= 100; } int time,time1, time2; time1 = a[1] * 60 + a[0]; time2 = b[1] * 60 + b[0]; time = time2 - time1; cout << time / 60 << ':' << time - time / 60 * 60; }
题目得分 10 -
【描述】
编写程序,输入两个整数,存放在整型变量a和b中,交换并输出a和b中的值。
可以借助中间变量,也可以使用算法库中的swap函数交换两个变量中的值。
【输入】
在一行中给出两个整数,以空格间隔。
【输出】
在一行中输出两个交换后的整数,以空格间隔,行末没有多余的空格。
【输入示例】6 2
【输出示例】2 6
【来源】
《程序设计基础——以C++为例》第2章实验3。 (10分)我的答案:
#include<iostream> #include<cmath> #include<iomanip> using namespace std; #define PI 3.1415926 int main() { int a, b; cin >> a >> b; cout << b << ' ' << a; }
题目得分 10 -
【描述】
输入一个三位正整数,要求正确地分离出它的百位、十位和个位数,并输出。
【输入】
一个三位正整数。
【输出】
分行输出该正整数的百位、十位和个位数。
【输入示例】123
【输出示例】1 2 3
【来源】
《程序设计基础——以C++为例》第2章实验4。 (10分)我的答案:
#include<iostream> #include<cmath> #include<iomanip> using namespace std; #define PI 3.1415926 int main() { int a,b[5]; cin >> a; for (int i = 0;i < 3;i++) { b[i] = a % 10; a /= 10; } for (int i = 2;i >= 0;i--) { cout << b[i] << endl; } }
题目得分 10 -
【描述】
小明有5元钱,他想去买雪糕吃,雪糕的价格各不相同,根据雪糕的价格,计算小明最多能买多少根雪糕。
【输入】
输入只有一个数,一根雪糕的价格,单位是元。
【输出】
输出只一个整数,小明最多能买到的雪糕数。
【输入示例】1.3
【输出示例】3
(10分)我的答案:
#include<iostream> #include<cmath> #include<iomanip> using namespace std; #define PI 3.1415926 int main() { double a; cin >> a; cout << static_cast<int>(5.0 / a); }
题目得分 10