题解 | #小红的好数#
小红的好数
https://www.nowcoder.com/practice/de234d0d61d549c6a436e9509dbeea11
从大到小枚举,检查数位是否合法,然后输出即可。
c++代码:
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
vector<string> ans;
bool check(string &s)
{
set<char> chk(s.begin(),s.end());
return chk.size()==s.size();
}
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(0);
string s;
int i,k;
for(i=99999;i>=10000;--i)
{
s=to_string(i);
if(check(s))
ans.emplace_back(s);
}
for(i=9999;i>=1000;--i)
{
s="0"+to_string(i);
if(check(s))
ans.emplace_back(s);
}
cin>>k;
cout<<ans[k-1];
return 0;
}
python代码:
ans=[]
def check(s:str)->bool:
chk=set(s)
return len(chk)==len(s)
for i in range(99999,9999,-1):
s=str(i)
if(check(s)):
ans.append(s)
for i in range(9999,999,-1):
s='0'+str(i)
if(check(s)):
ans.append(s)
k=int(input())
print(ans[k-1])
java代码:
import java.util.*;
public class ZT14 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<String> ans=new ArrayList<>();
int i,k;
for(i=99999;i>=10000;--i){
String s=""+i;
if(check(s))
ans.add(s);
}
for(i=9999;i>=1000;--i){
String s="0"+i;
if(check(s))
ans.add(s);
}
k=sc.nextInt();
System.out.println(ans.get(k-1));
}
public static boolean check(String s){
Set<Character> chk=new HashSet<>();
for(int i=0;i<s.length();i++)
chk.add(s.charAt(i));
return chk.size()==s.length();
}
}