SOJ 3300_Stockholm Coins
【题意】给n个数,求一个数,使这个数能且只能由(n个数每个至少出现一次)表示。输出满足条件的最小的数。
【分析】(完全背包)如果有满足条件的最小的数,那么这个数只能是这n个数的和total,通过记录每个可能和的组合数,求出total的组合数,如果为1则表示满足条件,即n个数每个正好出现一次,若>1,则找不到这样的数,即输出-1。
【代码】
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int n,total;
int b[35];
int v[350000];
int cash()
{
total=0;
scanf("%d",&n);
memset(v,0,sizeof(v));
v[0]=1;
for(int i=0;i<n;i++)
{
scanf("%d",&b[i]);
total+=b[i];
}
for(int i=0;i<n;i++)
{
for(int j=b[i];j<=total;j++)
v[j]+=v[j-b[i]];
}
if(v[total]==1)
return total;
else
return -1;
}
int main (void)
{
int c;
scanf("%d",&c);
while(c--)
{
getchar();
printf("%d\n",cash());
}
return 0;
}
SOJ 3300_Stockholm Coins的更多相关文章
- SOJ 2749_The Fewest Coins
[题意]:已知整个交易系统有N (1 ≤ N ≤ 100)种不同的货币,分别价值V1,V2,V3.......VN(1 ≤ Vi ≤ 120),FJ分别有C1,C2,C3.....CN(0 ≤ Ci ...
- Soj题目分类
-----------------------------最优化问题------------------------------------- ----------------------常规动态规划 ...
- [LeetCode] Arranging Coins 排列硬币
You have a total of n coins that you want to form in a staircase shape, where every k-th row must ha ...
- ACM: Gym 101047M Removing coins in Kem Kadrãn - 暴力
Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS Memory Limit:65536KB 64bit IO Fo ...
- Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)
传送门 Description Hasan and Bahosain want to buy a new video game, they want to share the expenses. Ha ...
- 【贪心】SOJ 13983
SOJ 13983. Milk Scheduling 这是比赛题,还是作死的我最讨厌的英文题,题目大意就是有n头奶牛,要在喂奶截止时间前给他喂奶并得到相应的含量的牛奶. 一开始的想法就是挑选截止日期的 ...
- csuoj 1119: Collecting Coins
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1119 1119: Collecting Coins Time Limit: 3 Sec Memo ...
- Coins
Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One day Hi ...
- hdu 1398 Square Coins (母函数)
Square Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
随机推荐
- $.extend(x,y); 函数用法介绍。
第一篇资料: 转自: https://www.cnblogs.com/yuqingfamily/p/5813650.html 语法:jQuery.extend( [deep ], target, o ...
- Wamp搭建的服务器登录的时候出现Access denied for user 'hello'@'localhost' (using password: YES)
想用自己电脑做一个服务器,然后就选择了Wamp,本来一切顺利,可是到登录的时候却出现了问题,出现了 Access denied for user 'hello'@'localhost' (using ...
- ibatis 的sqlMap 的xml 关注点
1.当有特殊字符时候需要保持原状 eg:特殊字符 <> 错误:t.name<>' ' 会报The content of elements must consist of ...
- iOS Programming UINavigationController
iOS Programming UINavigationController the Settings application has multiple related screens of info ...
- 重构31-Replace conditional with Polymorphism(多态代替条件)
多态(Polymorphism)是面向对象编程的基本概念之一.在这里,是指在进行类型检查和执行某些类型操作时,最好将算法封装在类中,并且使用多态来对代码中的调用进行抽象. public class O ...
- 唤醒键盘时取消对特定类的position:fixed定位
/* 唤起键盘时取消对特定类的position:fixed定位 */ var windheight = $(window).height(); /*未唤起键盘时当前窗口高度*/ $(window).r ...
- Farseer.net轻量级ORM开源框架 V1.x 入门篇:视图的数据操作
导航 目 录:Farseer.net轻量级ORM开源框架 目录 上一篇:Farseer.net轻量级ORM开源框架 V1.x 入门篇:视图实体类映射 下一篇:Farseer.net轻量级ORM开源 ...
- (二)Redis for 阿里云公网连接
目录 (一)Redis for Windows正确打开方式 (二)Redis for 阿里云公网连接 (三)Redis for StackExchange.Redis 阿里云目前仅支持内网连接Redi ...
- Which dispatch method would be used in Swift?
In this example: protocol MyProtocol { func testFuncA() } extension MyProtocol { func testFuncA() { ...
- ROS在rviz中实时显示轨迹(nav_msgs/Path消息的使用)
消息结构说明nav_msgs/Path.msg结构#An array of poses that represents a Path for a robot to followHeader heade ...