您的位置:首页 > 其它

解题报告:codeforces #339(Div.2)B.Gena's Code

2016-01-15 18:25 330 查看
B. Gena's Code
time limit pertest
0.5 seconds
memory limit pertest
256 megabytes
input
standard input
output
standard output
It's the year4527 and the tanks game that we all know and love still exists. There alsoexists Great Gena's code, written in 2016. The problem this code solves is:given the number of tanks that go into the battle from
each country, find theirproduct. If it is turns to be too large, then the servers might have not enoughtime to assign tanks into teams and the whole game will collapse!
There areexactly n distinct countries in the world and the i-th
countryadded ai tanks tothe game. As the developers of the game are perfectionists, the number of tanksfrom each country is beautiful.
A beautiful number, accordingto the developers, is such number that its decimal representation consists onlyof digits '1' and '0',
moreover itcontains at most one digit '1'. However, due to complaints from players,some number of tanks of one country was removed from the game,hence the
number of tanks of this country may not remain beautiful.
Your task is towrite the program that solves exactly the same problem in order to verifyGena's code correctness. Just in case.
Input
The first lineof the input contains the number of countries n (1 ≤ n ≤ 100 000).
The secondline contains n non-negative integers aiwithout
leadingzeroes — the number of tanks of the i-th country.
It is guaranteedthat the second line contains at least n - 1 beautifulnumbers and the total length
of all these number's representations doesn'texceed 100 000.
Output
Print a singlenumber without leading zeroes — the product of the number of tanks presentedby each country.
Sample test(s)
input
3

5 10 1
output
50
input
4

1 1 10 11
output
110
input
5

0 3 1 100 1
output
0
Note
In sample 1numbers 10 and 1 are beautiful, number 5 is not not.
In sample 2number 11 is not beautiful (contains two '1's), all othersare beautiful.
In sample 3number 3 is not beautiful, all others are beautiful.

题目大意及思路:给你一些数,求乘积,beautifulnumbers的设置降低了难度:最多只会有一个不美的数将其记录下来,其余的都是10的指数的形式,则只用记录长度就好最后输出几个零就好。需要注意的是可能全为合法数,这是需要先输出一个1。

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

char A[100000+5],B[100000+5];

int main()
{
int n;
while(scanf("%d",&n)==1)
{
int ans=0,m=1,k=0;
memset(B,'\0',sizeof(B));
while(n--)
{
scanf("%s",A);
if(A[0]=='0')
ans=-1;
if(ans==-1)
continue;

int len=strlen(A);
if(k)
{
ans+=len-1;
}
else
{
for(int i=0,num=0;A[i]!='\0'&&!k;i++)
{
if(A[i]=='1')
num++;
if(A[i]!='1'&&A[i]!='0')
k=1;
if(num>1)
k=1;
}
if(k)
strcpy(B,A);
else
ans+=len-1;
}
}
if(ans!=-1)
{
if(k)
printf("%s",B);
else
printf("1");
for(int i=0;i<ans;i++)
printf("0");
}
else
printf("0");
printf("\n");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: