2550 百步穿杨 - HDOJ

2550 百步穿杨

Problem Description

时维九月,序属三秋,辽军大举进攻MCA山,战场上两军正交锋.辽军统帅是名噪一时的耶律-James,而MCA方则是派出了传统武将中草药123.双方经过协商,约定在十一月八日正午十分进行射箭对攻战.中草药123早早就开始准备,但是他是武将而不是铁匠,造弓箭的活就交给聪明能干的你了,现在告诉你每种弓箭规格,即箭身的长度,以及每种规格弓箭所需要的数目,要求你把需要的弓箭都输出.
弓箭的基本样子为 “>+—+>”,其中”+—+”为箭身,数据保证箭身长度 > 2

Input

首先输入一个t,表示有t组数据,跟着t行:
每行一个N (N < 50 ),接下去有N行,第i行两个整数Ai , Bi,分别代表需要箭身长度为Ai的弓箭Bi枝. (Ai < 30 , Bi < 10 )
输入数据保证每一个Ai都是不同的.

Output

按照箭身的长度从小到大的顺序依次输出所有需要的弓箭,”每一种”弓箭后输出一个空行.

Sample Input

1
4
3 4
4 5
5 6
6 7

Sample Output

>+-+>
>+-+>
>+-+>
>+-+>

>+--+>
>+--+>
>+--+>
>+--+>
>+--+>

>+---+>
>+---+>
>+---+>
>+---+>
>+---+>
>+---+>

>+----+>
>+----+>
>+----+>
>+----+>
>+----+>
>+----+>
>+----+>

Solution:

字符串的s.insert(int pos, string substr);因此在insert前, 先用循环构造好需要插入的子串.

坑点:

  • 需要对每个case的箭按照长度从小到大排序, 刚开始题目没有读清楚.
  • 打印根本不需要特别注意格式, 是我想多了。
#include <iostream>
#include <string>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

struct Node {
int a, b;
};

bool cmp(Node n1, Node n2) {
return n1.a < n2.a;
}

int main() {
int T, flag = 0; cin >> T;
while (T--) {
vector<Node> v;
int N; cin >> N;
for (int i = 0; i < N; i++) {
Node node;
int a, b; cin >> node.a >> node.b;
v.push_back(node);
}

sort(v.begin(), v.end(), cmp);

for (int i = 0; i < N; i++) {
string s = ">++>", add = "";
for (int j = 0; j < v[i].a-2; j++) {
add += '-';
}
s.insert(2, add);
for (int j = 0; j < v[i].b; j++) {
cout << s << endl;
}
cout << endl;
}
}
return 0;
}