演算法競賽入門學習 習題3-3
阿新 • • 發佈:2018-12-12
#include<stdio.h> #include<string.h> #include<ctype.h> #include <stdlib.h> #define maxn 10 int count[maxn]; //#define LOCAL int main() { #ifdef LOCAL freopen("input.txt", "r", stdin); #endif int n = 0; scanf("%d", &n); for (int i = 0; i < n; i++) { memset(count, 0, sizeof(count)); int num; scanf("%d", &num); for (int j = 1; j <= num; j++) { char str[10]; //itoa(j, str, 10); snprintf(str, sizeof(str), "%d", j); int k = strlen(str); for (int p = 0; p < k; p++) { count[str[p] - '0']= count[str[p] - '0']+1; } } for (int j = 0; j < 9; j++) { printf("%d ", count[j]); } printf("%d\n", count[9]); } return 0; }
g++不支援itoa
所以將 //itoa(j, str, 10);
改為 snprintf(str, sizeof(str), "%d", j);