Problem A: 加法变乘法
Time Limit: 10 Sec Memory Limit: 256 MB
Description
已知X可以写成从1开始连续若干个整数的和, 现在要求把其中两个不相邻的加号变成乘号,使得结果为Y。
找出所有满足条件的可能答案并输出(把两个乘号左边的数字用小括号括起来,中间用英文逗号间隔,两个括号之间不空格);
若找不到满足的条件,则输出“NONE”字样。编写程序,完成n组数据的判定。
例如:当X为1225,Y为2015时
因为:1+2+3+ ... + 49 = 1225
且1+2+3+...+10*11+12+...+27*28+29+...+49 = 2015
所以:一个解为(10,27)。
Input
第一行为n值,接下来依次n行的第一个数据是加法结果X,第二个数据是变乘法后的结果Y,以空格间隔。
Output
输出n行,每一行的格式为“(***,***)(***,***)”(或者“NONE”)。请严格按照
格式书写,不能出现其它文字或符号。
Sample Input
3
1225 2015
1224 2015
1275 2065
Sample Output
(10,27)(16,24)
NONE
(10,27)(16,24)
Analysis
简单题,枚举尝试即可。
Code
#include<iostream>
#include<vector>
#include<map>
using namespace std;
int main() {
int cases;
cin >> cases;
auto solve = [](int x, int y) -> vector<pair<int, int> > {
int tmp = 0, buf = 0;
vector<pair<int, int> > ret;
for (int i = 2; i < 47; i++) {
tmp = buf = x - (i * 2 - 1) + i * (i - 1);
for (int j = i + 2; j < 50; j++) {
buf = tmp - (j * 2 - 1) + j * (j - 1);
if (buf == y) {
ret.push_back(make_pair<int, int>(i - 1, j - 1));
}
}
}
return ret;
};
while (cases--) {
int x, y;
cin >> x >> y;
auto solutions = solve(x, y);
if (solutions.size() == 0) {
cout << "NONE" << endl;
} else {
for (auto i : solutions) {
cout << '(' << i.first << ',' << i.second << ')';
}
cout << endl;
}
}
return 0;
}
Problem B: 核桃的数量
Time Limit