跳转至

CSU作业答案参考A

cpp作业1答案参考

以下答案仅供参考,由cpp渣渣哀歌殇年编写
有错误欢迎指出,感谢!(QQ:2690034441)
当前版本:V1.4.0(2023/10/16)
历史版本:

版本号 日期 说明
V1.4.0 2023/10/16 修正了错误-->实验3实验思考第4题
V1.3.0 2023/10/15 使用了侯睿咏同学的程序-->实验3实验思考第6题
V1.2.0 2023/10/14 添加了调试结果
V1.1.1 2023/09/29 修复了一些问题-->实验3实验内容第5题
V1.1.0 2023/09/26 使用了韩轶同学的修改程序-->实验3实验内容第5题
V1.0.0 2023/09/25 首次发布

重点提示:
1. main(); 这句可以不要,是编者为了调试程序方便加的
2. 所有代码都经过上机验证,报错应该是你的问题
3. 算法不止一种,建议先自己写一遍程序再看答案,要不然没有太大意义

实验2实验内容第5题

C++
#include <iostream>
using namespace std;
int main()
{
    unsigned short year, month, days;
    cout << "请输入年、月:";
    cin >> year >> month;
    switch (month) {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
        days = 31;
        break;
    case 4:
    case 6:
    case 9:
    case 11:
        days = 30;
        break;
    case 2:
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
            days = 29;
        }
        else {
            days = 28;
        }
    }
    cout << year << "年" << month << "月的天数为:" << days << endl;
    main();
    return 0;
}
Text Only
1
2
3
4
5
6
7
8
请输入年、月:2021 4
2021年4月的天数为:30
请输入年、月:2022 2
2022年2月的天数为:28
请输入年、月:2020 2
2020年2月的天数为:29
请输入年、月:1485 7
1485年7月的天数为:31

实验2实验思考第6题

C++
#include <iostream>
using namespace std;
int main()
{
    double x, y;
    char z;
    cout << "请输入简单的四则运算算式:";
    cin >> x >> z >> y;
    switch (z) {
    case '+':cout << x << "+" << y << "=" << x + y << endl; break;
    case '-':cout << x << "-" << y << "=" << x - y << endl; break;
    case '*':cout << x << "*" << y << "=" << x * y << endl; break;
    case '/':
        if (y == 0) {
            cout << "除数不能为零!" << endl;
        }
        else {
            cout << x << "/" << y << "=" << x / y << endl;
        }
        break;
    }
    main();
    return 0;
}
Text Only
请输入简单的四则运算算式:24.6+56.8
24.6+56.8=81.4
请输入简单的四则运算算式:22.7-56.2
22.7-56.2=-33.5
请输入简单的四则运算算式:25.1*77.4
25.1*77.4=1942.74
请输入简单的四则运算算式:56/9
56/9=6.22222
请输入简单的四则运算算式:45/0
除数不能为零!

实验3实验内容第2题

C++
#include <iostream>
using namespace std;
int main()
{
    int a, q, n, sum;
    a = 1; q = 2; n = sum = 0;
    do {
        sum = sum + a;
        ++n;
        a = a * q;
    } while (sum < 100);
    --n;
    cout << n << endl;
    return 0;
}
Text Only
6

实验3实验内容第5题

C++
#include <iostream>
using namespace std;
int main()
{
    int row, i, j, n;
    cout << "请输入行数:";
    cin >> row;
    n = row / 2 + 1;
    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;
    }
    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;
    }
    main();
    return 0;
}
Text Only
请输入行数:5
  *
 ***
*****
 ***
  *
请输入行数:4
  *
 ***
*****
 **

请输入行数:8
    *
   ***
  *****
 *******
*********
 ******
  ****
   **

请输入行数:11
     *
    ***
   *****
  *******
 *********
***********
 *********
  *******
   *****
    ***
     *
请输入行数:2
 *
***
本人觉得书上给的这个👆例程不是很好,行数为偶数个时比较菱形比较难看,就写了个别的,但是我写的程序代码比较长,韩哥给我优化了下:
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;
}
Text Only
请输入行数:5
  *
 ***
*****
 ***
  *
请输入行数:4
 *
***
***
 *
请输入行数:8
   *
  ***
 *****
*******
*******
 *****
  ***
   *
请输入行数:11
     *
    ***
   *****
  *******
 *********
***********
 *********
  *******
   *****
    ***
     *
请输入行数:2
*
*

实验3实验思考第1题

C++
#include <iostream>
using namespace std;
int main()
{
    int n;
    double sum = 0;
    cout << "输入n:";
    cin >> n;
    for (int i = 1; i <= n; i++) {
        sum = sum + i * i;
    }
    cout << sum / n << endl;
    main();
    return 0;
}
Text Only
1
2
3
4
5
6
输入n:5
11
输入n:11
46
输入n:24
204.167

实验3实验思考第2题

C++
#include <iostream>
using namespace std;
int main()
{
    int a, b;
    cout << "请输入两个正整数:";
    cin >> a >> b;
    if (a == b) {
        cout << "两数不互质" << endl;
    }
    else {
        if (a > b) {
            int i = a;
            a = b;
            b = i;
        }
        for (int j = 2; j <= a; j++) {
            if (a % j == 0 && b % j == 0) {
                cout << "两数不互质" << endl;
                goto end;
            }
        }
        cout << "两数互质" << endl;
    }
    end:
    main();
    return 0;
}
Text Only
请输入两个正整数:5 7
两数互质
请输入两个正整数:15 25
两数不互质
请输入两个正整数:1 4
两数互质
请输入两个正整数:100 37
两数互质
请输入两个正整数:49 14
两数不互质
编者提示:要尽可能地少用goto,想一想怎么改能不使用goto实现本题的要求👆

