| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2083 人关注过本帖, 1 人收藏
标题:小白虚心请教大佬
只看楼主 加入收藏
xy1024
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2020-4-1
结帖率:100%
收藏(1)
已结贴  问题点数:20 回复次数:6 
小白虚心请教大佬
先由计算机随机生成一个各位相异的4位数字,由用户来猜,每次猜测四个数字(包括了顺序),根据用户猜测的结果给出提示:xAyB
其中,A前面的数字表示有几位数字不仅数字猜对了,而且位置也正确,B前面的数字表示有几位数字猜对了,但是位置不正确。
我的代码
#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

void Initia(int *a);
void Input(int *b);
int nCompare(int a[],int b[]);
void Output();

int main ()
{
Output();
return 0;
}

void Initia(int *a)//随机产生四位四位不同的数
{
int nrand=0;
int m,n,i,j;//个,十,百,千位
srand( (unsigned)time( NULL ) );
nrand=rand();
//cout<<nrand<<endl;

m = nrand%10;
nrand = nrand/10;
n = nrand%10;
nrand = nrand/10;
while (m==n)
{
m = rand()%10;
}
i = nrand%10;
nrand = nrand/10;
while ((m==i)||(n==i))
{
i = rand()%10;
}
j = nrand%10;
while ((j==m)||(j==n)||(j==i))
{
j = rand()%10;
}

a[0] = m;
a[1] = n;
a[2] = i;
a[3] = j;

}

void Input(int *b)//用户输入的四个数字字符
{
int number=0;
int mm,nn,ii,jj;//输入的数分成个、十、百、千位

cout<<"please input a 4 bit number:"<<endl;
for(;;)//要求用户只能输入四位不同的四位数
{
cin>>number;

mm = number%10;

number = number/10;
nn = number%10;

number = number/10;
ii = number%10;

number = number/10;
jj = number%10;
if((number>9999)||(mm==nn)||(mm==ii)||(mm==jj)||(nn==ii)||(nn==jj)||(ii==jj))
{
cout<<"your input error!! please input again :"<<endl;
cin.clear();
cin.ignore(1000,'/n');
}
else
{
break;
}
}
//把个十百千位存在数组里
b[0] = mm;
b[1] = nn;
b[2] = ii;
b[3] = jj;

}
int nCompare(int a[],int b[])//比较 。(数组作参数)
{
int count1 = 0,count2 = 0;//count1数相同的个数,count2数和位置都相同的个数
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(a[i] == b[j])
{
count1++;
if(i==j)
count2++;
}
}
}
cout<<count2<<"A"<<(count1-count2)<<"B"<<endl;
return count2;//返回位置和数都对的个数
}

void Output()//输出结果
{
int a[4]={0};//存随机产生的四位不同的数
int b[4]={0};//存用户输入的四位数


Initia(a);

for(int k=3;k>=0;k--)
{
cout<<a[k];
}
cout<<endl;

for(int i=8;i>=0;i--)
{
Input(b);
int r=nCompare(a,b);

if(4==r)
{
cout<<"你真聪明 = "<<b[3]<<b[2]<<b[1]<<b[0]<<endl;
break;
}
else
{
cout<<"you have "<<i<<" chance"<<endl;
if(0==i)
cout<<"超过规定次数啦,重新来过吧"<<endl;
}
}

}
请问大佬
如何在每次猜对或超过规定次数后,添加一个是否继续游戏(Y/N)
搜索更多相关主题的帖子: number void cout int || 
2020-04-01 13:47
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:7 
瞎写的,没检查,经供参考

程序代码:
#include <iostream>
#include <random>
#include <array>
#include <tuple>

