299. Bulls and Cows ——猜数字游戏

本文介绍了一个简单的 Bulls and Cows 数字猜测游戏的实现方法,通过计算两个等长数字串之间的 bulls(位置与数值都相同的数字)和 cows(数值相同但位置不同的数字)的数量来给出提示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

299. Bulls and Cows ——猜数字游戏
Difficulty: Easy
位置正确的数字个数和位置错误的数字个数。

You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess match your secret number exactly in both digit and position (called “bulls”) and how many digits match the secret number but locate in the wrong position (called “cows”). Your friend will use successive guesses and hints to eventually derive the secret number.

For example:
Secret number: “1807”
Friend’s guess: “7810”
Hint: 1 bull and 3 cows.
(The bull is 8, the cows are 0, 1 and 7.)
Write a function to return a hint according to the secret number and friend’s guess, use A to indicate the bulls and B to indicate the cows. In the above example, your function should return “1A3B”.

Please note that both secret number and friend’s guess may contain duplicate digits, for example:
Secret number: “1123”
Friend’s guess: “0111”
In this case, the 1st 1 in friend’s guess is a bull, the 2nd or 3rd 1 is a cow, and your function should return “1A1B”.
You may assume that the secret number and your friend’s guess only contain digits, and their lengths are always equal.

“1103”
“1002” 则应返回“2A0B”

char* getHint(char* secret, char* guess) {
    int len=strlen(secret);
    int i,j;
    int Bulls=0,Cows=0;
    int a[10];
    char *Ret=(char*)malloc((len+4)*sizeof(char));
    char *string1=(char*)malloc(len*sizeof(char));
    char *string2=(char*)malloc(len*sizeof(char));
    char *reta="A";
    char *retb="B";

    memset(a,0,10*sizeof(int));

    if(strlen(secret) != strlen(guess))
        return 0;

    for(i=0;i<len;i++)   //记录secret字符串中每个数字出现的个数
    {
        a[secret[i]-'0']++;
    }
    for(i=0;i<len;i++)
    {
        if(secret[i] == guess[i])   //若在guess中找到一个数字与secret对应,Bulls加1,该数字的个数记录减1
        {
            Bulls++;
            a[guess[i]-'0']--;
        }
        else
        {
            if(a[guess[i]-'0'] == 0)  //若不对应,且secret中无此数字,跳过
            {
                continue;
            }
            else     //若不对应,secret中有此数字,说明该数字位置错误,Cows加1,该数字的个数记录减1
            {
                Cows++;
                a[guess[i]-'0']--;
            }
        }
    }
    for(i=0;i<10;i++)
    {
        if(a[i]<0)    //若某数字的个数记录变为负数,补偿Cows
            Cows=Cows+a[i]; 
    }

    sprintf(string1,"%d",Bulls);
    sprintf(string2,"%d",Cows);
    //  itoa(Bulls,string1,10);
    //  itoa(Cows,string2,10);
    strcpy(Ret,string1);
    strcat(Ret,reta);
    strcat(Ret,string2);
    strcat(Ret,retb);

    free(string1);
    free(string2);
    return Ret;
}

关键是“某数字的个数记录变为负数,补偿Cows”。
要点:
1,malloc()函数 从堆动态分配内存
头文件 include “stdlib.h”

2,函数要返回指针时,如 char* Ret;
最好动态分配。从堆分配比从栈分配安全。

3,strlen(A) 返回字符串A的长度
4,
字符转数字
‘3’-‘0’=3
‘3’-48=3

数字转字符串——按格式输出
char* a;
int b;
sprintf(a,”%d”,b); //3转字符串”3”

5,字符串拼接
strcpy(Ret,str1); //Ret=Ret+str1, “3”+”A”=”3A”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值