实验3实验思考第3题

C++
#include <iostream>
using namespace std;
int main()
{
    for (int year = 2000; year <= 3000; year++) {
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
            cout << year << " ";
        }
    }
    return 0;
}
Text Only
2000 2004 2008 2012 2016 2020 2024 2028 2032 2036 2040 2044 2048 2052 2056 2060 2064 2068 2072 2076 2080 2084 2088 2092 2096 2104 2108 2112 2116 2120 2124 2128 2132 2136 2140 2144 2148 2152 2156 2160 2164 2168 2172 2176 2180 2184 2188 2192 2196 2204 2208 2212 2216 2220 2224 2228 2232 2236 2240 2244 2248 2252 2256 2260 2264 2268 2272 2276 2280 2284 2288 2292 2296 2304 2308 2312 2316 2320 2324 2328 2332 2336 2340 2344 2348 2352 2356 2360 2364 2368 2372 2376 2380 2384 2388 2392 2396 2400 2404 2408 2412 2416 2420 2424 2428 2432 2436 2440 2444 2448 2452 2456 2460 2464 2468 2472 2476 2480 2484 2488 2492 2496 2504 2508 2512 2516 2520 2524 2528 2532 2536 2540 2544 2548 2552 2556 2560 2564 2568 2572 2576 2580 2584 2588 2592 2596 2604 2608 2612 2616 2620 2624 2628 2632 2636 2640 2644 2648 2652 2656 2660 2664 2668 2672 2676 2680 2684 2688 2692 2696 2704 2708 2712 2716 2720 2724 2728 2732 2736 2740 2744 2748 2752 2756 2760 2764 2768 2772 2776 2780 2784 2788 2792 2796 2800 2804 2808 2812 2816 2820 2824 2828 2832 2836 2840 2844 2848 2852 2856 2860 2864 2868 2872 2876 2880 2884 2888 2892 2896 2904 2908 2912 2916 2920 2924 2928 2932 2936 2940 2944 2948 2952 2956 2960 2964 2968 2972 2976 2980 2984 2988 2992 2996

实验3实验思考第4题

C++
#include <iostream>
using namespace std;
int main()
{
    int peach = 1;
    for (int i = 1; i < 10; i++) {
        peach = (peach + 1) * 2;
    }
    cout << peach << endl;
    return 0;
}
Text Only
1534

实验3实验思考第5题

C++
#include <iostream>
using namespace std;
int main()
{
    int n, s = 0;
    cout << "输入n:";
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= i; j++)
        {
            s = s + j;
        }
    }
    cout << s << endl;
    main();
    return 0;
}
Text Only
1
2
3
4
5
6
输入n:10
220
输入n:56
30856
输入n:18
1140

实验3实验思考第6题

C++
#include <iostream>
using namespace std;
int main()
{
    for (int a = 1; a <= 9; a++) {
        for (int b = 0; b <= 9; b++) {
            for (int c = 0; c <= 9; c++) {
                for (int d = 0; d <= 9; d++) {
                    for (int e = 2; e <= 9; e++) {
                        if ((a * 1000 + b * 100 + c * 10 + d) * e == d * 1000 + c * 100 + b * 10 + a) {
                            cout << a << b << c << d << " " << e << endl;
                        }
                    }
                }
            }
        }
    }
    return 0;
}
Text Only
1089 9
2178 4
嵌套循环是个好东西,可能有些同学第一次碰到不太理解

还有另一种方法,就是算出回文数,然后相除判断结果是不是整数,即

C++
#include <iostream>
using namespace std;
int main()
{
    int a, b, d; double c, e; c = 1;
    for (a = 1000; a <= 9999; ++a)
    {
        d = a;
        while (d > 0)
        {
            b = d % 10;
            d /= 10;
            c = c * 10 + b;
        }
        e = (c - 10000) / a;
        c = 1;
        if (int(e) == e && e != 1)
            cout << "满足条件的四位数为:" << a << "  满足条件的e为:" << e << endl;
    }
    return 0;
}
该代码由侯睿咏同学友情提供。

cpp作业2答案参考

以下答案仅供参考,由cpp渣渣哀歌殇年编写
有错误欢迎指出,感谢!(QQ:2690034441)
当前版本:V1.1.0(2023/10/20)
历史版本:

版本号 日期 说明
V1.1.0 2023/10/20 替换了专题6-2的程序,大大简化了其代码
V1.0.0 2023/09/26 首次发布

重点提示:
1. 所有代码都经过上机验证,报错应该是你的问题
2. 算法不止一种,建议先自己写一遍程序再看答案,要不然没有太大意义

专题3-1

C++
#include <iostream>
using namespace std;
int main(){
    int a,b,c,d;
    cin>>a >> b;
    c=a/b;
    d=a%b;
    cout<<"商="<<c<<",余数="<<d;
return 0;
}

专题3-2

C++
#include <iostream>
using namespace std;
int main(){
    int a,b,c;
    cin>>a;
    b=a%10;
    c=a/100;
    cout<<c*100+b;
return 0;
}

专题3-3

C++
#include <iostream>
using namespace std;
int main(){
    char a,b,c;
    cin>>a;
    b=a-1;
    c=a+1;
    cout<<b<<","<<a<<","<<c;
return 0;
}

专题4-1

