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;
    }
    
    
    
    
    
  • 相关阅读:
    【c++算法】移除性算法
    【c++容器】标准库与boost库中一些容器的介绍
    【Marva Collins' Way】第十章
    pc后时代的vs2012
    【行业关注】决策
    .net控件
    Silverlight 全屏显示
    ImageError error #4001 in control 'Xaml1': AG_E_NETWORK_ERROR 异常
    Silverlight跨域,Silverlight在IIS中部署等问题解决之道
    演练:使用 Expression Blend 或代码创建 Silverlight 时钟
  • 原文地址:https://www.cnblogs.com/xinxiangqing/p/4675889.html
Copyright © 2011-2022 走看看