簡體   English   中英

如何迭代 C 中的數字

[英]How to iterate over numbers in C

我必須計算某個數字在一個范圍內的每個數字中重復了多少次。 例如,在 0 到 20 之間的數字中,只有一次出現 1 重復兩次 (11)。 我最初是通過將 int 轉換為 str 並對其進行迭代來做到這一點的,但我希望能夠以算術方式解決這個問題。 有任何想法嗎?

這是您可以使用的通用解決方案,您的問題沒有包含太多信息,因此我假設您要計算每個數字中每個數字的重復次數。

所以我所做的就像散列,其中每個數字中的數字永遠不會超過值9 ,所以它們是從09所以我制作了名為arr的 hash 表,所以我所做的就是得出數字中的每一個數字和在arr中增加該數字的 position

例如,數字554將導致arr[5]++; 兩次和arr[4]++; 只有一次,使用 hash 表的簡單想法。

然后最后我遍歷 hash 數組,打印每個數字中每個數字的出現次數。

這是代碼:

#include <stdio.h>
#include <math.h>
int main()
{
    int arr[6] = {5555, 12112, 14, -3223, 44, 10001};

    int tempArr[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

    for (int i = 0; i < 6; i++) {
        int temp1 = arr[i];

        // get the number of occurrences of each digit in each number
        do{
            tempArr[(abs(temp1) % 10)]++;
            temp1 /= 10;
        }while(temp1 != 0);

        // loop over the array to know how many times each digit occurred
        printf("the number of occurrences in number called %d\n", arr[i]);
        for (int j = 0; j < 10; j++) {
            if(tempArr[j] > 1)
                printf("number %d occurred %d times\n", j, tempArr[j]);

            // resetting that position of the array
            tempArr[j] = 0;
        }

    }
    return 0;
}

這是 output:

the number of occurrences in number called 5555
number 5 occurred 4 times
the number of occurrences in number called 12112
number 1 occurred 3 times
number 2 occurred 2 times
the number of occurrences in number called 14
the number of occurrences in number called -3223
number 2 occurred 2 times
number 3 occurred 2 times
the number of occurrences in number called 44
number 4 occurred 2 times
the number of occurrences in number called 10001
number 0 occurred 3 times
number 1 occurred 2 times

您可以將您的數字多次除以 10:

int number = 72;
int rest;

while(number)
{
    rest = number % 10;
    printf("%d\n", rest);
    number /= 10;
}

這里rest包含“2”然后是“7”

這是對@Abdo Salm 提供的答案的一個非常小的重復,只是為了演示一種稍微替代的方法。 所有功勞歸功於 Abdo。

#include <stdio.h>
#include <string.h> // 'memset()'
#include <limits.h> // 'INT_MIN/MAX'

int my_main() {
    int tests[] = { 0, 11, 121, 5555, 12112, 14, -3223, 44, 1223334444, INT_MIN, INT_MAX };

    // run through multiple test values
    for( int i = 0; i < sizeof tests/sizeof tests[0]; i++ ) {
        // counter for each digit, all init'd to zero
        int cnts[ 10 ];
        memset( cnts, 0, sizeof cnts );

        int test = tests[ i ];
        // count occurences of each digit (right to left)
        for( ; test;test /= 10 )
            cnts[ abs(test % 10) ]++;

        // report only digits appearing multiple times
        printf( "%11d: ", tests[ i ] );
        char *sep = "";
        for( int j = 0; j < 10; j++ )
            if( cnts[ j ] > 1 )
                printf( "%s%dx%d", sep, cnts[ j ], j ), sep = ", ";

        if( !*sep )
                printf( "--" );

        putchar( '\n' );
    }
    return 0;
}

Output

          0: --
         11: 2x1
        121: 2x1
       5555: 4x5
      12112: 3x1, 2x2
         14: --
      -3223: 2x2, 2x3
         44: 2x4
 1223334444: 2x2, 3x3, 4x4
-2147483648: 3x4, 2x8
 2147483647: 3x4, 2x7

(是的,一個舊的 32 位編譯器。)

某個數字在一個范圍內的每個數字中重復多少次。

偽代碼*1

獲取范圍: imin, imaximin <= imax的任何int對)

獲取數字: digit 0 到 9

迭代miminimax ,包括

....打印m

.... 設置digit_count = 0

.... 重復

....... m的最后一位ldabs(m%10)

....... 如果ld == digit ,則增加digit_count

........ 將m除以 10

....直到m == 0

.... 打印digit_count

完畢


*1由於 OP 沒有提供代碼,似乎最好不要提供代碼答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM