咖啡


私信TA

用户名:Tianxn

访问量:130026

签 名:

十年OI一场空,不开LL见祖宗。

等  级
排  名 9
经  验 26292
参赛次数 10
文章发表 197
年  龄 22
在职情况 学生
学  校 西安电子科技大学
专  业 软件工程

  自我简介:

解题思路:


系统不严谨,说的是按字典序排列的,但是提交从小到大依然正确,我在这里写写按字典序的,其他的前面的大佬们已经解释到位了,我这里使用了一个冒泡排序,使用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 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区