C++
#include<iostream>
using namespace std;
int main() {
    int a;
    cin >> a;
    if (a % 5 == 0 && a % 7 == 0)
        cout << "Yes";
    else
        cout << "No";
    return 0;
}

专题4-2

C++
#include<iostream>
#include<cmath>
using namespace std;
int main(){
    float a, b, c, p, s;
    cin >> a >> b >> c;
    if (a + b > c && a + c > b && b + c > a)
    {
        p = (a + b + c) / 2;
        s = sqrt(p *(p - a)*(p - b)*(p - c));
        cout << s;
    }
    else
        cout << "Data error!";
    return 0;
}

专题4-3

C++
#include<iostream>
using namespace std;
int main() {
    float score;
    cin >> score;
    if (score >= 0 && score <= 100) {
        switch (int(score) / 10) {
        case 10:cout << "A" << endl; break;
        case 9:cout << "A" << endl; break;
        case 8:cout << "B" << endl; break;
        case 7:cout << "C" << endl; break;
        case 6:cout << "D" << endl; break;
        default:cout << "E" << endl;
        }
    }
    else
        cout << "输入数据出错";
    return 0;
}

专题5-1

方法一:懒人法 缺点:占内存

C++
#include<iostream>
using namespace std;
int main() {
    int a = 1, b = 2, c = 3, n;
    float s = 0;
    cin >> n;
    while (a <= n) {
        s += 1.0 / (a * b * c);
        a++; b++; c++;
    }
    cout << "s=" << s;
    return 0;
}
方法二:常规法 缺点:估计大部分人都是这么做的,千篇一律
C++
#include<iostream>
using namespace std;
int main() {
    int n;
    float s = 0;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        s += 1.0 / (i * (i + 1) * (i + 2));
    }
    cout << "s=" << s;
    return 0;
}
方法三:都说了学好编程需要数学基础法
C++
#include<iostream>
using namespace std;
int main() {
    int n;
    float s = 0;
    cin >> n;
    s = 0.25 - 0.5 / (n + 1) + 0.5 / (n + 2);
    cout << "s=" << s;
    return 0;
}

专题5-2

C++
#include <iostream>
using namespace std;
int main() {
    int num, digit, sum = 0;
    cin >> num;
    while (num > 0) {
        digit = num % 10; 
        sum += digit * digit;
        num /= 10; 
    }
    cout << "sum=" << sum <<endl;
    return 0;
}

专题6-1

C++
#include <iostream>
using namespace std;
int main(){
    int n;
    cin >> n;
    double pi=1, a, b;
    for(int m = 1;m <= n;m++){
        a = 4 * m * m;
        b = a - 1;
        pi = pi * a / b;
    }
    pi = pi * 2;
    cout << pi;
    return 0;
}

专题6-2

C++
#include <iostream>
using namespace std;
int main()
{
    int n, j, i, sum = 0, a;
    cin >> n;
    for (i = 1; i < n; i++)
    {
        a = i * (i + 1) - 1;
        for (j = 2; j <= a; j++) {
            if (a % j != 0 && j == a - 1)sum += a;
            if (a % j == 0)break;

        }
    }
    cout << sum << endl;
    return 0;
}

cpp作业3答案参考

以下答案仅供参考,由cpp渣渣哀歌殇年编写
有错误欢迎指出,感谢!(QQ:2690034441)
当前版本:V1.0.0(2023/11/1)
历史版本:

版本号 日期 说明
V1.0.1 2023/11/16 略微修改了专题7-3的程序
V1.0.0 2023/11/1 首次发布

重点提示:
1. 所有代码都经过上机验证,报错应该是开发环境问题或者非代码问题
2. 算法不止一种,建议先自己写一遍程序再看答案,要不然没有太大意义
3. 由于从函数开始,有些难度了,所以不再只给出答案,还给出相关思路

专题7-1

  定义函数long f(long n),求正整数n的各位数字的平方和。在主函数中输入三个正整数k、a、b,并调用f函数计算有多少个正整数n满足a≤n≤b,且k×f(n)=n。
  看到这道题,先不看后面一堆说了什么,先看题目要求的函数long f(long n)是如何构造的,该函数的作用是计算正整数n的各位数字平方和。每一位数字“提取”出来的方法还是老办法,用取余和除法就行了:

C++
1
2
3
4
5
6
7
8
long f(long n) {
    int sum = 0;
    while (n > 0) {
        sum += (n % 10) * (n % 10);
        n /= 10;
    }
    return sum;
}
  再用while整一个循环,把每一位的平方相加到sum变量里,至于while的条件不唯一,n>0或者n!=0或者其它合理的条件均可。返回sum,函数就构造完了。但是值得注意的是,为了调试方便需要,建议写好一个函数后就试着简单调用一遍,看看输出是否有问题。
  接下来看题目后半部分:在主函数中输入三个正整数k、a、b,并调用f函数计算有多少个正整数n满足a≤n≤b,且k×f(n)=n。除了题目要求的定义三个整数型变量k,a,b外,再定义一个用于计算满足条件个数的变量s,初始化为0。
C++
int k, a, b, s = 0;
cin >> k >> a >> b;
  然后我们可以遍历符合要求的值,条件有两个:a≤n≤b,且k×f(n)=n,这时可能有同学就想了,条件是不是要写成:
C++
if (a <= n && n <= b && k * f(n) == n)s++;
  虽然这样写也可以,但是我们可以取个巧:既然a≤n≤b,我们遍历时从a开始,把a直接赋给循环变量即可,而b作为循环条件,即:
