您的位置:首页 > 其它

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

2016-08-16 17:36 726 查看
http://codeforces.com/problemset/problem/489/C

题意:给两个数N和S,让你构造一个数,这个数是N位的,且这个数的所有位之和为S,如果能构造,输出最大的和最小的数,如果不能构造,则输出-1 -1.

做法:对于最大数的构造,只要从最高位开始,能放多少就放多少,放到不能放为止。对于最小数的构造,第一位先放1,然后从最后一位开始放,注意几种特殊情况的判断。

#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);

#define INF 0x3f3f3f3f
#define eps 1e-6
typedef long long LL;
const double pi = acos(-1.0);
const long long mod = 1e9 + 2015;
using namespace std;

int a[105];
int b[105];

int main()
{
ios_base::sync_with_stdio(false); cin.tie(0);
//freopen("int.txt","r",stdin);
//freopen("out.txt","w",stdout);
int N,S;
cin >> N >> S;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
int S1 = S;
if(S == 0 && N == 1)
puts("0 0");
else if(S == 0 || S > 9 * N)
puts("-1 -1");
else
{
for(int i = 1;i <= N;i++)
{
if(S == 0)
a[i] = 0;
else
{
a[i] = min(9,S);
S -= a[i];
}
}
b[1] = 1;
S1 -= 1;
for(int i = N;i > 0;i--)
{
if(S1 == 0)
b[i] += 0;
else
{
b[i] += min(9,S1);
S1 -= b[i];
}
}
for(int i = 1;i <= N;i++)
printf("%d",b[i]);
printf(" ");
for(int i = 1;i <= N;i++)
printf("%d",a[i]);
puts("");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: