完成猜数字游戏并记录猜测过程中的数字,并记录数字,包含对问题的分析和程序设计思路,各模块功能设计和函数说明

img

完成猜数字游戏并记录猜测过程中的数字,并记录数字,包含对问题的分析和程序设计思路,各模块功能设计和函数说明

你题目的解答代码如下:

#include<iostream>
#include <time.h>
using namespace std;

void sort(int a[],int n)
{
    int i,j;
    for(i=0;i<n-1;i++)
        for(j=0;j<n-i-1;j++)
            if(a[j] > a[j+1])
            {
                int t = a[j];
                a[j] = a[j+1];
                a[j+1] = t;
            }
}

int main()
{
    int a[100] = {0};
    int b[100] = {0};
    int c[1000];
    int n,d,i,j,t,k,u,g=0;
    srand((unsigned)time(NULL));
    for (i = 0; i < 10; i++)
    {
        d = rand()%1000+1;
        t = d;
        k = 0;
        do {
            a[k++] = t%10;
            t /= 10;
        } while (t>0);
        // cout << d << endl;
        cout << "第" << i+1 << "个生成的数值位数为:" << k << endl;
        while (1)
        {
            cout << "输入猜测值:";
            cin >> n;
            c[g++] = n;
            t = n;
            u = 0;
            do {
                b[u++] = t%10;
                t /= 10;
            } while (t>0);
            if (u!=k)
            {
                cout << "输入的猜测值位数不对" << endl;
                continue;
            }
            if (n==d)
            {
                cout << "猜测值正确" << endl;
                break;
            }
            cout << "猜测值中X的是不正确的数字:" << endl;
            for (j = k-1; j >= 0; j--)
            {
                if (a[j]==b[j])
                    cout << a[j];
                else
                    cout << "X";
            }
            cout << endl;
        }
    }
    clock_t startTime, endTime;
    startTime = clock();
    sort(c,g);
    endTime = clock();
    cout << "本次排序过程中所消耗的时间:" << (double)(endTime - startTime) / CLOCKS_PER_SEC << "s"  << endl;
    for (i = 0; i < g; i++)
    {
        cout << c[i] << " ";
    }
    return 0;
}

如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img