CSU作业答案参考B
C++程序设计答案专题
程序控制结构
作者:哀歌殇年(QQ:2690034441,VX:agsn2690034441)
版本:V1.0.0(2023/10/02)
历史版本:
版本号 |
日期 |
说明 |
V1.0.0 |
2023/10/02 |
首次发布 |
海伦公式计算三角形面积
C++ |
---|
| #include <iostream>
#include <cmath>
using namespace std;
int main()
{
float a, b, c, s, area;
cout << "输入三边的长度:";
cin >> a >> b >> c;
if (a+b>c && a+c>b && b+c>a) {
s = (a + b + c) / 2;
area = sqrt(s * (s - a) * (s - b) * (s - c));
cout << "三角形的面积:" << area << endl;
}
else {
cout << "你输入了不合法的数值,注意任意两边大于第三边,请重新输入" << endl;
}
main();
return 0;
}
|
计算一元二次方程的解
C++ |
---|
| #include <iostream>
#include <cmath>
using namespace std;
int main()
{
float a, b, c, j1, j2, delta, d;
cout << "请依次输入形如ax^2+bx+c=0的二元一次方程的a,b,c的值:";
cin >> a >> b >> c;
delta = sqrt(b * b - 4 * a * c);
if (delta > 0) {
j1 = (-b + delta) / (2 * a);
j2 = (-b - delta) / (2 * a);
cout << "该方程有两个实数解,分别是" << j1 << "," << j2 << endl;
}
else if (delta == 0) {
j1 = (-b) / (2 * a);
cout << "该方程有一个实数解,是" << j1 << endl;
}
else{
delta = -(b * b - 4 * a * c);
d = delta / (2 * a);
j1 = (-b) / (2 * a);
cout << "该方程有两个复数解,分别是" << j1 << "+" << d << "i," << j1 << "-" << d << "i" << endl;
}
main();
return 0;
}
|
购物打折优惠促销
C++ |
---|
| #include <iostream>
using namespace std;
int main()
{
float num;
cout << "请输入价格:";
cin >> num;
if (num > 4000) {
num = num * 0.8;
cout << "打八折:" << num << endl;
}
else if (num > 3000) {
num = num * 0.85;
cout << "打八五折:" << num << endl;
}
else if (num > 2000) {
num = num * 0.9;
cout << "打九折:" << num << endl;
}
else if (num > 1000) {
num = num * 0.95;
cout << "打九五折:" << num << endl;
}
else {
cout << "不打折:" << num << endl;
}
main();
return 0;
}
|
判断月的天数
C++ |
---|
| #include <iostream>
using namespace std;
int main()
{
int year, month, day = 0;
cout << "请依次输入年、月:";
cin >> year >> month;
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
day = 31;
}
else if (month == 4 || month == 6 || month == 9 || month == 11) {
day = 30;
}
else if (month == 2) {
if (year % 4 == 0) {
day = 29;
}
else {
day = 28;
}
}
else {
cout << "月输入不合法,月数应在1~12" << endl;
}
cout << day << endl;
main();
return 0;
}
|
求正整数的阶乘
C++ |
---|
| #include <iostream>
using namespace std;
int main()
{
unsigned long long a, s = 1;
cout << "请输入一个正整数:";//测试发现超过65会数据溢出而输出0
cin >> a;
for (; a > 0; a -= 1)s *= a;
cout << s << endl;
main();
return 0;
}
|
计算pi的近似值
C++ |
---|
| #include <iostream>
#include <cmath>
using namespace std;
int main()
{
double pi = 0, n = 1.0;
//for循环
for (int i = 0; (1.0 / n) >= 1E-8; i++)
{
pi = pi + 1 / n * pow(-1, i);
n = n + 2;
}
//while循环
/*
int m = 0;
while (1.0/n > 1E-8)
{
pi = pi + 1 / n * pow(-1, m);
n = n + 2;
m = m + 1;
}
*/
cout << pi * 4 << endl;
return 0;
}
|
求1000内的完数
C++ |
---|
| #include <iostream>
using namespace std;
int main() {
for (int i = 2; i <= 1000; ++i) {
int sum = 1;
for (int j = 2; j * j <= i; ++j) {
if (i % j == 0) {
sum += j;
if (j != i / j) {
sum += i / j;
}
}
}
if (sum == i) {
cout << i << "-->";
for (int j = 1; j < i; ++j) {
if (i % j == 0) {
cout << j;
if (j != i - 1) {
cout << ",";
}
}
}
cout << endl;
}
}
return 0;
}
|
猴桃问题
C++ |
---|
| #include <iostream>
using namespace std;
int main()
{
for (int mmon = 1; mmon <= 18; mmon++)
{
for (int fmon = 1; fmon <= 20 - mmon; fmon++)
{
for (int smon = 1; smon <= 20 - mmon - fmon; smon++)
{
if (50 == mmon * 5 + fmon * 4 + smon * 2 && 20 == mmon + fmon + smon)
{
cout << mmon << "," << fmon << "," << smon << endl;
}
}
}
}
return 0;
}
|
牛顿迭代法求根
C++ |
---|
| #include <iostream>
#include <cmath>
using namespace std;
int main() {
float xn,xn1,f,f1;
cout << "请输入x的初值:";
cin >> xn1;
do {
xn = xn1;
f = 3 * xn * xn * xn - 4 * xn * xn - 5 * xn + 13;
f1 = 9 * xn * xn - 8 * xn - 5;
xn1 = xn - f / f1;
} while (fabs(xn1 - xn) >= 1e-6);
cout << "方程的一个根为:" << xn1 << endl;
return 0;
}
|
输出菱形图案
C++ |
---|
| #include<iostream>
using namespace std;
int main() {
int row;
int i, j, n;
cout << "请输入行数:";
cin >> row;
if (row % 2 != 0)
n = row / 2 + 1;
else
n = row / 2;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n - i; j++)
cout << ' ';
for (j = 1; j <= 2 * i - 1; j++)
cout << '*';
cout << endl;
}
if (row%2==0) {
for (i = 1; i <= n; i++) {
for (j = 1; j <= i-1; j++)
cout << ' ';
for (j = 1; j <= row - 2 *i+1; j++)
cout << '*';
cout << endl;
}
}
else if (row%2!=0) {
for (i = 1; i <= n - 1; i++) {
for (j = 1; j <= i; j++)
cout << ' ';
for (j = 1; j <= row - 2 * i; j++)
cout << '*';
cout << endl;
}
}
return 0;
}
|
函数与编译预处理
作者:哀歌殇年(QQ:2690034441,VX:agsn2690034441)
版本:V1.0.0(2023/10/03)
历史版本:
版本号 |
日期 |
说明 |
V1.0.0 |
2023/10/03 |
首次发布 |
计算s=m!+n!
C++ |
---|
| #include <iostream>
using namespace std;
float add(int x, int y) {
return x + y;
}
float fac(int n) {
int s = 1;
for (int i = 1; i <= n; i++)
{
s = s * i;
}
return s;
}
int main()
{
int m, n;
cout << "输入m,n" << endl;
cin >> m >> n;
cout << add(fac(m), fac(n)) << endl;
main();
return 0;
}
|
求1~100的素数之和
C++ |
---|
| #include <iostream>
using namespace std;
int isprime(int n) {
for (int i = 2; i < n; i++)
{
if (n % i == 0) {
n = 0;
break;
}
}
return n;
}
int main()
{
int sum = 0;
for (int i = 2; i <= 100; i++)
{
sum = sum + isprime(i);
}
cout << sum << endl;
return 0;
}
|
求a的n次幂
C++ |
---|
| #include <iostream>
using namespace std;
//非递归
float pow(float a, int n) {
float sum = 1;
if (n == 0) {
return 1;
}
else if (n > 0) {
for (int j = 1; j <= n; j++)
{
sum *= a;
}
return sum;
}
}
//递归
/*
float pow(float a, int n) {
if (n == 0) {
return 1.0;
}
else if (n > 0) {
return a * pow(a, n - 1);
}
}
*/
int main()
{
int a, n;
cout << "输入a,n" << endl;
cin >> a >> n;
if (n >= 0) {
cout << pow(a, n) << endl;
}
else {
cout << "n必须大于等于0" << endl;
}
main();
return 0;
}
|
求和
C++ |
---|
| #include <iostream>
using namespace std;
//非递归
/*
float sum(int n) {
int s = 0;
for (int j = 1; j <= n; j++)
{
s = s + j;
}
return s;
}
*/
//递归
float sum(int n) {
if (n >= 3) {
return n + sum(n - 1);
}
}
int main()
{
int n;
cout << "求s=1+2+3+……+n,输入n" << endl;
cin >> n;
if (n >= 1) {
cout << sum(n) << endl;
}
else {
cout << "n必须大于等于1" << endl;
}
main();
return 0;
}
|
输出英文字符
C++ |
---|
| #include <iostream>
#include <cctype>
using namespace std;
int main() {
cout << "请输入一串字符: ";
char input;
while (cin.get(input)) {
if (isalpha(input)) {
cout << input;
}
}
cout << endl;
main();
return 0;
}
|
计算a!/b^n
file1.cpp |
---|
| inline float fac(int m) {
int s = 1;
for (int i = 1; i <= m; i++)
{
s = s * i;
}
return s;
}
|
file2.cpp |
---|
| #include <iostream>
#include "file1.cpp"
using namespace std;
//非递归
float pow(float a, int n) {
float sum = 1;
if (n == 0) {
return 1;
}
else if (n > 0) {
for (int j = 1; j <= n; j++)
{
sum *= a;
}
return sum;
}
}
//递归
/*
float pow(float a, int n) {
if (n == 0) {
return 1.0;
}
else if (n > 0) {
return a * pow(a, n - 1);
}
}
*/
int main()
{
int a, b, n;
cout << "输入a,b,n:";
cin >> a >> b >> n;
cout << fac(a) / pow(b, n) << endl;
main();
return 0;
}
|
数组与指针
作者:哀歌殇年(QQ:2690034441,VX:agsn2690034441)
版本:V1.0.0(2023/10/03)
历史版本:
版本号 |
日期 |
说明 |
V1.0.0 |
2023/10/03 |
首次发布 |
排序10个数字
C++ |
---|
| #include <iostream>
using namespace std;
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[10], n = 10;
cout << "请输入10个整数: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
bubbleSort(arr, n);
cout << "排序后的数组: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
|
插入新数字
C++ |
---|
| #include <iostream>
using namespace std;
int main()
{
cout << "请输入一个数插入到数组中" << endl;
double a[]{ 2,5,8,10,15,22,40,55,82,100 }, b;
cin >> b;
const int length = sizeof(a) / sizeof(a[0]);
double c[length + 1]{};
for (int i = 0; i < length; i++) {
if (a[i] <= b && b <= a[i + 1]) {
for (int k = 0; k < length; k++)
{
c[k] = a[k];
}
for (int j = length; j > i + 1; --j) {
c[j] = c[j - 1];
}
c[i + 1] = b;
break;
}
else if (b < a[i]) {
for (int k = 0; k < length; k++)
{
c[k] = a[k];
}
for (int j = length; j > i; --j) {
c[j] = c[j - 1];
}
c[i] = b;
break;
}
else if (b > a[length - 1]) {
for (int k = 0; k < length; k++)
{
c[k] = a[k];
}
c[length] = b;
break;
}
}
for (int i = 0; i <= length; i++)
{
cout << c[i] << " ";
}
main();
return 0;
}
|
剔除相同数字的数组
C++ |
---|
| #include <iostream>
using namespace std;
int main() {
int np[100], count = 0;
cout << "请输入100个整数:" << endl;
for (int i = 0; i < 100; ++i) {
int num;
cin >> num;
bool isDuplicate = false;
for (int j = 0; j < count; ++j) {
if (np[j] == num) {
isDuplicate = true;
break;
}
}
if (!isDuplicate) {
np[count] = num;
++count;
}
cout << i << " " << endl;
}
cout << "不重复的整数数组为:" << endl;
for (int i = 0; i < count; ++i) {
cout << np[i] << " ";
}
cout << endl;
return 0;
}
|
找二维数组的鞍点
C++ |
---|
| #include <iostream>
using namespace std;
int main() {
cout << "输入16个数到4*4的二维数组:";
int arr[4][4];
for (int n = 0; n <= 3; n++) {
for (int m = 0; m <= 3; m++) {
cin >> arr[n][m];
}
}
for (int n = 0; n <= 3; n++) {
for (int m = 0; m <= 3; m++) {
cout << arr[n][m] << "\t";
}
cout << endl;
}
int s = 0;
for (int i = 0; i < 3; i++) {
int row_max = arr[i][0];
int col_index = 0;
// 找出当前行的最大值和列索引
for (int j = 1; j < 3; j++) {
if (arr[i][j] > row_max) {
row_max = arr[i][j];
col_index = j;
}
}
// 检查当前列中是否是最小值
bool is_saddle_point = true;
for (int k = 0; k < 3; k++) {
if (arr[k][col_index] < row_max) {
is_saddle_point = false;
break;
}
}
// 如果是鞍点,则输出
if (is_saddle_point) {
cout << "鞍点位置 (" << i << ", " << col_index << "): " << arr[i][col_index] << endl;
s += 1;
}
}
if (s == 0) {
cout << "该数组没有鞍点" << endl;
}
else {
cout << "该数组有" << s << "个鞍点" << endl;
}
return 0;
}
|
逆序排放数组元素
C++ |
---|
| #include <iostream>
#include <algorithm>
using namespace std;
//方法一:对位替换法 缺点:要考虑元素个数奇偶情况
void f(int a[], int n) {
int s, t = 0;
if (n % 2 == 0)t = 1;
for (int m = 1; m <= n / 2; m++) {
s = a[n / 2 - m];
a[n / 2 - m] = a[n / 2 + m - t];
a[n / 2 + m - t] = s;
}
for (int k = 0; k <= n - 1; k++) {
cout << a[k] << " ";
}
}
//方法二:倒序赋值法 缺点:需要定义新数组,占用内存
void f2(int a[], int n) {
int* b = new int[n];
for (int m = 0; m <= n; m++) {
b[m] = a[n - m - 1];
}
for (int k = 0; k <= n - 1; k++) {
cout << b[k] << " ";
}
}
//方法三:函数秒杀法 缺点:出题人可能会觉得不爽
void f3(int a[], int n) {
for (int i = 0; i < n / 2; i++) {
swap(a[i], a[n - i - 1]);
}
for (int k = 0; k <= n - 1; k++) {
cout << a[k] << " ";
}
}
//方法四:函数秒杀法pro 缺点:出题人:你最好是删了重写
void f4(int a[], int n) {
reverse(a, a + n);
for (int k = 0; k <= n - 1; k++) {
cout << a[k] << " ";
}
}
int main() {
int n;
cout << "请输入数组大小: ";
cin >> n;
int* arr = new int[n];
cout << "请输入 " << n << " 个整数: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
f(arr, n);
return 0;
}
|
回文串
C++ |
---|
| #include <iostream>
#include <cstring>
using namespace std;
int f(char* s) {
int n = strlen(s), t = 0, k = 0;
if (n % 2 == 0)t = 1;
for (int m = 1; m <= n / 2; m++) {
if (s[n / 2 - m] == s[n / 2 + m - t]) {
k++;
}
}
if (n / 2 == k) {
return 1;
}
else {
return 0;
}
}
int main() {
char a[] = "ab11ba";
cout << f(a);
return 0;
}
|
双号元素赋值
C++ |
---|
| #include <iostream>
#include <cstring>
using namespace std;
int main() {
char str1[] = "Hello,World!";
char str2[100];
int len1 = strlen(str1);
for (int i = 0; i < len1; i += 2) {
str2[i / 2] = str1[i];
}
str2[len1 / 2] = '\0';
cout << "str1内容: " << str1 << endl;
cout << "str2内容: " << str2 << endl;
return 0;
}
|
统计字符数量
方法一:ASCII码法 估计是出题人的本意
C++ |
---|
| #include <iostream>
using namespace std;
int main() {
char input[1000];
cout << "请输入一串文字:";
cin.getline(input, 1000);
int uppercaseCount = 0, lowercaseCount = 0, spaceCount = 0, digitCount = 0, otherCount = 0;
for (int i = 0; input[i] != '\0'; i++) {
if (input[i] >= 'A' && input[i] <= 'Z') { // 大写字母 (A-Z)
uppercaseCount++;
}
else if (input[i] >= 'a' && input[i] <= 'z') { // 小写字母 (a-z)
lowercaseCount++;
}
else if (input[i] == ' ') { // 空格
spaceCount++;
}
else if (input[i] >= '0' && input[i] <= '9') { // 数字 (0-9)
digitCount++;
}
else {
otherCount++;
}
}
cout << "大写字母数量:" << uppercaseCount << endl;
cout << "小写字母数量:" << lowercaseCount << endl;
cout << "空格数量:" << spaceCount << endl;
cout << "数字数量:" << digitCount << endl;
cout << "其他字符数量:" << otherCount << endl;
return 0;
}
|
方法二:函数法
C++ |
---|
| #include <iostream>
#include <cctype>
using namespace std;
int main() {
char input[1000];
cout << "请输入一串文字:";
cin.getline(input, 1000);
int uppercaseCount = 0, lowercaseCount = 0, spaceCount = 0, digitCount = 0, otherCount = 0;
for (int i = 0; input[i] != '\0'; i++) {
if (isupper(input[i])) {
uppercaseCount++;
}
else if (islower(input[i])) {
lowercaseCount++;
}
else if (isspace(input[i])) {
spaceCount++;
}
else if (isdigit(input[i])) {
digitCount++;
}
else {
otherCount++;
}
}
cout << "大写字母数量:" << uppercaseCount << endl;
cout << "小写字母数量:" << lowercaseCount << endl;
cout << "空格数量:" << spaceCount << endl;
cout << "数字数量:" << digitCount << endl;
cout << "其他字符数量:" << otherCount << endl;
return 0;
}
|
统计单词数目
C++ |
---|
| #include <iostream>
#include <cctype>
using namespace std;
int main() {
char input[1000];
cout << "请输入一串英文单词:";
cin.getline(input, 1000);
int wordCount = 0;
bool inWord = false;
for (int i = 0; input[i] != '\0'; i++) {
if (!isspace(input[i])) {
if (!inWord) {
wordCount++;
inWord = true;
}
}
else {
inWord = false;
}
}
cout << "单词数量:" << wordCount << endl;
return 0;
}
|
输出最长的行
C++ |
---|
| #include <iostream>
#include <string>
using namespace std;
int main() {
string input, longestLine;
size_t maxLength = 0;
cout << "请输入若干字符行,以空行结束:" << endl;
while (true) {
getline(cin, input);
if (input.empty()) {
break;
}
if (input.length() > maxLength) {
maxLength = input.length();
longestLine = input;
}
}
if (!longestLine.empty()) {
cout << "最长的行是:" << longestLine << endl;
}
else {
cout << "未输入任何行。" << endl;
}
return 0;
}
|
输出国家名
C++ |
---|
| #include <iostream>
#include <string>
using namespace std;
bool compareStrings(const string& str1, const string& str2) {
return str1 > str2;
}
void sortCountries(string countries[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (compareStrings(countries[j], countries[j + 1])) {
string temp = countries[j + 1];
countries[j + 1] = countries[j];
countries[j] = temp;
}
}
}
}
int main() {
const int n = 20;
string countries[n] = {
"Canada", "Brazil", "Australia", "China", "India",
"France", "Germany", "Japan", "Mexico", "Russia",
"United States", "United Kingdom", "Italy", "Spain",
"South Korea", "Argentina", "Saudi Arabia", "South Africa", "Turkey"
};
sortCountries(countries, n);
cout << "按字母先后顺序排列的国家名:" << endl;
for (int i = 0; i < n; i++) {
cout << countries[i] << endl;
}
return 0;
}
|
编写find函数
C++ |
---|
| #include <iostream>
#include <string>
using namespace std;
bool find(const char* s, const char* word) {
string str(s);
string searchWord(word);
size_t pos = 0;
while ((pos = str.find(searchWord, pos)) != string::npos) {
if (pos == 0 || isspace(str[pos - 1])) {
if (pos + searchWord.length() == str.length() || isspace(str[pos + searchWord.length()])) {
return true;
}
}
pos += searchWord.length();
}
return false;
}
int main() {
const char* inputString = "This is a sample sentence containing words.";
const char* wordToFind = "sample";
if (find(inputString, wordToFind)) {
cout << "字符串中包含单词" << wordToFind << endl;
}
else {
cout << "字符串中不包含单词" << wordToFind << endl;
}
return 0;
}
|
自定义数据类型
作者:哀歌殇年(QQ:2690034441,VX:agsn2690034441)
版本:V1.0.0(2023/10/04)
历史版本:
版本号 |
日期 |
说明 |
V1.0.0 |
2023/10/04 |
首次发布 |
学生档案
C++ |
---|
| #include <iostream>
#include<string>
using namespace std;
struct date {
int year;
int month;
};
struct student {
int id;
string name;
string sex;
date birth;
int score;
string napla;
};
void bubbleSort(student arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j].score < arr[j + 1].score) {
student temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main()
{
student stu[5];
cout << "请输入学生信息:" << endl;
cout << "\t学号\t姓名\t性别\t出生年月\t入学总分\t籍贯" << endl;
for (int i = 0; i < 5; i++) {
cout << i + 1 << " ";
cin >> stu[i].id >> stu[i].name >> stu[i].sex >>
stu[i].birth.month >> stu[i].birth.year >> stu[i].score >> stu[i].napla;
}
cout << "排序结果如下:" << endl;
bubbleSort(stu, 5);
cout << "\t学号\t姓名\t性别\t出生年月\t入学总分\t籍贯" << endl;
for (int i = 0; i < 5; i++) {
cout << i + 1 << " ";
cout << stu[i].id << "\t" << stu[i].name << "\t" << stu[i].sex << "\t" <<
stu[i].birth.month << " " << stu[i].birth.year << "\t" << stu[i].score << "\t" << stu[i].napla;
}
return 0;
}
|
学生成绩
C++ |
---|
| #include <iostream>
#include<string>
using namespace std;
struct student {
int id;
string name;
double score[3];
};
void bubbleSort(student arr[], int n, int s) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j].score[s] < arr[j + 1].score[s]) {
student temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main()
{
student stu[10];
cout << "请输入学生信息:" << endl;
cout << "\t学号\t姓名\t课程1分数\t课程2分数\t课程3分数" << endl;
for (int i = 0; i < 10; i++) {
cout << i + 1 << " ";
cin >> stu[i].id >> stu[i].name >> stu[i].score[0] >> stu[i].score[1] >> stu[i].score[2];
}
for (int j = 0; j < 3; j++) {
cout << "课程" << j + 1 << "情况:" << endl;
float sum = 0;
for (int i = 0; i < 10; i++) {
sum += stu[i].score[j];
}
cout << "平均分:" << sum / 10 << endl;
bubbleSort(stu, 10, j);
int stunum = 0;
while (stunum <= 9) {
if (stunum == 0)cout << "最高分学生情况:" << endl;
if (stunum == 9)cout << "最低分学生情况:" << endl;
cout << "学号:" << stu[stunum].id << endl;
cout << "姓名:" << stu[stunum].name << endl;
cout << "成绩:" << stu[stunum].score[0] << "/" << stu[stunum].score[1] << "/" << stu[stunum].score[2] << endl;
cout << "平均分:" << (stu[stunum].score[0] + stu[stunum].score[1] + stu[stunum].score[2]) / 3 << endl << endl;
stunum += 9;
}
cout << endl;
}
return 0;
}
|