10344 - 23 out of 5
Time limit: 3.000 seconds
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1285
Your task is to write a program that can decide whether you can find an arithmetic expression consisting of five given numbers(1<=i<=5)
that will yield the value 23.
For this problem we will only consider arithmetic expressions of the following from:
where : {1,2,3,4,5} -> {1,2,3,4,5} is a bijective function
and {+,-,*} (1<=i<=4)
The Input consists of 5-Tupels of positive Integers, each between 1 and 50.
Input is terminated by a line containing five zero's. This line should not be processed.
For each 5-Tupel print "Possible" (without quotes) if their exists an arithmetic expression (as described above) that yields 23. Otherwise print "Impossible".
1 1 1 1 1
1 2 3 4 5
2 3 5 7 11
0 0 0 0 0
Impossible
Possible
Possible
水题。
完整代码:
/*0.279s*/
#include<cstdio>
#include<algorithm>
using namespace std;
int a[5];
bool flag;
void dfs(int cur, int ans)
{
if (cur == 5)
{
if (ans == 23) flag = true;
return;
}
if (!flag) dfs(cur + 1, ans + a[cur]);
if (!flag) dfs(cur + 1, ans - a[cur]);
if (!flag) dfs(cur + 1, ans * a[cur]);
}
int main()
{
while (scanf("%d%d%d%d%d", &a[0], &a[1], &a[2], &a[3], &a[4]), a[0])
{
flag = false;
sort(a, a + 5);
do
{
dfs(1, a[0]);
if (flag) break;
}
while (next_permutation(a, a + 5));
puts(flag ? "Possible" : "Impossible");
}
return 0;
}
分享到:
相关推荐
uva272
UVA109的题解,经测试完全正确,还附有题解。
有uva刘汝佳文件夹的50道题解,从数据结构开始,以后慢慢上传
包含UVA在线OJ系统的绝大部分的示例代码,并都已AC,可在刷题时参考
UVa在我看来是比较全的一个题解,希望能帮助大家。欢迎下载。
uva最全ac代码
uva531最长公共子序列问题水题,应用简单的dp即可ac有更快速的方法欢迎讨论
1.Uva_base的编译 在编译球队时,则需要在当前球队文件夹下打开终端输入执行以下命令(以下命令都是在root下执行的): ./configure make clean make 如果运行Uva_base后,出现球员越界或掉线的情况,就重新...
UVA 题目,不是很难,试试吧
uva10755 ac 代码,可以随意更改下载
uva357的栈实现版本
《算法竞赛入门经典》UVa配套题目pdf版完整
世界著名大学UVA OJ平台上的题目部分分类,分的不好请原谅。
这是一支完整的uva球队,包含所有基本模块,初者可在上修改得到自己的球队
uva_trilearn2002 源代码
主要是uvaoj习题相关题目 练习题目
开源项目-codingsince1985-UVa.zip,Been solving UVa Online Judge Problems in Golang for one year (and counting)
这里面全部为在Uva Online Judge上面的部分题目的解答,里面提供了解答使用的源代码。
UVA 499 Solution in C/ C++
PDF试题