输入包括两行,第一行包括一个整数n(1 ≤ n ≤ 50),即玩家的人数 第二行n个整数x[i](0 ≤ x[i] ≤ 100000),即每个玩家写下的整数。
输出一个整数,表示赢得游戏的那个玩家获得的最大数字是多少。
3 9638 8210 331
3689
while(line=readline()){ let line2=readline() function ans(line2) { return Math.max(...line2.split(' ').map((ele)=>{return parseInt(ele.split('').sort((a,b)=>{return a-b}).join('').replace(/\b(0+)/gi,""))})) } print(ans(line2)) }
import java.util.*; public class Main { private static final int INT_MIN = -0x3f3f3f3f; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int max = INT_MIN; for (int i=0; i!=n; i++) { char[] inc = sc.next().toCharArray(); Arrays.sort(inc); max = Math.max(max, Integer.parseInt(new String(inc))); } System.out.println(max); } }
import java.util.*; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner cin=new Scanner (System.in); int n=cin.nextInt();//玩家人数(数字数量) String str[]=new String [n]; for(int i=0;i<n;i++) { String s=cin.next(); String s1=""; int b[]=new int [s.length()]; for(int j=0;j<s.length();j++) { b[j]=Integer.parseInt(String.valueOf(s.charAt(j))); } Arrays.sort(b); int dd=0; if(b[dd] !=0)str[i]=String.valueOf(b[dd]); else { dd++; str[i]=String.valueOf(b[dd]); } for(int j=dd+1;j<b.length;j++) { if(j==0 && b[j]==0)continue; else str[i]=str[i]+String.valueOf(b[j])+s1; } } int max=Integer.parseInt(str[0]); for(int i=1;i<n;i++) { max=Math.max(max, Integer.parseInt(str[i])); } System.out.print(max); } }
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
//对于每一个数字将其数位按照非递减顺序排列 int ChangNum(int x){
vector<int> v1;
int m, n;
while (1){
m = x % 10;
v1.push_back(m);
x /= 10;
if (x == 0)
break;
}
sort(v1.begin(), v1.end());
int rec = 0;
for (int i = 0; i < v1.size(); i++){
rec *= 10;
rec += v1[i];
}
return rec;
}
int main(){
int n;
cin >> n;
vector<int> a;
int x;
for (int i = 0; i < n; i++){
cin >> x;
a.push_back(x);
}
for (int i = 0; i < n; i++){
a[i]=ChangNum(a[i]);
}
sort(a.begin(), a.end());
int count = *(a.end() - 1);
cout << count << endl;
system("pause");
return 0;
}
思想:利用 % / 1.先将数字拆分保存 2.重新排列数字 3.利用乘法得到新的数 4.比较大小 #include <iostream> #include <vector> #include <algorithm> #include <string> using namespace std; int main() { int n; cin>>n; int mid; vector<int> m; for(int i=0;i<n;i++) { cin>>mid; m.push_back(mid); } int res; int str=0; for(int i=0;i<n;i++) { int fin=0; vector<int> s; while(m[i]>=1) { res=m[i]%10; s.push_back(res); m[i]=m[i]/10; } sort(s.begin(),s.end()); for(int i=0;i<s.size();i++) { fin =fin*10+s[i]*10; } fin=fin/10; if(fin > str) { str=fin; } s.clear(); } cout<<str<<endl; return 0; }
def result(num):
if not num//10:
return num
L = list()
while num//10:
L.append(num%10)
num //= 10
if not num // 10:
L.append(num)
L.sort()
obj = 0
for item in L:
obj = obj*10+item
return obj
input()
print(max(map(result,list(map(int,input().split())) )))
n,arr=int(input()),list(input().split()) l=[] for x in arr: l.append(''.join(sorted(x))) print(max(list(map(int,l))))
#include<iostream> #include<algorithm> using namespace std; int main(){ int n; cin>>n; int* x=new int[n]; int* rev=new int[n]; for(int i=0;i<n;i++){ cin>>x[i]; int a[6],count=0,temp1=x[i],temp2=0; while(temp1>0){ a[count++]=temp1%10; temp1/=10; } sort(a,a+count); for(int j=0;j<count;j++) temp2=temp2+a[j]; rev[i]=temp2; } sort(rev,rev+n); cout<<rev[n-1]<<endl; }
#include<iostream> #include<stdio.h> #include<algorithm> #include<string> #include<vector> #include<math.h> using namespace std; int transfer(int a){ string s = to_string(a); sort(s.begin(), s.end()); return atoi(s.c_str()); } int main(){ int n; cin >> n; int x[55]; //十进制数,无前缀0 int max = -1; for(int i = 0; i < n; i++){ cin >> x[i]; if(transfer(x[i]) > max){ max = transfer(x[i]); } } //数位按照非递减顺序排列 3689 0128 133 cout << max << endl; }
这道题的用例极其不严谨!我一开始是这样实现的:
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main() {
size_t n;
while(cin>>){
int res = -1;
string x;
for (size_t i = 0; i < n; i++) {
cin >> x;
sort(x.begin(), x.end());
res = max(res, atoi(x.c_str()));
}
cout<<res;
}
}
然后通过80%的样例,有一个样例居然是:
5
1 10 100 1000 10000 100000
n=5居然输入了6个数,导致我上面的代码的while循环体执行了两次,导致样例不通过,试问我如果是在笔试模式如何发现得了这种错误??
改代码如下通过:
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main() {
size_t n;
cin>>n;
int res = -1;
string x;
for (size_t i = 0; i < n; i++) {
cin >> x;
sort(x.begin(), x.end());
res = max(res, atoi(x.c_str()));
}
cout<<res;
}
强烈谴责出题人的不严谨的态度!
#include <bits/stdc++.h> using namespace std; int F(int n){ int s=0,k=0; vector<int> v; while(n){ v.push_back(n%10); n /= 10; k++; } sort(v.begin(),v.end()); for(int i=0;i<v.size();i++) s = s*10+v[i]; return s; } int main() { int n,x; cin>>n int a[n],Max=0; for(int i=0;i<n;i++){ cin>>x; Max = max(F(x),Max); } cout<<Max<<endl; return 0; }
n = eval(input()) X = input().strip().split() Y = [] for x in X: y = [] for a in x: y.append(a) Y.append("".join(sorted(y))) Z = list(map(int, Y)) print(max(Z))
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; /** * @author wylu */ public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br.readLine(); int res = Integer.MIN_VALUE; for (String s : br.readLine().split(" ")) { char[] num = s.toCharArray(); Arrays.sort(num); res = Math.max(res, Integer.parseInt(new String(num))); } System.out.println(res); } }
import java.util.*; public class Main{ public static void main(String[] args){ try(Scanner in = new Scanner(System.in)){ int n = in.nextInt(),i = 0; String[] str = new String[n]; while(i < n){ str[i] = in.next(); i++; } System.out.println(helper(str)); } } public static int helper(String[] str){ int max = 0; for(String s:str){ char[] a = s.toCharArray(); Arrays.sort(a); max = Math.max(Integer.parseInt(new String(a)),max); } return max; } }
#include<bits/stdc++.h> using namespace std; int main() { int n;cin>>n; vector<int> v; getchar(); string t; for(int i=0;i<n;i++) { cin>>t; sort(t.begin(),t.end()); for(int j=0;t[j]=='0';) t.erase(0,1); v.push_back(stoi(t)); } sort(v.begin(),v.end()); cout<<v[n-1]<<endl; }