C++期末复习代码总结(笔记二)

2021/6/21 14:26:03

本文主要是介绍C++期末复习代码总结(笔记二),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

(1)统计从键盘上输入的字符中的数字字符的个数,用换行符结束循环。

#include <iostream>
using namespace std;
int main()
{
    int n = 0, c;
    c = getchar();
    while (c != '\n')
    {
        if (c >= '0' && c <= '9')
            n++;
        c = getchar();
    }
    cout << n << endl;
    return 0;
}

(2)

#include <stdio.h>
int main()
{
    int y = 10;
    do
    {
        y--;
    } while (--y);
    printf("%d\n", y--);
}

(3)

#include <stdio.h>
int main()
{
    int i, j, x = 0;
    for (i = 0; i < 2; i++)
    {
        x++;
        for (j = 0; j <= 3; j++)
        {
            if (j % 2)
                continue;
            x++;
        }
        x++;
    }
    printf("x=%d\n", x);
}

(4)

#include <stdio.h>
int main()
{
    int i = 1, s = 3;
    do
    {
        s += i++;
        if (s % 7 == 0)
            continue;
        else
            ++i;
    } while (s < 15);
    printf("%d", i);
}

(5)求有多少种方法可将1.0元人民币兑换成1分,2分,5分,1角,2角,5角。

#include <iostream>
using namespace std;
int main()
{
    int yf, ef, wf, yj, ej, wj, count;
    for (yf = 0; yf <= 100; yf++)
        for (ef = 0; ef <= 50; ef++)
            for (wf = 0; wf <= 20; wf++)
                for (yj = 0; yj <= 10; yj++)
                    for (ej = 0; ej <= 5; ej++)
                        for (wj = 0; wj <= 2; wj++)
                            if (yf + 2 * ef + 5 * wf + 10 * yj + 20 * ej + 50 * wj == 100)
                                count++;
    cout << count << endl;
    return 0;
}

(6)蝴蝶
无

#include <iostream>
using namespace std;
int main()
{
    int i, j, n;
    cin >> n;
    for (i = -(n / 2); i <= (n / 2); i++)
    {
        for (j = 0; j <= abs(i); j++)
            cout << " ";
        for (j = 0; j < 2 * ((n / 2 + 1) - abs(i)) - 1; j++)
            cout << "*";
        for (j = 0; j < 2 * abs(i); j++)
            cout << " ";
        cout << '\b';
        for (j = 0; j < 2 * ((n / 2 + 1) - abs(i)) - 1; j++)
            cout << "*";
        cout << endl;
    }
    return 0;
}

(7)数字金字塔

#include <iostream>
using namespace std;
int main()
{
    int i, j, k, h, n;
    cin >> n;
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= n - i; j++)
            cout << "  ";
        for (k = 1; k <= n - j + 1; k++)
            cout << k << " ";
        for (h = k - 1; h >= 1; h--)
        {
            if (h != (k - 1))
                cout << h << " ";
        }
        cout << endl;
    }
    return 0;
}

(8)找出方阵中每列中的最小元素及其所在的行号,并将这些最小元素中的最大值与最小值及其行列号输出。

#include <iostream>
using namespace std;
int main()
{
    int a[3][4] = {{1, 2, 3, 4}, {9, 8, 7, 6}, {-1, -2, 0, 5}};
    int i, j;
    int b[4][2] = {{-1, 2}, {-2, 2}, {0, 2}, {4, 0}};
    int min, max, min_j, max_j, ii;
    for (j = 0; j < 4; j++)
    {
        min = a[0][j];
        ii = 0;
        for (i = 0; i < 3; i++)
            if (a[i][j] < min)
            {
                min = a[i][j];
                ii = i;
            }
        b[j][0] = min;
        b[j][1] = ii;
    }
    for (i = 0; i < 4; i++)
    {
        for (j = 0; j < 2; j++)
            cout << b[i][j] << "   ";
        cout << endl;
    }

    min = max = b[0][0];
    min_j = max_j = 0;
    for (i = 0; i < 4; i++)
    {
        if (b[i][0] < min)
        {
            min = b[i][0];
            min_j = i;
        }
        if (b[i][0] > max)
        {
            max = b[i][0];
            max_j = i;
        }
    }
    cout << "min:" << min << " " << b[min_j][1] << " " << min_j << endl;
    cout << "min:" << b[min_j][0] << " " << b[min_j][1] << " " << min_j << endl;
    cout << "max:" << max << " " << b[max_j][1] << " " << max_j << endl;
    cout << "max:" << b[max_j][0] << " " << b[max_j][1] << " " << max_j << endl;
    return 0;
}

(9)随机产生15个互不重复的介于0~19之间的随机整数存入数组中。

#include <iostream>
#include <time.h>
#include "stdlib.h"
using namespace std;
int main()
{
    int i, j, x;
    int a[15];
    srand((unsigned)time(NULL));    
    for (i = 0; i < 15; i++)
    {
        x = rand() % 20;
        for (j = 0; j < i; j++)
            while (x == a[j])          //如果x与前面的数相同,则再找一个数
            {
                x = rand() % 20;
                j = 0;
            }
        a[i] = x;
    } 
    for (i = 0; i < 15; i++)
    {
        cout << a[i] << "  ";
    }
}
#include <iostream>
#include "stdlib.h"
#include <time.h>
using namespace std;
int main()
{
    int i, x;
    int a[15];
    bool f[20] = {0};
    for (i = 0; i < 15; i++)
    {
        x = rand() % 20;
        if (f[x])
        {
            i--;
            continue;
        }
        a[i] = x;
        f[x] = 1;
    }
    for (i = 0; i < 15; i++)
    {
        cout << a[i] << "  ";
    }
}


这篇关于C++期末复习代码总结(笔记二)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程