题目描述:编程在算式123_45_67_8_9=N的下划线部分填上加号(+)或减号(-),使该等式成立。要求程序运行时输出该等式。(保证数据存在满足的等式)
输入要求:输入一个整数N。
输出要求:输出满足条件的等式。若不存在满足的等式,则输出"impossible"(输出不包括引号)
输入样例:100
输出样例:123+45-67+8-9=100
// 题型:语法题
// 直接四重循环暴力求解即可
#include <iostream>
using namespace std;
char get(int x) {
if(!x) return '+';
else return '-';
}
int main()
{
int x;
cin >> x;
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 2; j++)
for(int m = 0; m < 2; m++)
for(int n = 0; n < 2; n++)
{
int t = x - 123;
if(i == 0) t -= 45;
else t += 45;
if(j == 0) t -= 67;
else t += 67;
if(m == 0) t -= 8;
else t += 8;
if(n == 0) t -= 9;
else t += 9;
if(!t) {
cout << "123" << get(i) << "45" << get(j) << "67" << get(m) << "8" << get(n) << "9=" << x << endl;
return 0;
}
}
}
puts("impossible");
return 0;
}