2016-12-25 11 views
2

数字を推測することについて簡単な例を挙げています。cで2つの整数の数字を比較する方法(配列と文字列を除く)

そして、私は番号をチェック機能を構築し、次のように2つの値を作りたい:

1)がヒット、両方の番号で、両方の番号の同じ場所に含まれている桁数を。

2)misses - 両方の数字に含まれているが同じ場所に含まれていない数字の数。例えば

:この例では

int systemNumber=1653; 
int userGuess=5243; 

、両方の数値に同じ場所で両方の数字桁3の桁5および3があります。しかし、systemNumberの数字5は、userNumberと同じ場所にありません。 1ヒットと1ヒット。

配列のコードを記述しましたが、配列と文字列なしでこれを行う方法があるかどうかを知りたいと思います。

ここに私のコードです。あなたは私のコードのいずれかの改善を持っている場合、私はそれを知っているように思います:)

#include <stdio.h> 
#include <stdlib.h> 

void checkUserCode(int num1[4], int num2[4]); // declare the function which check the guess 

int hits=0, misses=0; // hits and misses in the guess 

int main(void) 
{ 
    int userCode=0; 
    int userCodeArray[4]; 
    int systemCodeArray[4]={1, 4, 6, 3}; 
    int i=0; 

    // printing description 
    printf("welcome to the guessing game!\n"); 
    printf("your goal is to guess what is the number of the system!\n"); 
    printf("the number have 4 digits. Each digit can be between 1 to 6\nGood Luck!\n"); 

    // input of user guess 
    printf("enter number: "); 
    scanf("%d", &userCode); 

    for (i=3; i>=0; i--) 
    { 
     userCodeArray[i]=userCode%10; 
     userCode=userCode/10; 
    } 

    checkUserCode(systemCodeArray, userCodeArray); 

    printf("there are %d hits and %d misess", hits, misses); // output 

    return 0; 
} 



/* 
    this function gets two arrays and check its elements 
    input (parameters): the two arrays (codes) to check 
    output (returning): number of hits and misses 

    if the element in one array also contains in the other array but not the same index: add a miss 
    if the element in one array also contains in the other array and they have the same index: add a hits 
*/ 

void checkUserCode(int num1[4], int num2[4]) 
{ 
    int i=0, j=0; 

    for (i=0; i<4; i++) 
    { 
     for (j=0; j<4; j++) 
     { 
      if(num1[i]==num2[j]) 
      { 
       if (j==i) 
        hits++; 
       else 
        misses++; 
      } 
     } 
    } 
} 
+1

コードは、一般的にhttp://codereview.stackexchange.comを検討し、動作している場合。そうでなければ、改善以外に何か質問がありますか? – chux

答えて

3

私は私があなたの問題のために微調整され、しばらく前に書いた例です。

私は基本的に2つのループfor、最初の数、1653、そして乗り越え内側のループの上に行く外側のループを使用しています秒番号、5243。基本的には、最初の番号の個々の番号と2番目の番号のすべての番号を比較します。

カウンターに応じて、同じ番号が同じ位置に一致するかどうかを評価します。モジュロ%10を使用して各数値を比較します。

これはコードです:

#include <stdio.h> 
#include <stdlib.h> 

int 
main(void) { 
    int num1 = 1653; 
    int num2 = 5243; 

    int pos1, pos2, hit = 0, miss = 0, i, j; 

    pos1 = 0; 
    for (i = num1; i > 0; i /= 10) { 
     pos2 = 0; 

     for (j = num2; j > 0; j /= 10) { 

      if (i % 10 == j % 10) { 

       if (pos1 == pos2) { 
        hit++; 

       } else { 
        miss++; 
       } 
      } 
      pos2++; 
     } 
     pos1++; 
    } 

    printf("hits = %d\n", hit); 
    printf("misses = %d\n", miss); 

    return 0; 
} 
0

アプローチのようなリストは、これは目のゲームを雄牛のための一般的なものです。あなたは数字だけで作業することができます。

int n1 =...; 
int n2 =...; 

int count = 1; 
while(n1) { 
    d1 = n1%10; 
    tmp = n2; 
    int count2 = 1; 
    while(tmp): 
     d2 = n2 %10; 
     if(d1 == d2) { 
      if(count == count2) hit++; 
      else miss++; 
      break; 
     } 
     count2++; 
     tmp/=10; 
    } 
    count++; 
    n1/=10; 
} 
:ここではあなたが始めるために、コードのシンプルで非最適な一枚(!!数字は二度同じ桁の複数を持って検討し、私は最初の桁に0をこのよく扱うか、いないよは?!)です

もう一度これは完全に機能的なコードではありません!これはあなたを始めさせることです。それが失敗する場所を試してみてください。ここで