c - 如何在c中比较2个整数的数字(没有数组和字符串)

标签 c arrays numbers int

我正在制作一个关于猜数字的小游戏的简单示例。

我想构建一个函数来检查数字并生成两个值,如下所示:

1) 命中数-两个数字中包含的数字以及两个数字相同位置中包含的数字数量。

2) misss-两个数字中包含但不在同一位置的数字的个数。

例如:

int systemNumber=1653;
int userGuess=5243;

在此示例中,两个数字中都有数字 5 和 3。在两个数字中,数字 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++;
            }
        }
    }
}

最佳答案

这是我不久前写的一个示例,我针对您的问题进行了调整:

我基本上使用两个 for 循环,外部循环遍历第一个数字 1653,内部循环遍历第二个数字 5243。它基本上将第一个数字中的每个单独数字与第二个数字中的所有数字进行比较。

根据计数器,它会使用模 %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;
}

关于c - 如何在c中比较2个整数的数字(没有数组和字符串),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41321774/

相关文章:

c++ - 使用指针运算将两个数组的内容相加并保存到一个空数组

php - 找到唯一除数的有效方法

c++ - 如何简化此函数的调用?

c - 使用 _mm256_load_ps() Intel 内在函数时出错

c - 为什么我的数组的前一个元素会发生变化(C 中的字符串数组)

php - 从 php 中的数组构建 "crosstab"或 "pivot"表

c - 套接字上的 select() (问题)

java - 通过函数返回多个数组(处理)

algorithm - O(log log n) 算法确定 n 是否为完全平方

javascript - 转换字符串货币