Hướng dẫn compare digits in a number c++ - so sánh các chữ số trong một số c ++

Tôi đang làm ví dụ đơn giản về trò chơi nhỏ về những con số đoán.

Và tôi muốn xây dựng một hàm kiểm tra các số và tạo hai giá trị như sau:

1] HITS-Số chữ số chứa cả số và ở cùng một vị trí cho cả hai số.

2] Misses-Số lượng các chữ số chứa cả hai số nhưng không ở cùng một nơi.

Ví dụ:

int systemNumber=1653;
int userGuess=5243;

Trong ví dụ này, trong cả hai số có các chữ số 5 và 3. Trong cả hai số chữ số 3 ở cùng một nơi. Nhưng, chữ số 5 trong systemNumber không ở cùng một nơi với userNumber. Vì vậy, chúng tôi có ở đây 1 hit và 1 lần bỏ lỡ.

Tôi đã viết mã cho nó với các mảng và tôi muốn biết liệu có cách nào tôi có thể làm điều này mà không cần mảng và chuỗi.

Đây là mã của tôi. Xin vui lòng, nếu bạn có bất kỳ cải tiến nào cho mã của tôi, tôi muốn biết nó :]

#include 
#include 

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 l2] & nbsp; & nbsp; & nbsp; return1; & nbsp; & nbsp; otherif [l2> l1] & nbsp; & nbsp; & nbsp; return2; & nbsp; & nbsp; khác {& nbsp; & nbsp; & nbsp; for [n = 0; n b [n]] & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; return1; & nbsp; & nbsp; & nbsp; & nbsp; if [b [n]> a [n]] & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; return2; & nbsp; & nbsp; & nbsp; } & nbsp; & nbsp; & nbsp; if [n == l1] & nbsp; // có thể được xóa [tùy chọn] & nbsp; & nbsp; & nbsp; & nbsp; return0; & nbsp; & nbsp; } & nbsp; }if [p1 == 1 && p2 == 1] {
    if [l1 > l2]
      return 1;
    else if [l2 > l1]
      return 2;
    else {
      for [n = 0; n b[n]]
          return 1;
        if [b[n] > a[n]]
          return 2;
      }
      if [n == l1]  // Can be removed [optional]
        return 0;
    }
  }

& nbsp; // Nếu một số là dương và số khác là âm// If one number is positive and other is negative

& nbsp; if [p1 == 1 && p2 == 0] // đầu tiên không có tích cực & nbsp; & nbsp; return1; & nbsp; if [p1 == 0 && p2 == 1] // thứ 2 không có tích cực & nbsp; & nbsp; return2;if [p1 == 1 && p2 == 0] // 1st no positive
    return 1;
  if [p1 == 0 && p2 == 1] // 2nd no positive
    return 2;

& nbsp; // Nếu cả hai số âm âm// If both numbers negative

& nbsp; Nếu [l1if [l1

Chủ Đề