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;
    }
    
    
    
    
    
  • 相关阅读:
    Aster寻路算法1(转)
    谈谈项目纵向项目验收
    要有兴趣
    用c# 操作 文件的方法
    使用ACT进行测试
    Generated servlet error: keyword cannot be resolved or is not a type
    米尔顿艾瑞克森的催眠引导词
    c# webcliend 来制作 网页搜捕器
    网页木马的解决方案
    用Swing实现数据表格功能
  • 原文地址:https://www.cnblogs.com/xinxiangqing/p/4675889.html
Copyright © 2011-2022 走看看