不知一点而知所有

探究有一道数学题不知道,是否能从而知道全部。上代码:

#include <cstdlib>
#include <iostream>
#include <ctime>
#include <sstream>
using namespace std;
#define random(a,b) (rand() % (b-a+1))+ a
const int n = 3;//地图的行
const int m = 3;//地图的列
const int association = 70;//产生联想的概率
const int divergence = 29;//产生发散的概率
bool war = false;//是否会发生战争
int main()
{
    srand((int)time(0));
    string map[n][m];
    for(int i = 0;i < n;++i) {//初始化地图
        for(int j = 0;j < m;++j) {
            map[i][j] = " + ";
            cout << map[i][j];
        }
        cout << endl;
    }
    int hardWork;
    int impossible[1][2];
    cout << "请输入努力值:" << endl;//初始化努力值
    hardWork = 20;
    //cin >> hardWork;
    cout << "请输入不可知点的坐标:" << endl;//初始化不可知点坐标
    impossible[0][0] = 1;
    impossible[0][1] = 1;
    //cin >> impossible[0][0] >> impossible[0][1];
    map[impossible[0][0]][impossible[0][1]] = " x ";//显示地图
    for(int i = 0;i < n;++i) {
        for(int j = 0;j < m;++j) {
            cout << map[i][j];
        }
        cout << endl;
    }
    int try1[n][m] = {0};//初始化统计地图的点为0
    try1[impossible[0][0]][impossible[0][1]] = -1;//统计地图的不可知点设置为-1
    int x, y;//坐标点
    int tempX, tempY;
    bool first = false;//记录是否为第一次
    bool win = true;//是否胜利
    int count = 0;//记录目前为第几步
    int development;//记录发展的趋势
    int mode = -1;//记录发展模式
    while(true) {
        if(!first) {//随机初始化坐标
            x = random(0,n-1);
            y = random(0,m-1);
            while((x == impossible[0][0])&&(y == impossible[0][1])) {
                x = random(0,n-1);
                y = random(0,m-1);
                --hardWork;//如果初始化的点在不可知点上,努力值就减一
            }
            first = true;
        }
        cout << ++count << ":" << endl;
        if(mode == 0) {//输出发展的模式
            cout << "联想" << endl;
        }
        else if(mode == 1) {
            cout << "发散" << endl;
        }
        else if(mode == 2){
            cout << "战争" << endl;
        }
        else {
            cout << "开始" << endl;
        }
        if(!war) {//如果没有发动战争,统计地图的点就加一
            ++try1[x][y];
        }
        else {//发动了战争,统计地图的点就归零
            try1[x][y] = 0;
            war = false;
        }
        for(int i = 0;i < n;++i) {//每次得到新的点就重新输出地图
            for(int j = 0;j < m;++j) {
                if(((x == impossible[0][0])&&(y == impossible[0][1]))||(try1[i][j] <= 0)) {
                    cout << map[i][j];
                }
                else {
                    cout << " " << try1[i][j] << " ";
                }
            }
            cout << endl;
        }
        development = random(1,100);//随机出发展的趋势
        if(development > 0 && development <= association) {//如果随机的发展趋势为百分之70,则进入联想模式
            tempX = random(0,2) - 1;
            x = x + tempX;
            tempY = random(0,2) - 1;
            y = y + tempY;
            while(((x == impossible[0][0])&&(y == impossible[0][1]))||(x < 0 || x >= n || y < 0 || y >= m)) {
                if((x == impossible[0][0])&&(y == impossible[0][1])) {
                    --hardWork;
                }
                x = x - tempX;
                tempX = random(0,2) - 1;
                x = x + tempX;
                y = y - tempY;
                tempY = random(0,2) - 1;
                y = y + tempY;
            }
            mode = 0;
        }
        else if(development > association && development <= (association + divergence)) {//如果随机的发展趋势为百分之29,则进入发散模式
            tempX = x;
            x = random(0,n-1);
            tempY = y;
            y = random(0,m-1);
            while(((x == impossible[0][0])&&(y == impossible[0][1]))||(((x > (tempX - 1))&&(x < (tempX + 1)))||((y > (tempY - 1))&&(y < (tempY + 1))))) {
                if((x == impossible[0][0])&&(y == impossible[0][1])) {
                    --hardWork;
                }
                x = random(0,n-1);
                y = random(0,m-1);
            }
            mode = 1;
        }
        else {//否则发动战争
            x = random(0,n-1);
            y = random(0,m-1);
            while((x == impossible[0][0])&&(y == impossible[0][1])) {
                x = random(0,n-1);
                y = random(0,m-1);
                --hardWork;
            }
            war = true;
            mode = 2;
        }
        cout << "努力值剩余:" << hardWork << endl;//输出努力值剩余
        if(hardWork <= 0) {//如果努力值变为零,则表示没能开发全部的知识
            cout << "失败了" << endl;
            break;
        }
        else {//判断是否胜利了
            for(int i = 0;i < n;++i) {
                for(int j = 0;j < m;++j) {
                    if(try1[i][j] == 0) {
                        win = false;
                        break;
                    }
                    else {
                        win = true;
                    }
                }
                if(!win) {
                    break;
                }
            }
            if(win) {
                cout << "成功了" << endl;
                break;
            }
        }
    }
    
    return 0;
}

运行结果:

+ + +
+ + +
+ + +
请输入努力值:
请输入不可知点的坐标:
+ + +
+ x +
+ + +
1:
开始
+ + +
+ x +
+ + 1
努力值剩余:19
2:
联想
+ + +
+ x +
+ 1 1
努力值剩余:18
3:
联想
+ + +
+ x 1
+ 1 1
努力值剩余:18
4:
联想
+ + +
+ x 1
+ 1 2
努力值剩余:18
5:
发散
+ + +
1 x 1
+ 1 2
努力值剩余:18
6:
联想
+ 1 +
1 x 1
+ 1 2
努力值剩余:18
7:
联想
1 1 +
1 x 1
+ 1 2
努力值剩余:17
8:
发散
1 1 +
1 x 2
+ 1 2
努力值剩余:17
9:
发散
1 1 +
1 x 2
1 1 2
努力值剩余:16
10:
联想
1 1 +
2 x 2
1 1 2
努力值剩余:16
11:
发散
1 1 1
2 x 2
1 1 2
努力值剩余:16
成功了

 解说:这个程序让用户设定努力值,如果程序随机到了不可知点,努力值就会减小。不可知点也是用户定的。当努力值和不可知点都设定好以后程序就会自动运行了。随机的点如果和不可知点重合,努力值也会减小。点的展开有三种模式:联想、发散和战争。这三个的比例各不相同,可以调整。联想就是x减一或者加一,y减一或者加一。发散则是随机出联想范围之外的点。战争则是使随机出的某一个点归零。

程序在努力值没有归零,然后其它点都随机到的情况下结束程序,并输出成功。如果努力值归零了其它点还没随机完,就算是失败了。

posted on 2020-10-12 10:49  飞凤颖悟绝伦  阅读(144)  评论(0编辑  收藏  举报

导航