from:http://www.cnblogs.com/suncoolcat/archive/2013/08/25/3281447.html
题目1531:货币面值
时间限制:1 秒
内存限制:128 兆
特殊判题:否
提交:320
解决:119
- 题目描述:
-
小虎是游戏中的一个国王,在他管理的国家中发行了很多不同面额的纸币,用这些纸币进行任意的组合可以在游戏中购买各种装备来提升自己。有一天,他突然很想知道这些纸币的组合不能表示的最小面额是多少,请聪明的你来帮助小虎来解决这个财政问题吧。
题目1531:货币面值
时间限制:1 秒内存限制:128 兆特殊判题:否提交:320解决:119
题目描述:
小虎是游戏中的一个国王,在他管理的国家中发行了很多不同面额的纸币,用这些纸币进行任意的组合可以在游戏中购买各种装备来提升自己。有一天,他突然很想知道这些纸币的组合不能表示的最小面额是多少,请聪明的你来帮助小虎来解决这个财政问题吧。
输入:
输入包含多个测试用例,每组测试用例的第一行输入一个整数N(N<=100)表示流通的纸币面额数量,第二行是N个纸币的具体表示面额,取值[1,100]。
输出:
对于每组测试用例,输出一个整数,表示已经发行的所有纸币都不能表示的最小面额(已经发行的每个纸币面额最多只能使用一次,但面值可能有重复)。
样例输入:
5
1 2 3 9 100
5
1 2 4 9 100
5
1 2 4 7 100
样例输出:
7
8
15
来源:
网易游戏2013年校园招聘笔试题
思路:动态规划的思想, 对于从第1个到第i个数的和total,
如果第i+1个数大于total+1则不会组成total+1.
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;int n;
vector<int> v;void work() {
int i;
int total = 0;
if(v[0] != 1){
printf("1\n"); return;
}
else {
total = v[0];
for(i = 1; i < n; i++ ) {
if(v[i] <= total + 1) {
total += v[i];
} else {
printf("%d\n", total+1);
return;
}
}
}
printf("%d\n", total+1);
}int main()
{
int i, tmp;
while(scanf("%d", &n) != EOF) {
v.clear();
for(i = 0; i < n; i++) {
scanf("%d", &tmp);
v.push_back(tmp);
}
sort(v.begin(), v.end());
work();
}
return 0;
}
- 输入:
-
输入包含多个测试用例,每组测试用例的第一行输入一个整数N(N<=100)表示流通的纸币面额数量,第二行是N个纸币的具体表示面额,取值[1,100]。
- 输出:
-
对于每组测试用例,输出一个整数,表示已经发行的所有纸币都不能表示的最小面额(已经发行的每个纸币面额最多只能使用一次,但面值可能有重复)。
- 样例输入:
-
5 1 2 3 9 100 5 1 2 4 9 100 5 1 2 4 7 100
- 样例输出:
-
7 8 15
- 来源:
- 网易游戏2013年校园招聘笔试题
- 思路:动态规划的思想, 对于从第1个到第i个数的和total,
如果第i+1个数大于total+1则不会组成total+1. -
#include <iostream> #include <cstdio> #include <vector> #include <algorithm> using namespace std; int n; vector<int> v; void work() { int i; int total = 0; if(v[0] != 1){ printf("1\n"); return; } else { total = v[0]; for(i = 1; i < n; i++ ) { if(v[i] <= total + 1) { total += v[i]; } else { printf("%d\n", total+1); return; } } } printf("%d\n", total+1); } int main() { int i, tmp; while(scanf("%d", &n) != EOF) { v.clear(); for(i = 0; i < n; i++) { scanf("%d", &tmp); v.push_back(tmp); } sort(v.begin(), v.end()); work(); } return 0; }