C++
1
2
3
for (int n = a; n <= b; n++) {
    if (k * f(n) == n)s++;
}
  然后整合一下,就大功告成了:
C++
#include<iostream>
using namespace std;
long f(long n) {
    int sum = 0;
    while (n > 0) {
        sum += (n % 10) * (n % 10);
        n /= 10;
    }
    return sum;
}
int main()
{
    int k, a, b, s = 0;
    cin >> k >> a >> b;
    for (int n = a; n <= b; n++) {
        if (k * f(n) == n)s++;
    }
    cout << s;
    return 0;
}
  值得注意的是,如果你的函数long f(long n)写在了主函数后面,别忘了在顶部声明。这里顺带废话几句:
  其实我在学编程语言时,函数声明操作其实不叫声明,而是叫提升,把后面的函数提升到了前面,这是从形象角度定义的;而声明是在编译时告诉编译器确实有这么个函数,在前面声明一下,这是从实质角度定义的。无论哪种叫法,能够理解就行。

专题7-2

  根据下列公式,编写函数double pai(double e)求π/2的值,直到累加项的值小于给定精度e为止。在主函数中输入e的值,并调用该函数并输出π的值。
  公式就不放了,值得一说的是这道题就构造函数略有些难,重点要理解题意,多次调试发现错误修改即可,我用的是嵌套循环,就不赘述了:

C++
#include<iostream>
using namespace std;
double pai(double e) {
    double k = 1, sum = 0;
    for (int i = 1; k >= e; i++) {
        sum += k;
        k = 1;
        for (int j = 1; j <= i; j++)k *= j * 1.0 / (j * 2 + 1);
    }
    return sum * 2;
}
int main()
{
    double e;
    cin >> e;
    cout << pai(e);
    return 0;
}

专题7-3

  这道题简单到炸,只要理解了书上的东西,闭着眼都能做,下面是参考代码:

C++
#include<iostream>
using namespace std;
int fib(int n) {
    if (n > 1)return fib(n - 2) + fib(n - 1);
    return n;
}
int main()
{
    int n;
    cin >> n;
    cout << fib(n);
    return 0;
}

cpp作业4答案参考

以下答案仅供参考,由cpp渣渣哀歌殇年编写
有错误欢迎指出,感谢!(QQ:2690034441)
当前版本:V1.1.0(2023/11/11)
历史版本:

版本号 日期 说明
V1.1.0 2023/11/11 修改了一些存在的问题,优化了代码
V1.0.0 2023/11/2 首次发布

重点提示:
1. 所有代码都经过上机验证,报错应该是开发环境问题或者非代码问题
2. 算法不止一种,建议先自己写一遍程序再看答案,要不然没有太大意义

实验5实验内容第2题

C++
#include<iostream>
using namespace std;
int IsPrime(int n);
int main()
{
    int m, k;
    for (m = 100; m <= 200; m += 2) {
        for (k = 3; k <= m / 2; k += 2)
            if ((IsPrime(k) == 1) && (IsPrime(m-k) == 1))
                cout << m << "=" << k << "+" << m - k << endl;
    }
    return 0;
}
int IsPrime(int n) {
    int i;
    for (i = 2; i <= n / 2; i++)
        if (n % i == 0)return 0;
    return 1;
}
Text Only
100=3+97
100=11+89
100=17+83
100=29+71
100=41+59
100=47+53
102=5+97
102=13+89
102=19+83
102=23+79
……
198=67+131
198=71+127
198=89+109
198=97+101
200=3+197
200=7+193
200=19+181
200=37+163
200=43+157
200=61+139
200=73+127
200=97+103

实验5实验内容第3题

C++
#include<iostream>
using namespace std;
int f(int n) {
    int s = 0;
    if (n == 1)s = 1;
    else if (n % 2 == 0)s = f(n / 2);
    else s = f((n - 1) / 2) + f((n - 1) / 2 + 1);
    return s;
}
int main()
{
    int n;
    do {
        cin >> n;
    } while (n < 1);
    cout << "f" << n << "=" << f(n) << endl;
    return 0;
}
Text Only
1
f1=1
2
f2=1
3
f3=2
4
f4=1
5
f5=3
6
f6=2
7
f7=3
8
f8=1
9
f9=4
10
f10=3

实验5实验思考第2题

多么精巧!
有一说一我这个代码慎用,个人写代码的风格的太明显了

C++
#include<iostream>
#include<cmath>
using namespace std;
bool p(int a) {
    for (int i = 2; i <= sqrt(a); i++)if (a % i == 0)return false;
    return true;
}
int mp(int a, int b) {
    while (1) {
        if (p(a))return a;
        a += b;
    }
}
int main()
{
    cout << "min:" << mp(1000, 1) << " max:" << mp(2000, -1);
    return 0;
}
Text Only
min:1009 max:1999

实验5实验思考第3题

C++
#include<iostream>
using namespace std;
int h(int a) {
    int b = 0;
    while (a > 0) {
        b += a % 10;
        b *= 10;
        a /= 10;
    }
    return b / 10;
}
int h3(int m) {
    if (m == h(m) && m * m == h(m * m) && m * m * m == h(m * m * m)) {
        return m;
    }
    else {
        return 0;
    }
}
int main()
{
    for (int i = 11; i <= 999; i++) {
        if (h3(i) != 0)cout << i << endl;
    }
    return 0;
}
Text Only
1
2
3
11
101
111

实验5实验思考第4题

