2016暑期集训---周赛(水题)

今天被这个题前前后后折磨了好几次。其实最开始就想到可能需要用数组来存数。但是做题过程中却偏偏只是一步一步地放大储存容量。我感觉这可能是程序员的一个小小职业病。由于计算机的存储空间以及计算性能是有限的,所以一直精打细算时间跟空间,一种“不浪费时间和空间的思想”让我在这题上栽倒了好几次。

第一次,用int存数。显然,要挂。其实这次WA是不意外的。
第二次,于是把所有的数都换成了long long。结果。还是WA。这次想到如果要把数字的储存方式换成数组的话,代码就需要重构。犯了一下懒,心里跟自己说可能是代码bug。于是花了无谓的半个小时来调BUG。
第三次,这次本来打算把数据类型都换成double。然后本地试了一下。由于精度问题。用double反倒不如用long long,就没有换成double了。
第四次,经过一段时间无谓的折腾之后,把代码全删了重写。数字的储存方式换成了数组。结果又WA了。这次就有点不可理解了。我明明都已经用数组存数了。还这样…
第五次,又仔细看了一下题。看到这句the total length of all these number’s representations doesn’t exceed 100 000.我就想,会不会有某个数的长度就是100000.又回头看了一下我的代码,数组的大小是10000,于是把数组大小换成了100010.终于才过.

【题面】
Description
It’s the year 4527 and the tanks game that we all know and love still exists. There also exists 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 their product. If it is turns to be too large, then the servers might have not enough time to assign tanks into teams and the whole game will collapse!

There are exactly n distinct countries in the world and the i-th country added ai tanks to the game. As the developers of the game are perfectionists, the number of tanks from each country is beautiful. A beautiful number, according to the developers, is such number that its decimal representation consists only of digits ‘1’ and ‘0’, moreover it contains 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 to write the program that solves exactly the same problem in order to verify Gena’s code correctness. Just in case.

Input
The first line of the input contains the number of countries n (1 ≤ n ≤ 100 000). The second line contains n non-negative integers ai without leading zeroes — the number of tanks of the i-th country.

It is guaranteed that the second line contains at least n - 1 beautiful numbers and the total length of all these number’s representations doesn’t exceed 100 000.

Output
Print a single number without leading zeroes — the product of the number of tanks presented by each country.

Sample Input

Input
3
5 10 1
Output
50

Input
4
1 1 10 11
Output
110

Input
5
0 3 1 100 1
Output
0

【题目大意】
给定n个无前导零的数。
规定如果这个数是1个1后面跟几个零。那么就是漂亮数,比如1, 10, 100, 1000, 10000, …
输入保证这n个数最多之后1个非漂亮数。
给出这n个数相乘的结果。
【解法】
如果这n个数里面有0.可直接输出0
如果没有零:
判断这n个数里有没有非漂亮数。
如果有非漂亮数,将这个非漂亮数作为结果的前缀输出。
如果没有非漂亮数,将1作为结果的前缀输出。
最终将所有漂亮数得后缀0个数总数输出。
【坑点】必须用数组存数字。
【AC代码】

#include 
#include 
#include 
#include 
#include 

using namespace std;
char not_beautiful[100010];
bool mark;
char num[100010];
int tal;

int count(char * num){
    int len = strlen(num);
    int ans = 0;
    for(int i = len - 1; i >= 0; i--){
        if(num[i] == '0'){
            ans++;
        }
        else{
            break;
        }
    }
    if(ans == len - 1 && num[0] == '1'){                                         ///漂亮数
        return ans;
    }
    else{                                                       ///不是漂亮数
        return -1;
    }
}

int main(){
    int n;
    while(scanf("%d", &n) != EOF){                              ///多组数据
        memset(not_beautiful, 0, sizeof(not_beautiful));        ///初始化
        mark = 0;                                               ///初始化
        tal = 0;
        for(int i = 0; i < n; i++){                             ///读入n个数
            scanf("%s", num);
            if(num[0] == '0'){                                    ///当前这个数是0
                mark = 1;
            }
            else{                                               ///当前这个数不是0
                int cnt = count(num);
                if(cnt == -1){                                 ///当前这个数不是漂亮数
                    memcpy(not_beautiful, num, sizeof(num));
                }
                else{                                           ///当前这个数是漂亮数
                    tal += cnt;
                }
            }
        }

        if(mark == 1){                                          ///有0
            printf("0\n");
        }
        else{                                                   ///没0
            if(not_beautiful[0] == 0){                          ///没有不漂亮数
                printf("1");
            }
            else{                                               ///有不漂亮数
                int l = strlen(not_beautiful);
                for(int i = 0; i < l; i++){
                    printf("%c", not_beautiful[i]);
                }
            }

            for(int i = 0; i < tal; i++){
                printf("0");
            }
            printf("\n");
        }

    }

    return 0;
}

你可能感兴趣的:(2016暑期集训,水题)