怪獸數列(5位數練習)

 avatar
user_6817964
c_cpp
2 years ago
615 B
2
Indexable
Never
#include <stdio.h> // printf
int main()
{
    int n;
    scanf_s("%d", &n);
    int a[5];
    a[0] = n / 10000;
    n %= 10000;
    a[1] = n / 1000;
    n %= 1000;
    a[2] = n / 100;
    n %= 100;
    a[3] = n / 10;
    n %= 10;
    a[4] = n;

    int sum[5] = {1, 1, 1, 1, 1};

    for (int i = 4; i >= 0; i--) {
        for (int j = 0; j < i; j++)
            if (a[i] == a[j]) {
                sum[j] += 1;
                sum[i] = 0;
        }
    }
    for (int i = 0; i <= 4; i++) {
        if (sum[i] != 0) {
            printf("%d%d", sum[i], a[i]);
        }
    }
    
}