数字游戏
来源:
题目描述:
牛牛举办了一场数字游戏,有n个玩家参加这个游戏,游戏开始每个玩家选定一个数,然后将这个数写在纸上(十进制数,无前缀零),然后接下来对于每一个数字将其数位按照非递减顺序排列,得到新的数,新数的前缀零将被忽略。得到最大数字的玩家赢得这个游戏。
输入描述: 输入包括两行,第一行包括一个整数n(1 ≤ n ≤ 50),即玩家的人数 第二行n个整数x[i](0 ≤ x[i] ≤ 100000),即每个玩家写下的整数。
输出描述:
输出一个整数,表示赢得游戏的那个玩家获得的最大数字是多少。
示例:
输入 3 9638 8210 331
输出
3689
思路:
思路很简单,就是把每个数字的没个位数都拆分出来,然后排序,将0剔除,最后将转换好后的数字再排序,选出最大的即可
AC代码:
#include <bits/stdc++.h>
using namespace std;
int test(int a)
{
int b=0,c[100],pos=0,result=0;
while(a>0)
{
c[pos++]=a%10;
a/=10;
}
sort(c,c+pos);
int res=0;
for(int i=0;i<pos;i++)
if(c[i]!=0)
res=res*10+c[i];
return res;
}
int main()
{
int n,s[100];
cin>>n;
for(int i=0;i<n;i++){
cin>>s[i];
s[i]=test(s[i]);
}
sort(s,s+n);
cout<<s[n-1]<<endl;
}
最后修改于 2021-01-22

本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。