UVA - 10344

Problem I

23 Out of 5

Input: standard input

Output: standard output

Time Limit: 1 second

Memory Limit: 32 MB

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)

Input

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.

Output

For each 5-Tupel print "Possible" (without quotes) if their exists an arithmetic expression (as described above) that yields 23. Otherwise print "Impossible".

Sample Input

1 1 1 1 1
1 2 3 4 5
2 3 5 7 11
0 0 0 0 0

Sample Output

Impossible
Possible
Possible

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6
 7 int cal(int a,char c,int b) {
 8     if (c == ‘+‘) return a + b;
 9     if (c == ‘-‘) return a - b;
10     if (c == ‘*‘) return a * b;
11 }
12
13 int a[5];
14 char s[4] = {"+-*"};
15 int dfs(int sum ,int cur) {
16     if (cur == 5) {
17         if (sum == 23) return 1;
18         else return 0;
19     }
20     for (int i = 0;i < 3;i++) {
21         if (dfs(cal(sum,s[i],a[cur]),cur + 1)) return 1;
22     }
23     return 0;
24 }
25
26 int main () {
27     // freopen("1.in","r",stdin);
28     while (cin >> a[0]) {
29         for (int i = 1;i < 5;i++) {
30             cin >> a[i];
31         }
32         if (a[0] == 0) break;
33         int ok = 0;
34         sort(a,a + 5);
35         do {
36             if(dfs(a[0],1)) {
37                 cout << "Possible" << endl;
38                 ok = 1;
39                 break;
40             }
41         }while (next_permutation(a,a + 5));
42         if (ok == 0) cout << "Impossible" << endl;
43     }
44 }

				
时间: 2024-10-08 13:40:03

UVA - 10344的相关文章

uva 10344 23 out of 5 (DFS)

uva 10344 23 out of 5 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 expression

UVa 10344 算23点

题意:有5个数,3种运算符:加.减.乘,用全部5个数,和4个运算符,构成一个表达式,使得值为23,这里没有运算符的优先级,全部是从左往右算.是可以这么理解,题目给的是从左到右依次打了括号. 思路:这里是对表达式的位置进行dfs.可以看到第0位是数字,第1位是运算符,依次则,偶数位是数字,奇数位是运算符.dfs中把当前位置cur分为奇偶分别处理.偶数位置时,则对数字进行枚举,并计算当前表达式的值,注意dfs之后的恢复:奇数位置时,枚举三种运算符.对比可以发现,枚举数字时,由于数字只能出现一次,所以

UVa 10344 - 23 out of 5

题目:给你五个数字,在他们中间加上'+','-','*',构成结果23,问能否成功. 分析:搜索,24点类似物. 首先,求出五个数的全排列: 然后,按顺序枚举三种运算,计算结果,判断即可. 说明:注意优先级,左边的高. #include <iostream> #include <cstdlib> #include <cstdio> using namespace std; int data[5],save[5],used[5],buf[5]; int P[120][5]

uva 10344 23 out of 5(递归+全排列)

这道题好像不是回溯就是简单的递归,全排列一下就ok啦,晚上估计不能好好刷题了 代码: #include<stdio.h> #include<string.h> #include<stdlib.h> #include<algorithm> using namespace std; int cmp(const void *c,const void *d) { return *(int *)c - *(int *)d ; } int flag; int a[5];

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

UVA 10341 Solve It

Problem F Solve It Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of multiple test cases and te

UVA 11014 - Make a Crystal(容斥原理)

UVA 11014 - Make a Crystal 题目链接 题意:给定一个NxNxN的正方体,求出最多能选几个整数点.使得随意两点PQ不会使PQO共线. 思路:利用容斥原理,设f(k)为点(x, y, z)三点都为k的倍数的点的个数(要扣掉一个原点O).那么全部点就是f(1),之后要去除掉共线的,就是扣掉f(2), f(3), f(5)..f(n).n为素数.由于这些素数中包括了合数的情况,而且这些点必定与f(1)除去这些点以外的点共线,所以扣掉.可是扣掉后会扣掉一些反复的.比方f(6)在f

[UVa] Palindromes(401)

UVA - 401 Palindromes Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDED

uva 401.Palindromes

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=342 题目意思:给出一段字符串(大写字母+数字组成).判断是否为回文串 or 镜像串 or 回文镜像串 or 什么都不是.每个字母的镜像表格如下 Character Reverse Character Reverse Character Reverse A A M M Y Y B