用c语言编写程序输入一个正整数n
#include <stdio。h>#include <string。h>int count_one(unsigned int n){ int cnt = 0; char buf[100]; for (int i = 1; i <= n; i++){ sprintf(buf, "%d", i); for(int j = 0;j < strlen(buf); j++)if(buf[j] == '1')cnt++; } return cnt;}int main(){ printf("%d %d %d\n", count_one(1), count_one...全部
#include <stdio。h>#include <string。h>int count_one(unsigned int n){ int cnt = 0; char buf[100]; for (int i = 1; i <= n; i++){ sprintf(buf, "%d", i); for(int j = 0;j < strlen(buf); j++)if(buf[j] == '1')cnt++; } return cnt;}int main(){ printf("%d %d %d\n", count_one(1), count_one(100), count_one(1000)); return 0;}[41@~/c-cpp]$ gcc -Wall --std=c99 test。
c[42@~/c-cpp]$ 。/a。out 1 21 301。收起