定义为:每个数字的十进制表示中(0~9),每个数位各不相同且各个数位之和等于N。
满足条件的数字可能很多,找到其中的最小值即可。
多多君还有很多研究课题,于是多多君找到了你--未来的计算机科学家寻求帮助。
数据范围:
进阶:空间复杂度 ,时间复杂度
进阶:空间复杂度 ,时间复杂度
共一行,一个正整数N,如题意所示,表示组合中数字不同数位之和。
(1 <= N <= 1,000)
共一行,一个整数,表示该组合中的最小值。
如果组合中没有任何符合条件的数字,那么输出-1即可。
5
5
符合条件的数字有:5,14,23,32,41其中最小值为5
12
39
50
-1
没有符合条件的数字 (T▽T)
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); if (n < 10) { System.out.println(n); return; } if (n > 45) { System.out.println(-1); return; } String s = "1, 2, 3, 4, 5, 6, 7, 8, 9, 19, 29, 39, 49, 59, 69, 79, 89, 189, 289, 389, 489, 589, 689, 789, 1789, 2789, 3789, 4789, 5789, 6789, 16789, 26789, 36789, 46789, 56789, 156789, 256789, 356789, 456789, 1456789, 2456789, 3456789, 13456789, 23456789, 123456789"; String[] strings = s.split(",\\s"); System.out.println(strings[n - 1]); } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); int N = scanner.nextInt(); //每个数位各不相同且各个数位之和等于N——1+2+3+4+5+6+7+8+9 = 45,如果大于45一定会重复 if(N > 45){ System.out.println(-1); return; } //如果N<10,可以直接返回数字本身 if(N < 10){ System.out.println(N); return; } //右侧数位越大,越能保证左侧数位越小,越能保证整个数最小 int nums = 0; int digit = 0; for(int i = 9; i>0; i--){ if(N != 0 && i <= N){ N -= i; nums += (int)Math.pow(10,digit)*i; digit++; } } System.out.println(nums); } }
N=input() N=int(N) if N>0 and N<=17: m=N//9 n=N%9 print((n+1)*10**m-1) elif N<=24: n=N-17 print((n+1)*10**2-11) elif N<=30: n=N-24 print((n+1)*10**3-211) elif N<=35: n=N-30 print((n+1)*10**4-3211) elif N<=39: n=N-35 print((n+1)*10**5-43211) elif N<=42: n=N-39 print((n+1)*10**6-543211) elif N<=44: n=N-42 print((n+1)*10**7-6543211) elif N==45: print(123456789) else: print(-1)
import java.util.Scanner; /** * @author xq * @create 2021-05-20-15:13 */ public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int N = scanner.nextInt(); if (N > 45){ System.out.println(-1); System.exit(0); } StringBuilder stringBuilder = new StringBuilder(); for (int i = 9; i > 0; i--) { if (N>=i){ N-=i; stringBuilder.insert(0,i); } } System.out.println(stringBuilder.toString()); } }
#include<iostream> #include<vector> #include<math.h> using namespace std; int getMinByN(int N) { if (N < 10) return N; if (N > 45) return -1; int m = N; vector<int> stk; for (int i = 9; i > 0; i--) { if (m != 0 && i<=m) { stk.push_back(i); m -= i; } } int res = 0; for (int i = 0; i < stk.size(); i++) { res += stk[i] * pow(10, i); } return res; } int main() { int N; cin >> N; int res = getMinByN(N); cout << res << endl; system("pause"); return 0; }
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); if(n < 10){ System.out.println(n); }else if(n > 45){ System.out.println(-1); }else{ int num = 0; int index = 0; for(int i = 9; i > 0; i--){ // 从9开始自减遍历,将大数放在低位来保证结果最小 if(n > 0 && i <= n){ n -= i; // i用完了就减掉 num += (int) Math.pow(10, index) * i; index ++; } } System.out.println(num); } } }scala版
import scala.io.StdIn object Main { def main(args: Array[String]): Unit = { var n = StdIn.readInt if(n < 10){ println(n) }else if(n > 45){ println(-1) }else{ var num = 0 var index = 0 for(i <- 9 to 1 by -1){ if(n > 0 && n >= i){ n -= i num += (i * math.pow(10, index)).toInt index += 1 } } println(num) } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int input = sc.nextInt(); sc.close(); if (input>45){ System.out.println(-1); return; } if (input<=9){ System.out.println(input); return; } // 欲使得到的数最小,我们应尽量使越高的位的数字越小 // 即最后得到的数看起来像是一个递增的数列 // 换言之,低位的数越大越好 // 即个位最好为9,十位最好为8(如果存在更高的位的话) int res = 0;//拼接结果 int bit = 0; while (input>9-bit){ res+=(9-bit)*Math.pow(10,bit); input-=9-bit++; } res+=input*Math.pow(10,bit); System.out.println(res); } }
#include<stdio.h> c语言版本~ int main(void){ int N; while(~scanf("%d",&N) != 0) if(N>45){ printf("-1"); return 0; }else{ double s[45] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 19, 29, 39, 49, 59, 69, 79, 89, 189, 289, 389, 489, 589, 689, 789, 1789, 2789, 3789, 4789, 5789, 6789, 16789, 26789, 36789, 46789, 56789, 156789, 256789, 356789, 456789, 1456789, 2456789, 3456789, 13456789, 23456789, 123456789}; printf("%0.0f",s[N-1]); return 0; } }
N = int(input()) list = [1,2,3,4,5,6,7,8,9] if N<10: print(N) elif N>45: print(-1) else: flag = -1 temp = 0 for i in list: temp = temp + i if N-temp>0 and N-temp<=9: flag = N-temp break templist = list[0:i] templist.append(flag) while 1: lastlen = len(templist) for j in range(len(templist)-1,0,-1): k = j - 1 while templist[j] + templist[k] >= 10&nbs***bsp;templist[j] + templist[k] in templist: k = k -1 if k < 0: break if templist[j] + templist[k] < 10 and templist[j] + templist[k] not in templist: templist.append(templist[j]+templist[k]) remove1 = templist[j] remove2 = templist[k] templist.remove(remove1) templist.remove(remove2) templist.sort() break newlen = len(templist) if lastlen==newlen: break result = 0 for inum in range(len(templist) - 1, -1,-1): result = templist[inum]*(10**(len(templist) - 1-inum)) + result print(result)
#include <cmath> #include <iostream> using namespace std; int main() { int N; cin >> N; int ans = 0; if (N <= 9) { cout << N; return 0; } if (N > 45) { cout << -1; return 0; } int count = 0, minus = 9, remainder = N; while (remainder > minus) { remainder -= minus; ans += minus * pow(10, count); minus--; count++; } ans += remainder * pow(10, count); cout << ans; return 0; }
#include <iostream> using namespace std; int main() { int n; cin >> n; if (n > 45) { cout << -1 << endl; return 0; } int ret = 0, crnt = 9, mul = 1; while (n) { if (n >= crnt) { ret = ret + crnt * mul; n -= crnt; mul *= 10; } crnt--; } cout << ret << endl; return 0; }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int N=in.nextInt(); if(N<10){ System.out.println(N); } else if(N>45){ System.out.println(-1); } else{ int ans=0; int digit=0; for(int i=9;i>0;i--){ if(i<=N){ N-=i; ans+=(int)Math.pow(10,digit)*i; digit++; } else{ ans+=(int)Math.pow(10,digit)*N; break; } } System.out.println(ans); } } }
C++ #include <iostream> using namespace std; int main() { int a; while (cin >> a) { // 注意 while 处理多个 case if(a > 45) { cout<<-1<<endl; continue; } int sum = 0; int d = 9; int e = 1; while(a > d) { a -= d; sum += e*d; d--; e *= 10; } sum += e*a; cout<<sum<<endl; } }