UVA - 817 According to Bartjens

修复打印错误并解析数学表达式
本文介绍了一个程序设计问题,即如何通过在给定的数字串中正确插入算术运算符,使最终表达式的计算结果等于2000。文章详细解释了问题背景、输入输出格式及示例,并提供了完整的源代码实现。

Description

Download as PDF


 According to Bartjens 

The wide dissemination of calculators and computers has itsdisadvantages. Even students in technical disciplinestend to exhibit a surprising lack of calculating ability. Accustomed tothe use of calculators and computers, many ofthem are unable to make calculations like 7 * 8 mentally or like 13 *17 using pencil and paper. We all know, butwho cares?

Professor Bartjens cares. Professor Bartjens is a bit old fashioned.Hedecided to give his students some training incalculating without electronic equipment by creating a collection ofcalculation problems, (like 2100 - 100 = ...). Tosimplify grading the problems, he constructed them so that almost allof them had 2000 as an answer. Not all ofthem, of course. His students would be smart enough to recognize thepattern, and fill in 2000 everywhere withoutfurther thinking.

Unfortunately Professor Bartjens’ printer driver turned out to be evenmore old-fashioned than the professor himself,and it could not interface with his new printer. Inspecting the printedproblems, he soon recognized the pattern: noneof the operations was transmitted to the printer. A problem like:
2100-100=
was printed as:
2100100=
Fortunately, all the digits and the equal sign were still printed.

To make this bad situation much worse, Professor Bartjens’ source filehad disappeared. So Professor Bartjens hasanother problem: what were his original problems? Given the fact thatthe answer (most likely) should be 2000, theline 2100100= could have been any one of the lines:

2100-100=
2*100*10+0=
2*100*10-0=
2*10*0100=
2*-100*-10+0=

Professor Bartjens does remember a few things about how he wrote theproblems:

  • He is sure that whenever he wrote down a number (other than 0),itwould not start with a zero. So2*10*0100= could not have been one of his problems.
  • He also knows he never wrote the number zero as anything but 0.So hewould not have a problem like2*1000+000=.
  • He used only binary operators, not the unary minus or plus, so2*-100*-10+0= was not an option either.
  • He used the operators +, - and * only, avoiding the operator /(afterall, they were first year students).
  • He knew all problems followed the usual precedence andassociativityrules.
You are to help Professor Bartjens recover his problem set bywriting aprogram that when given a row of digits,insert one or more of the operators +, - and * in such a way that thevalue of the resulting expression equals 2000.

Input 

The input consists of one or more test cases. Each test case is asingle line containing n digits ('0'–'9'), 1 ≤n ≤9,followed by an equal sign. There will not be any blanks embedded in theinput, but there may be some after theequal sign.

The last test case is followed by a line containing only the equalsign. This line should not be processed.

Output 

For each test case, print the word Problem, then the number of thecase, then all possible ways of insertingoperators in the row of digits such that the resulting expression hasthe value 2000, subject to Professor Bartjens’memory of how he wrote the problems. Use the format shown below. Ifthere is more than one possible problem,write the problems in lexicographic order. Each possible problemshould be on a new line, indented 2 spaces. If there is no solution theanswer IMPOSSIBLE should be printed,indented 2 spaces.


Sample InputOutput for the Sample Input
2100100=
77=
=
Problem 1
  2*100*10+0=
  2*100*10-0=
  2100-100=
Problem 2
  IMPOSSIBLE

题意:求在一个字符串中插入运算符使得结果是2000的全部的可能

思路:枚举每一个位置得到如干个数字在回溯运算符得到结果,字典序输出

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <set>
using namespace std;
const int MAXN = 200;
const int INF = 0x3f3f3f3f;
typedef long long ll;

string str;
ll res[MAXN];
int N, M, op[MAXN];
set<string> f;