C++
#include<iostream>
using namespace std;
double p(double x, int n) {
    if (n == 0) {
        return 1;
    }
    else if (n > 0) {
        return x * p(x, n - 1);
    }
}
int main()
{
    int x, n;
    cin >> x >> n;
    cout << p(x, n);
    return 0;
}
Text Only
1
2
3
4
5
6
5 6
15625
2 3
8
7 4
2401

cpp作业5答案参考

以下答案仅供参考,由cpp渣渣哀歌殇年编写
有错误欢迎指出,感谢!(QQ:2690034441)
当前版本:V1.0.0(2023/11/11)
历史版本:

版本号 日期 说明
V1.0.0 2023/11/11 首次发布

重点提示:
1. 所有代码都经过上机验证,报错应该是开发环境问题或者非代码问题
2. 算法不止一种,建议先自己写一遍程序再看答案,要不然没有太大意义

实验6实验内容第4题

C++
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    int a[6][6], i, j;
    for (i = 0; i < 6; i++) {
        for (j = 0; j < 6; j++) {
            if (i == j || j == 0)a[i][j] = 1;
            else if (i < j)a[i][j] = j - i + 1;//填a[i][j - 1] + 1也行
            else a[i][j] = a[i - 1][j] + a[i - 1][j - 1];
            cout << setw(6) << a[i][j];
        }
        cout << '\n';
    }
    return 0;
}

实验6实验内容第5题

C++
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    int i, j, a[5][5];
    for (i = 0; i < 5; i++) {
        a[i][0] = 1; a[i][i] = 1;
        for (j = 1; j < i; j++)a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
    }
    for (i = 0; j < 5; i++) {
        cout << "\n";
        for (j = 0; j <= i; j++)cout << setw(6) << a[i][j];
    }
    cout << "\n";
    return 0;
}

实验6实验思考第1题

C++
#include<iostream>
using namespace std;
int lookup(int x[], int n, int y) {
    for (int i = 0; i <= n - 1; i++)if (x[i] == y)return i;
    return -1;
}
int main()
{
    int x[10] = { 5,4,1,2,8,4,1,6,2,0 }, n, y;
    cin >> n >> y;
    cout << "在数组中的前" << n << "个元素中寻找的" << y << "的下标为:" << lookup(x, n, y);
    return 0;
}
Text Only
1
2
3
4
5
6
7
8
3 4
在数组中的前3个元素中寻找的4的下标为:1
7 4
在数组中的前7个元素中寻找的4的下标为:1
2 8
在数组中的前2个元素中寻找的8的下标为:-1
10 8
在数组中的前10个元素中寻找的8的下标为:4

实验6实验思考第2题

C++
#include<iostream>
using namespace std;
int max(int x[], int n) {
    int max = -2147483648;
    for (int i = 0; i <= n - 1; i++)if (max < x[i])max = x[i];
    return max;
}
int main()
{
    int x[10]{ -14,5,22,4,12,9992,5,-451,77,1 }, n;
    cin >> n;
    cout << max(x, n);
    return 0;
}
Text Only
1
2
3
4
5
22
8
9222

实验7实验内容第2题

C++
#include<iostream>
using namespace std;
void f(float a, float b, float* area, float& p) {
    *area = a * b;
    p = 2 * (a + b);
}
int main()
{
    float l, w, x, y;
    cout << "\nPlease input l and w:";
    cin >> l >> w;
    f(l, w, &x, y);
    cout << "\n 面积=" << x << "\n周长=" << y;
    cout << '\n';
    return 0;
}

实验7实验内容第3题

C++
#include<iostream>
#include<iomanip>
using namespace std;
int table[10];
void lookup(int t[], int n, int* min, int* max) {
    int k;
    *min = t[0]; *max = t[0];
    for (k = 1; k < n; k++) {
        if (t[k] < *min)*min = t[k];
        if (t[k] > *max)*max = t[k];
    }
}
int main()
{
    int k, a, b;
    for (k = 0; k < 10; k++) cin >> table[k];
    lookup(table, 10, &a, &b);
    cout << "\nmin=" << a << "\nmax" << b;
    return 0;
}
Text Only
1
2
3
55 41 7 20 54 99 54 77 10 26
min=7
max=99

实验7实验内容第4题

C++
#include<iostream>
using namespace std;
int main()
{
    char a[80];
    int i, j;
    cout << "Input a string:\n";
    cin.getline(a, 80, '\n');
    for (i = j = 0; a[i]; i++) if (!(a[i] >= '0' && a[i] <= '9'))a[j++] = a[i];
    a[j] = '\0';
    cout << "Then new string is:\n";
    cout << a << endl;
    return 0;
}

实验7实验思考第1题

C++
#include<iostream>
using namespace std;
void invent(int a[], int n) {
    int* start = a;
    int* end = a + n - 1;
    while (start < end) {
        int temp = *start;
        *start = *end;
        *end = temp;
        start++;
        end--;
    }
}
int main()
{
    int a[10]{ 5,4,2,7,8,9,3,1,6,0 };
    invent(a, 10);
    for (int i = 0; i < 10; i++) cout << a[i];
    return 0;
}

实验7实验思考第3题

C++
#include <iostream>
using namespace std;
int* lookup(int t[], int n, int val) {
    for (int i = 0; i < n; ++i) if (t[i] == val) return &t[i];
    return NULL;
}
int main() {
    int a[10] = { 5,4,8,5,6,9,2,1,5,1 }, val;
    cin >> val;
    cout << lookup(a, 10, val);
    return 0;
}

