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.
要注意char的用法
char * f = "abcdef";
strcpy(f, "abcdef");
printf("%s",f);
//上面會掛 因為當char *指向一串"" 的字串 會把它處理為 唯讀
The first example is a char *
to a character literal (a literal is "something"
). Character literals are read-only, and attempting to write to them can result in crashes. Your first pointer should really be const char *f = "abcdef";
, which strcpy
won't take.
char s[] = "ddd";
strcpy(&s[0], "eee");
printf("%s", s);
//這個會放在stack 所以是可改的http://stackoverflow.com/questions/5645949/program-aborts-when-using-strcpy-on-a-char-pointer-works-fine-on-char-array
要注意 strcpy一定要是在stack的獲釋malloc 這些有實體記憶體位置的
不能直接char *p; 一個空指標過去
另外 在第26是傳&p[0]回去,這樣不行是因為 p是stack的區域變數 回傳回去記憶體就被釋放了 必須要一個在heap的記憶體ANS來負責回傳
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | char* getHint(char* secret, char* guess) { int size=strlen(secret); int A=0,B=0; char p[size]; //strcpy(p,guess); // can't use pointer in strcpy *p strcpy(&p[0],guess); for(int i=0;i<size;i++)if(secret[i]==guess[i]){ A++; p[i]='A'; } for(int i=0;i<size;i++){ for(int j=0;j<size;j++){ if(secret[i]==p[j] &&p[i]!='A' ){//p[i]!='A' is to avoid, the (1122,1222), need to avoid the A B++; p[j]='b'; break; } } } char *ans=malloc(sizeof(char)*size); sprintf(ans,"%dA%dB\0",A,B); //sprintf(p,"%dA%dB\0",A,B); printf("%s",p); //return &p[0]; return ans;//can't use &p[0], because the p is in the stack } |
----------------
更漂亮一點 不需要額外跟guess 依樣長的字串
方法是用一個HASH array (第4行)
裡面記錄誰看過這個數字了
+1 代表secret看過
-1 代表guess看過
所以當secret檢查發現<0 guess="" nbsp="" p="" secret="">當guess檢查的時候發現>0 代表之前secret有放進來 ,可以算進B
要注意的是第5行的memset 只能用在初始0 或是 -1 因為他是把所以的記憶體都寫成一樣的內容
還有要注意11行要減去'0' 才可以轉換成0~9的範圍
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | char* getHint(char* secret, char* guess) { int A=0,B=0; int p[10]; memset(&p[0],0,sizeof(int)*10);//beware the memset can only set to 0 or -1 for(;*secret;secret++,guess++){ if(*secret==*guess){ A++; }else{ if(p[(int)*secret -'0'] <0 )B++; //if is <0, there has a guess put a value in here before p[(int)*secret-'0']+=1;//use 1 to indicate the secret has a vaule in here if(p[(int)*guess -'0'] >0)B++;//if >0, there is a secret put a value in here p[(int)*guess-'0']-=1; //use -1 to indicate the guess has a value in here printf("%d %d %d\n",p[(int)*secret-'0'],p[(int)*guess-'0'],B); } } char *ans=malloc(sizeof(char)*4); sprintf(ans,"%dA%dB\0",A,B); //sprintf(p,"%dA%dB\0",A,B); printf("%s",p); //return &p[0]; return ans;//can't use &p[0], because the p is in the stack } |
0>
沒有留言:
張貼留言