ll tran(int st, int ed) {
	if (str[st] == '0' && ed-st > 0)
		return -1;
	ll x = 0;
	for (int i = st; i <= ed; i++) 
		x = x*10 + str[i]-'0';
	return x;
}

void cal() {
	int m = N-1;
	int len = str.length()+N-2;
	string p(len+1, '*');
	p[len--] = '=';
	for (int i = N-1; i >= 0; i--) {
		ll x = res[i];
		if (x == 0)
			p[len--] = '0';
		while (x) {
			p[len--] = x%10+'0';
			x /= 10;
		}
		if (i) {
			if (op[m-1] == 0) {
				p[len--] = '+';
				m--;
			}
			else if (op[m-1] == 1) {
				p[len--] = '-';
				m--;
			}
			else {
				p[len--] = '*';
				m--;
			}
		}
	}
	f.insert(p);
}

void findExpression(int cur, stack<ll> s) {
	if (cur == N-1) {
		ll ans = 0;
		while (!s.empty()) {
			ans += s.top();
			s.pop();
		}
		if (N > 1 && ans == 2000)
			cal();
		return;
	}	
	for (int i = 0; i < 3; i++) {
		op[cur] = i;
		if (i == 0) {
			s.push(res[cur+1]);
			findExpression(cur+1, s);
			s.pop();
		}
		else if (i == 1) {
			s.push(-res[cur+1]);
			findExpression(cur+1, s);
			s.pop();
		}
		else {
			ll x = res[cur+1]*s.top();
			s.pop();
			s.push(x);
			findExpression(cur+1, s);
		}
	}
}

void solve(int cur, int n) {
	if (cur == str.length()-1) {
		N = n;
		stack<ll> s;
		s.push(res[0]);
		findExpression(0, s);
		return;
	}
	for (int i = cur; i < str.length()-1; i++) {
		ll x = tran(cur, i);
		if (x != -1) {
			res[n] = x;
			solve(i+1, n+1);
		}
	}
}

int main() {
	int cas = 1;
	while (cin>>str) {
		f.clear();
		if (str.length() == 1 && str[0] == '=') 
			break;
		printf("Problem %d\n", cas++);
		solve(0, 0);
		if (f.size() == 0) 
			printf("  IMPOSSIBLE\n");
		else {
			for (set<string>::iterator it = f.begin(); it != f.end(); it++)
				cout << "  " << *it << endl;
		}
	}	
	return 0;
}



版权声明:本文博主原创文章,博客,未经同意不得转载。

转载于:https://www.cnblogs.com/mengfanrong/p/4829970.html

在车辆工程中,悬架系统的性能评估和优化一直是研究的热点。悬架不仅关乎车辆的乘坐舒适性,还直接影响到车辆的操控性和稳定性。为了深入理解悬架的动态行为,研究人员经常使用“二自由度悬架模型”来简化分析,并运用“传递函数”这一数学工具来描述悬架系统的动态特性。 二自由度悬架模型将复杂的车辆系统简化为两个独立的部分:车轮和车身。这种简化模型能够较准确地模拟出车辆在垂直方向上的运动行为,同时忽略了侧向和纵向的动态影响,这使得工程师能够更加专注于分析与优化与垂直动态相关的性能指标。 传递函数作为控制系统理论中的一种工具,能够描述系统输入和输出之间的关系。在悬架系统中,传递函数特别重要,因为它能够反映出路面不平度如何被悬架系统转化为车内乘员感受到的振动。通过传递函数,我们可以得到一个频率域上的表达式,从中分析出悬架系统的关键动态特性,如系统的振幅衰减特性和共振频率等。 在实际应用中,工程师通过使用MATLAB这类数学软件,建立双质量悬架的数学模型。模型中的参数包括车轮质量、车身质量、弹簧刚度以及阻尼系数等。通过编程求解,工程师可以得到悬架系统的传递函数,并据此绘制出传递函数曲线。这为评估悬架性能提供了一个直观的工具,使工程师能够了解悬架在不同频率激励下的响应情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值