cpp作业6答案参考

以下答案仅供参考,由cpp渣渣哀歌殇年编写
有错误欢迎指出,感谢!(QQ:2690034441)
当前版本:V1.0.0(2023/11/13)
历史版本:

版本号 日期 说明
V1.0.0 2023/11/13 首次发布

重点提示:
1. 所有代码都经过上机验证,报错应该是开发环境问题或者非代码问题
2. 算法不止一种,建议先自己写一遍程序再看答案,要不然没有太大意义

专题8-1

  这里只要max足够小,min足够大就可以了,如果这类题使用了极大或极小(负数)的测试数据可以改成变量类型的最大最小范围。

C++
#include<iostream>
using namespace std;
int main()
{
    int num[6], max = 0, min = 1000, maxi, mini;
    for (int i = 0; i < 6; i++) {
        cin >> num[i];
        if (num[i] >= max) { 
            max = num[i];
            maxi = i;
        }
        if (num[i] <= min) { 
            min = num[i];
            mini = i;
        }
    }
    num[mini] = max;
    num[maxi] = min;
    for (int i = 0; i < 6; i++)cout << num[i] << endl;
    return 0;
}

专题8-2

  主打一个逆向思维doge

C++
#include<iostream>
using namespace std;
int main()
{
    int num[3][3], sum = 0;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            cin >> num[i][j];
            if (!(i == 1 && j == 1))sum += num[i][j];
        }
    }
    cout << "S=" << sum;
    return 0;
}
  个人觉得这道题用数组感觉很牵强,不用数组也能解决,而且内存占用更少:
C++
#include<iostream>
using namespace std;
int main()
{
    int num, sum = 0;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            cin >> num;
            if (!(i == 1 && j == 1))sum += num;
        }
    }
    cout << "S=" << sum;
    return 0;
}

专题8-3

C++
#include<iostream>
using namespace std;
int find(int a[], int n, int x) {
    for (int i = 0; i < n; i++)if (a[i] == x) return i;
    return -1;
}
int main()
{
    int a[10], n, x;
    cin >> n;
    for (int i = 0; i < n; i++)cin >> a[i];
    cin >> x;
    int m = find(a, n, x);
    if (m == -1) {
        cout << "Not Found";
    }
    else {
        for (int i = 0; i < n - 1; i++) {
            if (i >= m) {
                a[m] = a[m + 1]; 
                m++;
            }
            cout << a[i] << endl;
        }
    }
    return 0;
}

专题9-1

C++
#include<iostream>
#include<cmath>
using namespace std;
void isprime(int n, int* f) {
    *f = 1;
    for (int i = 2; i < sqrt(n); i++)if (n % i == 0)*f = 0;
}
int main()
{
    int num, p;
    cin >> num;
    isprime(num, &p);
    if (p) cout << num / 100 + num / 10 % 10 * 10 + num % 10 * 100;
    else cout << num / 100 * 100 + num / 10 % 10 + num % 10 * 10;
    return 0;
}

专题9-2

C++
#include <iostream>
using namespace std;
void bubbleSort(double x[], int n) {
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (x[j] > x[j + 1]) {
                double temp = x[j];
                x[j] = x[j + 1];
                x[j + 1] = temp;
            }
        }
    }
}
double median(double x[], int n) {
    bubbleSort(x, n);
    if (n % 2 != 0) return x[n / 2];
    return (x[n / 2 - 1] + x[n / 2]) / 2.0;
}
int main() {
    int n;
    cin >> n;
    double* x = new double[n];
    for (int i = 0; i < n; i++) {
        cin >> x[i];
    }
    cout << median(x, n) << endl;
    delete[] x;
    return 0;
}
另一种函数也是一样的,只是把函数头替换即可。

专题10-1

方法一:字符串函数法(懒人法)

C++
#include <iostream>
#include <string>
using namespace std;
int main() {
    string inString, newString;
    int m;
    cin >> inString >> m;
    newString = inString.substr(m - 1);
    cout << newString << endl;
    return 0;
}
方法二:字符数组法(手搓法)
C++
#include <iostream>
using namespace std;
int main() {
    char inString[30], newString[30];
    int m;
    cin >> inString >> m;
    for (int i = --m; i < 30; i++) {
        newString[i - m] = inString[i];
    }
    cout << newString << endl;
    return 0;
}

专题10-2

方法一:字符串函数法(内置库yyds)

C++
#include <iostream>
#include <string>
using namespace std;
int main() {
    string inString;
    int m;
    cin >> inString >> m;
    inString = inString.substr(m) + inString.substr(0, m);
    cout << inString;
    return 0;
}
方法二:字符数组法(除了显得字多感觉不如前者)
C++
#include <iostream>
using namespace std;
void moveString(char str[], int m) {
    int length = 0;
    while (str[length] != '\0') {
        length++;
    }
    for (int i = 0; i < m; i++) {
        char temp = str[0];
        for (int j = 0; j < length - 1; j++) {
            str[j] = str[j + 1];
        }
        str[length - 1] = temp;
    }
}
int main() {
    char inString[20];
    int m;
    cin >> inString >> m;
    moveString(inString, m);
    cout << inString << endl;
    return 0;
}
建议10-1用方法一,10-2用方法二,因为这样显得既会用内置库函数,又会手搓函数,懂得多还技术好doge

cpp作业8答案参考

以下答案仅供参考,由cpp渣渣哀歌殇年编写
有错误欢迎指出,感谢!(QQ:2690034441)
当前版本:V1.0.0(2023/12/7)
历史版本:

