zoukankan      html  css  js  c++  java
  • ACM判断所给数字能否表示所有的数字

    Description

    A magic island Geraldion, where Gerald lives, has its own currency system. It uses banknotes of several values. But the problem is, the system is not perfect and sometimes it happens that Geraldionians cannot express a certain sum of money with any set of banknotes. Of course, they can use any number of banknotes of each value. Such sum is called unfortunate. Gerald wondered: what is the minimumunfortunate sum?

    Input

    The first line contains number n (1 ≤ n ≤ 1000) — the number of values of the banknotes that used in Geraldion.

    The second line contains n distinct space-separated numbers a1, a2, ..., an (1 ≤ ai ≤ 106) — the values of the banknotes.

    Output

    Print a single line — the minimum unfortunate sum. If there are no unfortunate sums, print  - 1.

    Sample Input

    Input
    5
    1 2 3 4 5
    Output
    -1
    解题思路:
    这个题目的意思就是让我们判断所给的所有的数字能不能表示所有的数字(即通过相互加减可以表示所有的数字)。如果能我们就输出-1,如果没有我们就要找到一个最小的数,使得这里的所有的数能表示所有的数;我们知道在这中情况下只要这一串数中有一个1就行了,就之一同过以表示所有的数了。举个例子如果我们输入的数中没有1,那么我们就无法表示1这个数了,但是如果有一个1,我们就可以表示所有的数字了。
    程序代码:
    #include <iostream>
    using namespace std;
    int a[1005];
    int main()
    {
        int n,first=0;
        cin>>n;
        for(int i=0;i<n;i++)
            cin>>a[i];
        for(int j=0;j<n;j++)
        {
            if(a[j]==1)
            {
                first=1;
                break;
            }
        }
        if(first)
            cout<<"-1"<<endl;
        else 
            cout<<"1"<<endl;
        return 0;
    }
    
    
    
    
    
  • 相关阅读:
    1442. Count Triplets That Can Form Two Arrays of Equal XOR
    1441. Build an Array With Stack Operations
    312. Burst Balloons
    367. Valid Perfect Square
    307. Range Sum Query
    1232. Check If It Is a Straight Line
    993. Cousins in Binary Tree
    1436. Destination City
    476. Number Complement
    383. Ransom Note
  • 原文地址:https://www.cnblogs.com/xinxiangqing/p/4675889.html
Copyright © 2011-2022 走看看