解题思路:
系统不严谨,说的是按字典序排列的,但是提交从小到大依然正确,我在这里写写按字典序的,其他的前面的大佬们已经解释到位了,我这里使用了一个冒泡排序,使用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语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:515 |
2005年春浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:651 |
C语言程序设计教程(第三版)课后习题5.5 (C语言代码)浏览:561 |
1024题解浏览:820 |
A+B for Input-Output Practice (VI) (C语言代码)浏览:559 |
大家好,我是验题君浏览:578 |
DNA (C语言代码)浏览:754 |
复数求和 (C语言代码)浏览:929 |
格式化数据输出 (C语言代码)浏览:824 |
矩阵的对角线之和 (C语言代码)浏览:1359 |