解题思路:
系统不严谨,说的是按字典序排列的,但是提交从小到大依然正确,我在这里写写按字典序的,其他的前面的大佬们已经解释到位了,我这里使用了一个冒泡排序,使用strcmp函数的特性(按字典序比较大小),来给数组排序;
strcmp(s1,s2):(字典序)
s1 > s2 返回 1
s1 = s2 返回0
s1 < s2 返回-1
注意事项:
参考代码:
#include <stdio.h> #include <cstdlib> #include <iostream> #include <cmath> #include <cstring> #include <string> #include <algorithm> #include <functional> using namespace std; char s[10][100]; long long fact(int n) { int ans = 1; for(int i = n; i >= 2; --i) { ans *= i; } return ans; } long long f(int n) { long long sum = 0; while(n) { sum += fact(n%10); n /= 10; } return sum; } int main() { int j = 0; for(int i = 1; i <= 100000; ++i) { if(i == f(i)) { sprintf(s[j], "%d ", i); j++; } } for(int i = 0; i < j-1; ++i) { for(int k = 0; k < j-1-i; ++k) { if(strcmp(s[k], s[k+1]) > 0) { char tmp[10]; strcpy(tmp,s[k]); strcpy(s[k],s[k+1]); strcpy(s[k+1],tmp); } } } for(int i = 0; i < j; ++i) { printf("%s ", s[i]); } return 0; }
0.0分
1 人评分
C语言程序设计教程(第三版)课后习题6.1 (C语言代码)浏览:452 |
C语言程序设计教程(第三版)课后习题6.1 (C语言代码)浏览:520 |
用筛法求之N内的素数。 (C语言代码)浏览:1253 |
C语言训练-计算t=1+1/2+1/3+...+1/n (C语言代码)浏览:906 |
【绝对值排序】 (C语言代码)浏览:821 |
打印十字图 (C语言代码)浏览:2710 |
简单的a+b (C语言代码)浏览:579 |
C语言程序设计教程(第三版)课后习题9.4 (C语言代码)浏览:644 |
程序员的表白 (C语言代码)浏览:598 |
C语言程序设计教程(第三版)课后习题1.6 (C语言代码)浏览:672 |