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

来源:互联网 发布:淘宝卖家关闭交易 编辑:程序博客网 时间:2024/05/21 07:08

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”

0 0
原创粉丝点击