版本号 日期 说明
V1.1.0 2023/12/18 采纳了谭智勋同学的意见,修正了思考题的程序
V1.0.0 2023/12/7 首次发布

重点提示:
1. 所有代码都经过上机验证,报错应该是开发环境问题或者非代码问题
2. 算法不止一种,建议先自己写一遍程序再看答案,要不然没有太大意义
3. 其实这些程序的答案在书上有,下面是我自己写的

实验9-1

C++
#include <iostream>
using namespace std;
class fac {
    int num;
public:
    fac(int n) {
        num = n;
    }
    double cal() {
        double s = 1;
        for (int i = 1; i <= num; i++)s *= i;
        return s;
    }
};
int main() {
    int num;
    cin >> num;
    fac f1(num);
    cout << f1.cal();
    return 0;
}

实验9-2

C++
#include <iostream>
using namespace std;
class Box {
    double a, b, c;
public:
    Box(int x, int y, int z) {
        a = x; b = y; c = z;
    }
    double C() {
        return (a * b + a * c + b * c) * 2;
    }
    double S() {
        return a * b * c;
    }
};
int main() {
    int a, b, c;
    cin >> a >> b >> c;
    Box b1(a, b, c);
    cout << b1.C() << " " << b1.S();
    return 0;
}

实验9-3

C++
#include <iostream>
using namespace std;
class Counter {
    int num;
public:
    Counter(int a = 0) {
        num = a;
    }
    void add(int a = 1) {
        num += a;
    }
    void sub(int a = 1) {
        num -= a;
    }
    int out() {
        return num;
    }
};
int main() {
    int num, a, f = 1;
    char p;
    cout << "请输入计数器初值:";
    cin >> num;
    Counter c1(num);
    cout << "a 增值,s 减值,o 输出,k 结束。增值和减值操作需要参数,即增减的量" << endl;
    while (f) {
        cout << ">> ";
        cin >> p;
        switch (p) {
        case 'a':cin >> a; c1.add(a); break;
        case 's':cin >> a; c1.sub(a); break;
        case 'o':cout << c1.out() << endl; break;
        case 'k':f = 0; break;
        default:cout << "操作码有误!" << endl;
        }
    }
    return 0;
}

实验9-4

C++
#include <iostream>
using namespace std;
class Sample {
    double a, b;
public:
    Sample(double x, double y) {
        a = x; b = y;
    }
    void mult(Sample& s) {
        double ta = a * s.a - b * s.b;
        double tb = a * s.b + b * s.a;
        a = ta; b = tb;
    }
    void out() {
        cout << a;
        if (b > 0)cout << "+";
        cout << b << "i" << endl;
    }
};
int main() {
    double a1, a2, b1, b2;
    cin >> a1 >> a2 >> b1 >> b2;
    Sample s1(a1, a2), s2(b1, b2);
    s1.mult(s2);
    s1.out();
    return 0;
}

实验10-1

C++
#include <iostream>
#include <cmath>
using namespace std;
class point {
    double x, y, z;
public:
    point(int a, int b, int c) {
        x = a; y = b; z = c;
    }
    friend double tpd(point& p1, point& p2);
};
inline double p(double a, double b) {
    return (a - b) * (a - b);
}
double tpd(point& p1, point& p2) {
    return sqrt(p(p1.x, p2.x) + p(p1.y, p2.y) + p(p1.z, p2.z));
}
int main() {
    double x, y, z;
    cout << "依次输入两点x,y,z值:";
    cin >> x >> y >> z;
    point p1(x, y, z);
    cin >> x >> y >> z;
    point p2(x, y, z);
    cout << "两点距离为:" << tpd(p1, p2);
    return 0;
}

实验10-2

C++
#include <iostream>
using namespace std;
class tpd {
    double x1, y1, x2, y2;
public:
    tpd(double a1, double a2, double b1, double b2) {
        x1 = a1; y1 = b1;
        x2 = a2; y2 = b2;
    }
    double dis() {
        return x1 * y1 + x2 * y2;
    }
};
int main() {
    double x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    tpd t1(x1, y1, x2, y2);
    cout << t1.dis();
    return 0;
}

实验10-3

C++
#include <iostream>
#include <string>
using namespace std;
class Student {
    int n;
    string* name;
    int* id;
    float* score;
public:
    Student(int x) {
        n = x;
        name = new string[n];
        id = new int[n];
        score = new float[n];
    }
    ~Student() {
        delete[]name, id, score;
    }
    void in() {
        cout << "学号\t姓名\t成绩" << endl;
        for (int i = 0; i < n; i++)cin >> id[i] >> name[i] >> score[i];
    }
    void out() {
        cout << "学号\t姓名\t成绩" << endl;
        for (int i = 0; i < n; i++)cout << id[i] << "\t" << name[i] << "\t" << score[i] << endl;
    }
    float avg() {
        float sum = 0;
        for (int i = 0; i < n; i++)sum += score[i];
        return sum / n;
    }
};
int main() {
    int N;
    cout << "请输入N:";
    cin >> N;
    Student s(N);
    s.in();
    cout << "平均分:" << s.avg() << endl;
    s.out();
    return 0;
}

实验10-4