class foo
{
public:
    foo() noexcept
    {
        _rand_gen.seed( std::random_device()() );
    }
    std::array<unsigned,4> generate_4digits_unique() const noexcept
    {
        unsigned buf[] = { 0,1,2,3,4,5,6,7,8,9 };
        std::uniform_int_distribution<> gen;
        using param_type = std::uniform_int_distribution<>::param_type;
        std::swap( buf[0], buf[gen(_rand_gen,param_type{1,9})] );
        std::swap( buf[1], buf[gen(_rand_gen,param_type{1,9})] );
        std::swap( buf[2], buf[gen(_rand_gen,param_type{2,9})] );
        std::swap( buf[3], buf[gen(_rand_gen,param_type{3,9})] );
        return { buf[0], buf[1], buf[2], buf[3] };
    }
    std::array<unsigned,4> input_4digits_unique() const noexcept
    {
        auto is_unique = [](unsigned n) {
            bool buf[10] = {};
            for( ; n!=0; n/=10 ) {
                if( buf[n%10] )
                    return false;
                buf[n%10] = true;
            }
            return true;
        };

        unsigned n;
        std::cout << "please input a 4 digits number:" << std::endl;
        for( ; !(std::cin>>n) || n<1000 || n>9999 || !is_unique(n); )
        {
            std::cin.clear();
            std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
            std::cout << "your input is wrong!! please input again :" << std::endl;
        }
        return { n/1000, n/100%10, n/10%10, n%10 };
    }
    std::tuple<unsigned,unsigned> compare( const std::array<unsigned,4>& a, const std::array<unsigned,4>& b ) const noexcept
    {
        bool buf[10] = {};
        unsigned x=0, y=0;
        for( size_t i=0; i!=size(a); ++i )
        {
            buf[a[i]] = true;
            x += a[i]==b[i];
        }
        for( size_t i=0; i!=size(b); ++i )
            y += buf[b[i]];
        return { x, y-x };
    }
    bool game( size_t count=8 ) const noexcept
    {
        std::array<unsigned,4> key = generate_4digits_unique();
        std::cout << key[0] << key[1] << key[2] << key[3] << std::endl;

        for( size_t i=count; i!=0; --i )
        {
            std::array<unsigned,4> guess = input_4digits_unique();
            std::tuple<unsigned,unsigned> result = compare( key, guess );
            if(  std::get<0>(result) == 4 )
            {
                std::cout << "Bingo" << std::endl;
                return true;
            }
            std::cout << std::get<0>(result) << 'A' << std::get<1>(result) << 'B' << std::endl;
            std::cout << "you have " << (i-1) << " chance" << std::endl;
        }
        std::cout << "超过规定次数啦,重新来过吧" << std::endl;
        return false;
    }

private:
    mutable std::mt19937 _rand_gen;
};

int main( void )
{
    using namespace std;
    for( foo a; ; )
    {
        a.game(8);
        cout << "是否继续(Y/N)? ";
        char ch;
        if( !(cin>>ch) || (ch!='Y' && ch!='y') )
            break;
    }
}

收到的鲜花
  • 叶纤2020-04-02 10:56 送鲜花  1朵   附言:静静的收藏着
2020-04-02 10:50
lin5161678
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:45
帖 子:1136
专家分:3729
注 册:2011-12-3
收藏
得分:7 
        std::swap( buf[0], buf[gen(_rand_gen,param_type{1,9})] );
        std::swap( buf[1], buf[gen(_rand_gen,param_type{1,9})] );
        std::swap( buf[2], buf[gen(_rand_gen,param_type{2,9})] );
        std::swap( buf[3], buf[gen(_rand_gen,param_type{3,9})] );

目测是笔误 这样写第一位 不可能是0
应该不符合你写代码的思路
估计应该是 std::swap( buf[0], buf[gen(_rand_gen,param_type{0,9})] );

https://zh.
2020-04-02 11:44
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
这样写第一位 不可能是0
题目要求是“4位数字”,我也不知道题目是否要求必须是四位数。

如果不要求的话,
std::swap( buf[0], buf[gen(_rand_gen,param_type{1,9})] ); 改为 std::swap( buf[0], buf[gen(_rand_gen,param_type{0,9})] );
n<1000 也不用了
2020-04-02 12:22
叶纤
Rank: 8Rank: 8
等 级:禁止访问
威 望:1
帖 子:658
专家分:848
注 册:2019-11-22
收藏
得分:7 
代码很精华啊,一小段代码里就包含了很多很多个值得学习的地方

把学习时间浪费在混坛上是傻瓜行为,更何况自己的水平连一两都没到。
2020-04-02 12:46
xy1024
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2020-4-1
收藏
得分:0 
谢谢
2020-04-02 13:30
lin5161678
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:45
帖 子:1136
专家分:3729
注 册:2011-12-3
收藏
得分:0 
以下是引用rjsp在2020-4-2 12:22:59的发言:

题目要求是“4位数字”,我也不知道题目是否要求必须是四位数。

如果不要求的话,
std::swap( buf[0], buf[gen(_rand_gen,param_type{1,9})] ); 改为 std::swap( buf[0], buf[gen(_rand_gen,param_type{0,9})] );
n<1000 也不用了

哦 你是按照4位数理解的 最高位不为0
应该是不要求的 只是有序数字

https://zh.
2020-04-02 13:38
快速回复:小白虚心请教大佬
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018744 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved