首页 > 其他分享 >UVa 10344 23 out of 5 (全排列枚举&回溯)

UVa 10344 23 out of 5 (全排列枚举&回溯)

时间:2023-04-12 11:37:23浏览次数:42  
标签:cur 23 d% dfs flag ans UVa 10344


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)

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



水题。


完整代码:


/*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;
}



标签:cur,23,d%,dfs,flag,ans,UVa,10344
From: https://blog.51cto.com/u_5535544/6185332

相关文章

  • UVa 11205 The broken pedometer (枚举好题&巧用二进制)
    11205-ThebrokenpedometerTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=107&page=show_problem&problem=2146TheProblemAmarathonrunnerusesapedometerwithwhichheishavingpro......
  • UVa 10167 Birthday Cake (枚举)
    10167-BirthdayCakeTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=107&page=show_problem&problem=1108BackgroundLucyandLilyaretwins.Todayistheirbirthday.Motherbuysabirthd......
  • UVa 129 Krypton Factor (回溯好题)
    129-KryptonFactorTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=65YouhavebeenemployedbytheorganisersofaSuperKryptonFactorContestinwhichcontestantshaveveryhighmental......
  • UVa 11210 Chinese Mahjong (模拟&枚举&回溯)
    11210-ChineseMahjongTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=2151Mahjong()isagameofChineseoriginusuallyplayedbyfourpersonswithtilesresemblingdominoesandbearing......
  • UVa 11375 Matches (DP&高精度)
    11375-MatchesTimelimit:2.000secondshttp://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=2370Wecanmakedigitswithmatchesasshownbelow:Given N matches,findthenumberofdifferentnumbersrepresentableusing......
  • UVa 11507 Bender B. Rodríguez Problem (模拟&异或)
    11507-BenderB.RodríguezProblemTimelimit:4.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2502Benderisarobotbuiltby Mom'sFriendlyRobotCompany atits......
  • UVa 443 / POJ 2247 Humble Numbers (4因子-丑数&STL灵活运用)
    443-HumbleNumbersTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=384http://poj.org/problem?id=2247Anumberwhoseonlyprimefactorsare2,3,5or7isc......
  • CVPR 2023|两行代码高效缓解视觉Transformer过拟合,美图&国科大联合提出正则化方法DropK
    前言 美图影像研究院(MTLab)与中国科学院大学突破性地提出正则化方法DropKey,用于缓解VisionTransformer中的过拟合问题。该方法通过在注意力计算阶段随机drop部分Key以鼓励网络捕获目标对象的全局信息,从而避免了由过于聚焦局部信息所引发的模型偏置问题,继而提升了基于Tra......
  • 23.text--生成验证码
    生成验证码内容:可以是小写字母,也可以是大写字母,还可以是数字规则:长度为5,内容是四位字母,一位数字,其中数字只有1位,但是可以出现在任意位置publicstaticvoidmain(String[]args){//创建含有大小写字母的数组char[]letterArr=newchar[52];//小写字母在ASC......
  • UVa 10673 Play with Floor and Ceil (数论)
    10673-PlaywithFloorandCeilTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1614TheoremForanytwointegers x and k thereexiststwomoreintegers p ......