c++作业,麻烦了

浪客剑芯 2014-06-25 10:02:15
使用VC作成Console应用,完成如下功能的程序:

猜数游戏1。
计算机拟定一个4位的随机数后,
由猜数人来输入4位数。
每次给出有多少位数字正确,有多少位位置正确。
形如:D2P1,表示:数字2位正确,位置1位正确。
请猜数人再次输入,直到全部正确为止。


猜数游戏2。
出题人拟定一个4位数后,
让计算机输出4位数。并接收出题人的回答,有多少位数字正确,有多少位位置正确。
然后计算机再次输出4位数,直到全部正确为止。

以上
...全文
209 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-06-26
  • 打赏
  • 举报
回复
试试看,不保证对:
    while (1) {
        cout << "输入数据(4位数字):";
        cin >> str2;
        //cout<<str2<<endl;
        if (strlen(str2)!=4) continue;
        if (!(isdigit(str2[0])
           && isdigit(str2[1])
           && isdigit(str2[2])
           && isdigit(str2[3]))) continue;
        int x[4],y;
        y=0;
        for (n = 0; n < 4; n++){
            x[n]=0;
            if (str1[n] == str2[n]){
                x[n]=n+1;
                y++;
            }
        }
        if (y == 4){
            cout << "恭喜你答对了!" << endl;
            getchar();
            return 0;
        };
        cout << "恭喜你答对了" << y << "位位置" << endl;
        y=0;
        for (n=0;n<4;n++) {
            for (m=0;m<4;m++) {
                if (str2[n]==str1[m]) {
                    y++;
                    break;
                }
            }
        }
        cout << "恭喜你答对了" << y << "位数字" << endl;
    }
孤雲独去闲 2014-06-26
  • 打赏
  • 举报
回复

void CheckInput(int input[],int target[])
{
int A=0,B=0,i,j;
for(i=0;i<4;i++)
{
     for(j=0;j<4;j++)
{
      if(input[i]==target[j]) B++;
}
} 
 for(i=0;i<4;i++)
{
  if(input[i]==target[i])
  {
   A++;
   B--;
  }
}
  printf("%dA%dB\n",A,B);
}
浪客剑芯 2014-06-26
  • 打赏
  • 举报
回复
我这个有多少位数字正确,有多少位位置正确。不太理想。求改进谢谢
赵4老师 2014-06-25
  • 打赏
  • 举报
回复
有啥问题? 具体需要改进那几点?
浪客剑芯 2014-06-25
  • 打赏
  • 举报
回复
帮忙改进下谢谢:
#include "stdafx.h"
#include <iostream> 
#include <math.h> 
#include <ctime> 
#include <Windows.h> 
using namespace std;

#define MAX 10
#define N 4

int main()
{
	int i, j, k, n, m;
	int a[N];
	char str1[N], str2[N + 1];

	//产生随机数


	srand((unsigned)time(NULL));//srand()函数产生一个以当前时间开始的随机种子 
	for (n = 0; n<4; n++)
	{
		a[n] = rand() % MAX;//MAX为最大值,其随机域为0~MAX-1
		//cout << a[n];
		str1[n] = a[n] + '0';
	}

	int flag = 1;
	while (flag>0)
	{
		flag++;
		cout << "输入数据:"; 
		cin >> str2;
		//cout<<str2<<endl;
		int x[6] = { 0 }, y = 0;
		for (n = 0; n < 4; n++){		
			if (str1[n] == str2[n]){
				x[n]=n+1;
			 y++;
			};
		};
		//for (i = 0; i < 4; i++){
		//	for (n = 0; n < 4; n++){
		//		if (str1[i] == str2[n] && str1[n] != str2[n])
		//			y++;
		//	};
		//	
		//};
		if (y == 4){
			cout << "恭喜你答对了!" << endl;
			flag = 0;
			getchar();
			exit;
		};
		for (int i = 0; i < 4;i++){			
				cout << "恭喜你答对了" << x[n]<<"位置"<<endl;					
		}
		cout << "恭喜你答对了" << y << "位" << endl;
		y = 0;
		for (int i = 0; i < 4;i++){
			x[n] = 0;		
		};
		
	}
}

64,682

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