贪心/CodeForces 489C Given Length and Sum of Digits...

 1 #include<cstdio>
 2 using namespace std;
 3 int m,s;
 4 int main()
 5 {
 6     scanf("%d%d",&m,&s);
 7     if (s==0 && m==1) printf("0 0\n");
 8     else if (s==0) printf("-1 -1\n");
 9     else if (s>m*9) printf("-1 -1\n");
10     else
11     {
12         int temp=s;
13         char mmax[110];
14         for (int i=0;i<m;i++) mmax[i]=‘0‘;
15         int d=0;
16         while (temp>9)
17         {
18             mmax[d]=‘9‘;
19             d++;
20             temp-=9;
21         }
22         mmax[d]=temp+‘0‘;
23         mmax[m]=‘\0‘;
24
25         temp=s-1;
26         char mmin[110];
27         for (int i=0;i<m;i++) mmin[i]=‘0‘;
28         mmin[0]=‘1‘;
29         d=m-1;
30         while (temp>9)
31         {
32             mmin[d]=‘9‘;
33             d--;
34             temp-=9;
35         }
36         if (d==0) mmin[d]=temp+1+‘0‘;
37         else mmin[d]=temp+‘0‘;
38         mmin[m]=‘\0‘;
39
40         printf("%s %s\n",mmin,mmax);
41     }
42     return 0;
43 }
时间: 2024-10-12 02:33:43

贪心/CodeForces 489C Given Length and Sum of Digits...的相关文章

Codeforces 489C Given Length and Sum of Digits...

m位长度,S为各位的和 利用贪心的思想逐位判断过去即可 详细的注释已经在代码里啦~ //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #include <iostream> #include <cstring> #include <cmath> #include <stack> #include <queu

codeforces 489C.Given Length and Sum of Digits... 解题报告

题目链接:http://codeforces.com/problemset/problem/489/C 题目意思:给出 m 和 s,需要构造最大和最小的数.满足长度都为 m,每一位的数字之和等于 s.如果构造不出来,输出 -1 -1.否则输出最小和最大且符合条件的数. 想了两个多小时,发现想错了方向...... /****************************************** 首先把不能得到最小数和最大数的情况揪出来. 第二组测试数据 3 0 有提示,s = 0 且 m >

CF 489 C Given Length and Sum of Digits... 贪心

题目链接:http://codeforces.com/problemset/problem/489/C 题目大意:给定位数和各个位的和,问满足条件数字的最大值,最小值. 解题思路:模拟即可.主要是细节判断. 代码: 1 const int inf = 0x3f3f3f3f; 2 const int maxn = 1e2 + 5; 3 int m, s; 4 char ans1[maxn], ans2[maxn]; 5 6 void solve(){ 7 memset(ans1, 0, sizeo

Codeforces Round #277.5 (Div. 2)——C贪心—— Given Length and Sum of Digits

You have a positive integer m and a non-negative integer s. Your task is to find the smallest and the largest of the numbers that have length m and sum of digits s. The required numbers should be non-negative integers written in the decimal base with

Codeforces Round #277.5 (Div. 2)C. Given Length and Sum of Digits...(贪心)

传送门 Description You have a positive integer m and a non-negative integer s. Your task is to find the smallest and the largest of the numbers that have length m and sum of digits s. The required numbers should be non-negative integers written in the d

Codeforces Round #277.5 (Div. 2) C Given Length and Sum of Digits...

大大的传送门:就是这里 这一道卡在了特判的  1   0 本来输出 0  0 输出  -1    -1了,就错了,, 这道题就是一个恨好的贪心,往大了贪 下面是代码 #include <cstdio> #include <cstring> #include <vector> using namespace std; int n,m; int ans[110]; int main(){ scanf("%d%d",&n,&m); mems

C. Given Length and Sum of Digits...

http://codeforces.com/contest/489/problem/C 大数就是从最高位,能大就大:小数就是从最低位,能小就小,再处理下最高位为0的情况. 无结果无非一个sum太小,min全为0,一个sum太大,全为9还有剩 1 public class Main { 2 public static void main(String[] args) { 3 Scanner io = new Scanner(System.in); 4 int len = io.nextInt(),

8504. Sum of Digits

Sum of Digits Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a n

Sum of Digits is Prime

Sum of Digits is Prime Daoyi Peng August 19, 2014 For an integer $q\geqslant2$ let $s_q(n)$ denote the $q$-ary sum-of-digits function of a non-negative integer $n$, that is, if $n$ is given by its $q$-ary digits expansion $n=\sum\limits_{k=0}^{r} a_k