CF 489 C Given Length and Sum of Digits...(水题)
2016-03-20 21:58
381 查看
题目链接:
CF 489 C Given Length and Sum of Digits…
题意:
给出m和s找到位数为m各位上数字和为s的最大数和最小数,不要前导0,如果找不到输出-1.
1<=m<=100,0<=s<=300.
分析:
贪心思想。最大值一定是9尽量靠前,最小数一定是9尽量靠后,但是要保证首位至少为1.最大值不存在那就是每位上数字都为9仍然满足不了和为s,最小值不存在那就是s为0但是m>1.
注意:
s=0,m=1时需要特判。
CF 489 C Given Length and Sum of Digits…
题意:
给出m和s找到位数为m各位上数字和为s的最大数和最小数,不要前导0,如果找不到输出-1.
1<=m<=100,0<=s<=300.
分析:
贪心思想。最大值一定是9尽量靠前,最小数一定是9尽量靠后,但是要保证首位至少为1.最大值不存在那就是每位上数字都为9仍然满足不了和为s,最小值不存在那就是s为0但是m>1.
注意:
s=0,m=1时需要特判。
//31MS 2000K #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> using namespace std; const int maxn=110; int m,s; char s1[maxn],s2[maxn]; int main() { while(~scanf("%d%d",&m,&s)){ if(s>9*m||(s==0&&m>1)){ printf("-1 -1\n"); continue; } else if(s==0&&m==1){//Special printf("0 0\n"); continue; } s1[m]=s2[m]='\0'; int tmp=s; for(int i=0;i<m;i++){//MAX if(tmp>=9){ s1[i]=9+'0'; tmp-=9; }else if(tmp>0){ s1[i]=tmp+'0'; tmp=0; }else{ s1[i]='0'; } } tmp=s-1; for(int i=m-1;i>=0;i--){//MIN if(tmp>9){ s2[i]=9+'0'; tmp-=9; }else if(tmp>0){ s2[i]=tmp+'0'; tmp=0; }else{ s2[i]='0'; } } s2[0]+=1; printf("%s %s\n",s2,s1); } return 0; }
相关文章推荐
- Codeforces Round #197 (Div. 2)
- Codeforces Round #198 (Div. 1)
- Codeforces 405E Codeforces Round #238 (Div. 2)E
- Codeforces 407C Codeforces Round #239 (Div. 1)C
- CodeForces 449A - Jzzhu and Chocolate
- CodeForces 449 B. Jzzhu and Cities
- codeforces 618C. Constellation
- Codeforces Round #265 (Div. 2)
- Codeforces #310 div2 C. Case of Matryoshkas
- 状态压缩DP codeforces 244 Problem C. The Brand New Function 和 codeforces 165 E. Compatible Numbers
- codeforces 16 Problem E fish
- Codeforces Round332 部分题解
- CodeForces 603A_Alternative Thinking (DP)
- CodeForces 602B_Approximating a Constant Range_DP
- Codeforces round #247 for Div. 2
- Codeforces Round #246 (Div. 2)
- Codeforces #264(div 2)D.Gargari and Permutations
- Codeforces Round #236 (Div. 2)------A,B
- codeforces 257 div2 B
- Codeforces Gym100571A Cursed Query