如何在 C 中迭代数字

问题描述 投票:0回答:4

我必须计算某个数字在某个范围内的每个数字中重复的次数。例如,在 0 到 20 之间的数字中,仅出现一次 1 重复两次 (11)。我最初通过将 int 转换为 str 并迭代它来完成此操作,但我希望能够以算术方式解决此问题。有什么想法吗?

c loops iteration
4个回答
2
投票

您可以将您的数字多次除以 10:

int number = 72;
int rest;

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

这里 rest 包含“2”,然后包含“7”


1
投票

这是您可以使用的通用解决方案,您的问题没有包含太多信息,所以我假设您想要计算每个数字中每个数字的重复次数。

所以我所做的就像散列,其中每个数字中的数字永远不会交叉值

9
,所以它们从
0
9
所以我制作了一个名为
arr
的哈希表,所以我所做的是到达数字中的每一位数字并增加该数字在
arr

中的位置

例如,数字

554
将导致
arr[5]++;
两次,而
arr[4]++;
仅一次,使用哈希表的简单想法。

最后我迭代哈希数组,打印每个数字中每个数字出现的次数。

这是代码:

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

这是输出:

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

0
投票

某个数字在某个范围内的每个数字中重复了多少次。

伪代码*1

获取范围:

imin, imax
(任何
int
对,其中
imin <= imax

获取数字:

digit
0 到 9

m
迭代
imin
imax
,包括

....打印

m

....设置

digit_count = 0

...重复

.......

ld
的最后一位数字
m
abs(m%10)

....... 如果

ld == digit
,则增加
digit_count

........ 将

m
除以 10

....直到

m == 0

....打印

digit_count

完成


*1 由于OP没有提供代码,似乎最好不要提供代码答案。


0
投票

重复 @Abdo Salm 提供的答案只是为了演示一种稍微另类的方法。一切都归功于阿卜杜。

#include <stdio.h>
#include <string.h>
#include <limits.h>

void process( int n, int cnts[] ) {
    // count occurrences of each digit (right to left)
    while( n )
        cnts[ abs(n % 10) ]++, n /= 10;
}

void report( int cnts[], int thresh ) {
    char *sep = "";
    for( int j = 0; j < 10; j++ )
        if( cnts[ j ] > thresh )
            printf( "%s%dx%d", sep, cnts[ j ], j ), sep = ", ";

    if( !*sep )
        printf( "no digit occurred multiple times" );

    putchar( '\n' );
}

int main( void ) {
    int ranges[][2] = {
        {   0,  10, },
        {   0,  22, },
        { 110, 133, },
        { 447, 448, },
    };

    // four trail ranges above
    for( int r = 0; r < sizeof ranges/sizeof ranges[0]; r++ ) {
        int metacnts[ 10 ] = {0};

        // examine each number in the range (inclusive of start & end)
        for( int i = ranges[r][0]; i <= ranges[r][1]; i++ ) {
            int cnts[ 10 ] = {0};

            process( i, cnts );

            // tabulate only 'digits' occurring more than once
            for( int j = 0; j < 10; j++ )
                if( cnts[ j ] > 1 )
                    metacnts[ j ]++;
        }

        // report only digits appearing multiple times in numbers between min & max
        printf( "Range %3d-%3d (incl) ", ranges[r][0], ranges[r][1] );
        report( metacnts, 0 );
    }

    return 0;
}

输出:

Range   0- 10 (incl) no digit occurred multiple times
Range   0- 22 (incl) 1x1, 1x2
Range 110-133 (incl) 12x1, 1x2, 1x3
Range 447-448 (incl) 2x4
© www.soinside.com 2019 - 2024. All rights reserved.