0
点赞
收藏
分享

微信扫一扫

HDOJ--2550 百步穿杨

认真的老去 2022-08-16 阅读 71


百步穿杨
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10441 Accepted Submission(s): 3919


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
>+-+>
>+-+>
>+-+>
>+-+>

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

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

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

方法一:

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
fun p[] = new fun[n];
for(int i=0;i<n;i++){
p[i] = new fun();
p[i].a = sc.nextInt();
p[i].b = sc.nextInt();
}
Arrays.sort( p , new Comparator <fun> (){
public int compare( fun x, fun y ){
return x.getA()-y.getB(); //a的大小,从小到大排序
}
});
for(int i=0;i<n;i++){ //n组
for(int j=0;j<p[i].getB();j++){ // 每个弓箭有b支
System.out.print(">+");
for(int k=0;k<p[i].getA()-2;k++){
System.out.print("-");
}
System.out.println("+>");
}
System.out.println();
}
}
}
}
class fun{
protected int a;
protected int b;
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public int getB() {
return b;
}
public void setB(int b) {
this.b = b;
}
}

方法二:

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
char chs[] = new char[30];
while(t-->0){
int n=sc.nextInt();
Me po[]=new Me[n];
for(int i=0;i<n;i++){
po[i]=new Me();
po[i].a=sc.nextInt();
po[i].b=sc.nextInt();
}
for(int i=0;i<po.length-1;i++){
for(int j=i+1;j<po.length;j++){
if(po[i].a>po[j].a){
Me temp=po[j];
po[j]=po[i];
po[i]=temp;
}
}
}
for (int i = 0; i < po.length; i++) {
int a=po[i].a - 2;
for (int j = 0; j < a; j++) {
chs[j] = '-';
}
StringBuilder stb = new StringBuilder(">++>");
stb.insert(2, chs, 0, a);
for (int j = 0; j < po[i].b; j++) {
System.out.println(stb);
}
System.out.println();
}
}
}
}
class Me{
int a=0;
int b=0;
}


举报

相关推荐

0 条评论