C++
#include <iostream>
using namespace std;
inline int year_r(int year) {
    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)return 1;
    return 0;
}
int day_t[2][12]{
        {31,28,31,30,31,30,31,31,30,31,30,31},
        {31,29,31,30,31,30,31,31,30,31,30,31}
};
int year_t[2]{ 365,366 };
class Date {
    int year, month, day, r_day;
public:
    Date(int y, int m, int d) {
        int f = 0;
        if (m > 12 || m < 1) { m = 1; f = 1; }
        if (d > day_t[year_r(y)][m - 1] || d < 1) { d = day_t[year_r(y)][m - 1]; f = 1; }
        year = y; month = m; day = d;
        if (f) { cout << "使用了不合法的日期,已修正!修正后的日期为:"; out(); cout << endl; }
        r_day = 0; r_ds();
    }
    void r_ds() {
        for (int j = 0; j < month - 1; j++)r_day += day_t[year_r(year)][j];
        r_day += day;
    }
    void out() {
        cout << year << "-" << month << '-' << day;
    }
    friend int day_s(Date& d1, Date& d2);
};
int day_s(Date& d1, Date& d2) {
    int sum = 0;
    for (int y = d1.year; y < d2.year - 1; y++)sum += year_t[year_r(y)];
    if (d1.month > d2.month) sum += year_t[year_r(d1.year)] - d1.r_day + d2.r_day;
    else sum += d2.r_day - d1.r_day;
    return sum;
}
int main() {
    int y, m, d;
    cout << "请输入日期1:";
    cin >> y >> m >> d;
    Date da1(y, m, d);
    cout << "请输入日期2:";
    cin >> y >> m >> d;
    Date da2(y, m, d);
    da1.out();cout << "与";da2.out();cout << "相差" << day_s(da1, da2) << "天" << endl;
    return 0;
}

思考题

程序1(普通成员函数):

C++
#include <iostream>
using namespace std;
class sub {
    double y = 0, n;
public:
    void cal(double _y) {
        for (n = 1; y < _y; n++)y += 2.0 / ((4 * n - 3) * (4 * n - 1));
        y -= 2.0 / ((4 * n - 3) * (4 * n - 1)); n -= 2;
    }
    void out() {
        cout << "y=" << y << " n=" << n;
    }
};
int main() {
    sub s1;
    s1.cal(0.78);
    s1.out();
    return 0;
}
程序2(构造函数);
C++
#include <iostream>
using namespace std;
class sub {
    double y, n;
public:
    sub(double _y) {
        y = 0;
        for (n = 1; y < _y; n++)y += 2.0 / ((4 * n - 3) * (4 * n - 1));
        y -= 2.0 / ((4 * n - 3) * (4 * n - 1)); n -= 2;
    }
    void out() {
        cout << "y=" << y << " n=" << n;
    }
};
int main() {
    sub s1(0.78);
    s1.out();
    return 0;
}
程序3(友元函数):
C++
#include <iostream>
using namespace std;
class sub {
    double y = 0, n;
public:
    void out() {
        cout << "y=" << y << " n=" << n;
    }
    friend void cal(double _y,sub &s);
};
void cal(double _y,sub &s) {
    for (s.n = 1; s.y < _y; s.n++)s.y += 2.0 / ((4 * s.n - 3) * (4 * s.n - 1));
    s.y -= 2.0 / ((4 * s.n - 3) * (4 * s.n - 1)); s.n -= 2;
}
int main() {
    sub s1;
    cal(0.78, s1);
    s1.out();
    return 0;
}

cpp作业9答案参考

以下答案仅供参考,由cpp渣渣哀歌殇年编写
有错误欢迎指出,感谢!(QQ:2690034441)
当前版本:V1.0.0(2023/12/8)
历史版本:
|版本号|日期|说明| |-|-|-| |V1.0.0|2023/12/8|首次发布|

重点提示:
1. 所有代码都经过上机验证,报错应该是开发环境问题或者非代码问题
2. 算法不止一种,建议先自己写一遍程序再看答案,要不然没有太大意义

慕课课程期末考试(编程题)

  1. 数列求和

    C++
    #include<iostream>
    using namespace std;
    int main()
    {
        double n, b = 1, y = 0;
        cin >> n;
        for (int i = 1; i <= n; i++) {
            b *= i;
            y += 1 / b;
        }
        cout << "Y=" << y;
        return 0;
    }
    

  2. 完美素数

    C++
    #include<iostream>
    #include<cmath>
    using namespace std;
    bool isPrime(int num) {
        if (num <= 1 && num >= -1)return false;
        for (int i = 2; i <= sqrt(fabs(num)); i++)if (num % i == 0)return false;
        return true;
    }
    int nand(int num) {
        int sum = 0;
        while (num) {
            sum += num % 10;
            num /= 10;
        }
        return sum;
    }
    int main()
    {
        int n, sum = 0;
        cin >> n;
        for (int i = 1; i <= n; i++)if (isPrime(i) && isPrime(nand(i)))sum++;
        cout << sum;
        return 0;
    }
    

  3. 最值

    C++
    #include<iostream>
    using namespace std;
    void numt(int num[], int ai, int bi) {
        int t = num[ai];
        num[ai] = num[bi];
        num[bi] = t;
    }
    int main()
    {
        int num[10];
        for (int i = 0; i < 10; i++)cin >> num[i];
        int max = num[1], min = num[1], maxi = 1, mini = 1;
        for (int i = 2; i < 9; i++) {
            if (num[i] > max) {
                max = num[i]; 
                maxi = i; 
            }
            if (num[i] < min) {
                min = num[i];
                mini = i;
            }
        }
        numt(num, 0, maxi);
        numt(num, 9, mini);
        cout << num[0];
        for (int i = 1; i < 10; i++)cout << "," << num[i];
        return 0;
    }