POJ1100---Dreisam Equations

問題描述:

Dreisam Equations
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 2060 Accepted: 418
Description


During excavations in the Dreisamwuste, a desert on some far away and probably uncivilized planet, sheets of paper containing mysterious symbols had been found. After a long investigation, the project scientists have concluded that the symbols might be parts of equations. If this were true, it would be proof that the Dreisamwuste was civilized a long long time ago. 
The problem, however, is that the only symbols found on the sheets are digits, parantheses and equality signs. There is strong evidence that the people living in the Dreisamwuste knew only of three arithmetic operations: addition, subtraction, and multiplication. It is also known that the people of the Dreisamwuste did not have prioritization rules for arithmetic operations - they evaluate all terms strictly left to right. For example, for them the term 3 + 3 * 5 would be equal to 30, and not 18. 


But currently, the sheets do not contain any arithmetic operators. So if the hypothesis is true, and the numbers on the sheets form equations, then the operators must have faded away over time. 


You are the computer expert who is supposed to find out whether the hypothesis is sensible or not. For some given equations (without arithmetic operators) you must find out if there is a possibility to place +, -, and * in the expression, so that it yields a valid equation. For example, on one sheet, the string "18=7 (5 3) 2" has been discovered. Here, one possible solution is "18=7+(5-3)*2". But if there was a sheet containing "5=3 3", then this would mean that the Dreisamwuste people did not mean an equation when writing this.
Input


Each equation to deal with occupies one line in the input. Each line begins with a positive integer (less than 230) followed by an equality sign =. (For your convenience, the Dreisamwuste inhabitants used equations with trivial left sides only.) This is followed by up to 12 positive integers forming the right side of the equation. (The product of these numbers will be less than 230.) There might be some parentheses around groups of one or more numbers. There will be no line containing more than 80 characters. There is no other limit for the amount of the parentheses in the equation. There will always be at least one space or parenthesis between two numbers, otherwise the occurrence of white space is unrestricted. 


The line containing only the number 0 terminates the input, it should not be processed.
Output


For each equation, output the line "Equation #n:", where n is the number of the equation. Then, output one line containing a solution to the problem, i. e. the equation with the missing +, -, and * signs inserted. Do not print any white space in the equation. 


If there is no way to insert operators to make the equation valid, then output the line "Impossible". 


Output one blank line after each test case.
Sample Input


18 = 7 (5 3) 2
30 = 3 3 5
18 = 3 3 5
5 = 3 3
0
Sample Output


Equation #1:
18=7+(5-3)*2


Equation #2:
30=3+3*5


Equation #3:
Impossible


Equation #4:
Impossible
Hint


In case of mutiple answers, output the least one in order (+ < - < *).


解法:

//這裏假設等號後面的第一個數一定是正的,若可以是負的,只要對solution(int,int)的參數做一下修改,並在print()中對第一個數的輸出進行特殊處理就可以了。

//沒有太多測試參數,就寫成這樣吧

#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
using namespace std;

ifstream fin("C:\\data35.in");

int N,cnt;
string equation;
long long numarr[80];
char operation[80];//儲存標點符號,operation[i]中儲存的是numarr[i]前面的標點符號,見格式’A=B+C-D*E‘,除了第一個數字,所有的數字前面都有標點符號

//動態規劃,格式來源見下面的鏈接(算法競賽入門中的提示)
int solution(int pos,int ans)
{
	if(pos==N)
	{
		if(ans==numarr[0])
			return 1;
		else
		{
			return 0;
		}
	}
	if(solution(pos+1,ans+numarr[pos]))
	{
		operation[pos]='+';
		return 1;
	}
	else if(solution(pos+1,ans-numarr[pos]))
	{
		operation[pos]='-';
		return 1;
	}
	else if(solution(pos+1,ans*numarr[pos]))
	{
		operation[pos]='*';
		return 1;
	}
	else
		return 0;
}
//打印,遇到數字打印數字,遇到括號打印括號,遇到空格打印標點符號(因爲前兩個空格中間加了個’=‘號,所以前兩個空格作廢,對前兩個空格做了特殊處理)
void print()
{
	int pos=0;
	for(int i=0;i<equation.length();++i)
	{
		if(equation[i]==' ')
		{
			if(pos!=1)
			cout<<operation[pos];
		}
		else if(equation[i]=='('||equation[i]==')')
		{
			cout<<equation[i];
		}
		else if(equation[i]=='=')
		{
			cout<<'=';
		}
		else
		{
			for(;equation[i]>='0'&&equation[i]<='9'&&i<equation.length();++i);
			{
				cout<<numarr[pos++];
				--i;
			}
		}
	}
	cout<<endl;
}		

int main()
{
	operation[1]='=';
	cnt=0;
	//本次循環只把輸入方程中的數字提取出來。(有點煩了,但我只能想到這個辦法了)。核心思想就是把char格式的數字轉化中long long格式的數字。其餘的忽略,與本次循環的目的無關
	while(getline(fin,equation))
	{
		bool isTerminate=false;
		N=0;
		++cnt;
		for(int i=0;i<equation.length();++i)
		{
			if(!(equation[i]>='0'&&equation[i]<='9'))
			continue;
			long long ans=0;
			for(;(equation[i]>='0'&&equation[i]<='9')&&i<equation.length();++i)
			{
				ans=ans*10+(int)(equation[i]-'0');
			}
			//發現一個0就表示該退出本次測試了
			if(ans==0)
			{
				isTerminate=true;
				break;
			}
			numarr[N++]=ans;
		}
		if(isTerminate)
			break;
		cout<<"Equation #"<<cnt<<":"<<endl;
		if(solution(2,numarr[1]))
			print();
		else
			cout<<"Imposible"<<endl;
	}
	system("pause");
	return 0;
}



附上一個鏈接:

http://blog.csdn.net/u014583062/article/details/41011533

見第三段代碼,我說寫的很漂亮的那一段,格式是相同的

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章