【问题标题】:How to compare the digits of 2 integer numbers in c (without arrays and strings)如何比较c中2个整数的位数(没有数组和字符串)
【发布时间】:2017-05-10 08:37:35
【问题描述】:

我正在做一个关于猜数字的小游戏的简单例子。

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

1) hits - 两个数字中包含的位数以及两个数字在同一位置的位数。

2) 遗漏 - 两个数字都包含但不在同一位置的数字的个数。

例如:

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++;
            }
        }
    }
}

【问题讨论】:

标签: c arrays numbers int


【解决方案1】:

这是我前段时间写的一个例子,我针对你的问题进行了调整:

我基本上使用了两个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;
}

【讨论】:

    猜你喜欢
    • 2012-08-16
    • 2021-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多