标签:
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
5
1 2 3 4 5
-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; }
标签:
原文地址:http://www.cnblogs.com/xinxiangqing/p/4675889.html