我一直在試圖讓一個程序,用所謂的「智多星」的遊戲幫助。如果你不熟悉這個遊戲簡單說是這樣的:有需要被猜中的密碼,一個球員做這個代碼,而另一名球員試圖猜測它的基礎上,猜球員得到一個黑色的PEG爲每他猜對的信件在正確的地方,每個字母的白色釘在錯誤的地方,屬於模式中的其他地方,如果字母完全不出現,則最終沒有釘住。爲什麼我的程序的輸出不能複製所需的輸出?
我的程序應該採取的猜測,白色釘的數量和黑釘和輸出所有可能的解決方案的數量。另外一個要求是程序必須使用遞歸。
樣的程序應該如何運行:
Enter the pattern length: 3 Input the guess pattern: abc Enter the number of black pegs in the feedback: 2 Enter the number of white pegs in the feedback: 0 The possible key patterns are: aac aba abb abd abe abf acc adc aec afc bbc cbc dbc ebc fbc
正如你將能夠運行,我寫我的程序顯然不給出相同輸出的代碼來看看。 (順便說我也轉換輸入到int數組char數組和打印的內容如炭)
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
void userEnter(int*pattern, int n);
void print(int * s, int n);
void recurs(int * s, int * a, int n, int wpegs, int bpegs);
bool Done (int*s);
bool bPegs(int*a ,int*s, int bpegs, int wpegs, int n);
bool wPegs(int* modcom, int* modoriginal, int*s, int wpegs, int w);
void change(int*modoriginal, int*modcom, int i, int k, int w);
int main(void)
{
int i, n, bpegs, wpegs;
printf("Enter the pattern length: ");
scanf("%d",&n);
int *a = malloc(n * sizeof *a);
printf("Input the guess pattern: ");
int pattern[n];
userEnter(pattern, n);
printf("Enter the number of black pegs in the feedback: ");
scanf("%d",&bpegs);
printf("Enter the number of white pegs in the feedback: ");
scanf("%d",&wpegs);
printf("The possible key patterns are: ");
for(i=0; i<=n-1; i++)
{
a[i]=0;
}
print(a, n);
recurs(a, pattern, n, wpegs, bpegs);
}
void userEnter(int*pattern, int n)
{
char input[n];
scanf("%s",input);
int i;
for(i = 0; i < n-1; i++)
{
pattern[i] = input[i]-65;
}
}
void print(int * s, int n)
{
int i;
printf("\n");
for(i = n-1; i >= 0; i--)
{
printf("%c", (s[ i ] + 65));
}
}
void recurs(int * s, int * a, int n, int wpegs, int bpegs)
{
int i;
if(Done(s))
{
print(s, n);
printf("\nAccomplisshed!\n");
}
else{
s[ 0 ] += 1;
for(i = 0; i < n-1; i++)
{
if(s[ i ] == 6){
s[ i ] = 0;
s[ i + 1 ] += 1;
}
}
if(bPegs(a ,s, bpegs, wpegs, n))
{
print(s, n);
}
recurs(s, a, n, wpegs, bpegs);
}
}
bool Done (int*s)
{
int i;
bool done=true;
for (i=0;i<=11;i++)
{
if(s[i]!=5)
{
done=false;
}
}
return done;
}
bool bPegs(int*a ,int*s, int bpegs, int wpegs, int n)
{
int i,j,c=0;
bool d = false;
for(i=0; i<n-1; i++)
{
if(a[i]=s[i])
{
c++;
}
}
int x =n-c;
int* modcom;
int*modoriginal;
modcom=(int*)malloc((x)*(sizeof(int)));
modoriginal=(int*)malloc((x)*(sizeof(int)));
int w=0;
for(j=0; j<n-1; j++)
{
if(a[j]!=s[j])
{
modcom[w]=s[j];
modoriginal[w]=a[j];
w++;
}
}
if(c=bpegs)
{
d = wPegs(modcom, modoriginal, s, wpegs, w);
}
return d;
}
bool wPegs(int*modcom, int*modoriginal, int*s, int wpegs, int w)
{
int i, k, count=0;
for(i=0; i<=w; i++)
{
for(k=0; k<=w; k++)
{
if (modoriginal[i]==modcom[k])
{
count++;
change(modoriginal, modcom, i, k, w);
}
}
}
if(wpegs=count)
{
return true;
}
}
void change(int*modoriginal, int*modcom, int i, int k, int w)
{
int c, o;
for(c=i-1; c<w-1; c++)
{
modoriginal[c]=modoriginal[c+1];
}
for(o=k-1;o<w-1;o++)
{
modcom[o]=modcom[o+1];
}
}
此:'INT * A = (int *)malloc((n)*(sizeof(int)));'最好寫成int * a = malloc(n * sizeof * a);'。這是短,更安全,只求更好(見[不投'的malloc()'的返回在C值(http://stackoverflow.com/a/605858/28169)瞭解詳細信息)。 – unwind