百步穿杨

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

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

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

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

>+----+>
>+----+>
>+----+>
>+----+>
>+----+>
>+----+>
>+----+>
JAVA代码,已AC。开始想的简单了,后来发现需要排序,稍微麻烦了一些。提交的时候遇到个小插曲,--t不小心放错位置了,程序不能正常结束,一直WA,搞的我很郁闷。
import java.util.Scanner;

/**
 * @author Blank ShootWithGreatPrecision.java 2011-4-21
 */
public class ShootWithGreatPrecision {

	/**
	 * @param args
	 */

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		Boolean exchange;
		while (t > 0) {

			int n = sc.nextInt();
			int a[][] = new int[2][52];
			for (int j = 0; j < n; j++) {

				a[0][j] = sc.nextInt();
				a[1][j] = sc.nextInt();
			}
			for (int i = 0; i < n-1; i++) {
				exchange=false;
				for (int j = 0; j < n-i-1; j++) {
					if (a[0][j] > a[0][j + 1]) {
						int temp = a[0][j];
						a[0][j] = a[0][j + 1];
						a[0][j + 1] = temp;
						temp = a[1][j];
						a[1][j] = a[1][j + 1];
						a[1][j + 1] = temp;
						exchange=true;
					}
					
				}
				 if(!exchange) //本趟排序未发生交换,提前终止算法
			            break;
			}
			for (int i = 0; i < n; i++) {
				int s=a[0][i],b=a[1][i];
				for (int j = 0; j < b; j++) {
					shoot(s - 2);
				}
				System.out.println();
			}
			--t;
		}
		
	}

	static void shoot(int a) {
		String str1 = ">+";
		String str2 = "+>";
		String str = "";
		for (int i = 0; i < a; i++) {
			str += "-";
		}
		str=str1+str+str2;
		System.out.println(str);

	}

}

posted @ 2011-04-21 21:31  丶Blank  阅读(405)  评论(0编辑  收藏  举报