[ACM] 第八届西邮杯初赛题解

Problem A: 加法变乘法

Time Limit: 10 Sec              Memory Limit: 256 MB

Description

已知X可以写成从1开始连续若干个整数的和, 现在要求把其中两个不相邻的加号变成乘号,使得结果为Y。
找出所有满足条件的可能答案并输出(把两个乘号左边的数字用小括号括起来,中间用英文逗号间隔,两个括号之间不空格);
若找不到满足的条件,则输出“NONE”字样。编写程序,完成n组数据的判定。

例如:当X为1225,Y为2015时
因为:1+2+3+ ... + 49 = 12251+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
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值