ACM详解(10)——辽宁省赛(上)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/JavaEETeacher/article/details/5764757

 第一题:Dinner

Timelimit: 1s Memorylimit:32M
Description
Little A is one member of ACM team. He had just won the gold in World Final. To celebrate, he decided to invite all to have one meal. As bowl, knife and other tableware is not enough in the kitchen, Little A goes to take backup tableware in warehouse.        There are many boxes in warehouse, one box contains only one thing, and each box is marked by the name of things inside it. For example, if "basketball" is written on the box, which means the box contains only basketball. With these marks, Little A wants to find out the tableware easily. So, the problem for you is to help him, find out all the tableware from all boxes in the warehouse.
Input
There are many test cases. Each case contains one line, and one integer N at the first, N indicates that there are N boxes in the warehouse. Then N strings follow, each string is one name written on the box.
Output
For each test of the input, output all the name of tableware.对于每组测试数据,输出餐具的名字。
Sample Input
3 basketball fork chopsticks
2 bowl letter
Sample Output
fork chopsticks
bowl
HINT
The tableware only contains: bowl, knife, fork and chopsticks.
解题思路
从输入中得到物品集合,然后判断是不是餐具即可。参考代码如下:
       /*
        * Dinner
        */
       public static void test7(Scanner scanner){
              String[] s=new String[]{"bowl","knife","fork","chopsticks"};
              int n=scanner.nextInt();
              int index=0;
              for(int i=0;i<n;i++){
                     String temp = scanner.next();
                     for(String temp2:s){
                            if(temp.equals(temp2)){
                                   if(index!=0)
                                          System.out.print(" ");
                                   System.out.print(temp);
                                   index++;
                                   break;
                            }
                     }
              }
              System.out.println();
       }
 
第二题:You are my brother
Timelimit: 1s Memorylimit:32M
Description
最近Little A认识了一个新的朋友Little B在他们聊天的时候他们竟然发现500年前他们是一家人现在Little A想知道Little是他的长辈晚辈还是兄弟。
Input
There are several test cases.
每组测试数据首先输入一个整数N(N<=1000),接着N行,每行两个整数a,b表示a的父亲是b(1<=a,b<=2000)Little的编号为1Little的编号为2。每个人只能有一个父亲。
Proceed to the end of file.
Output
对于每组测试数据如果Little BLittle A的晚辈则输出”You are my younger”,如果Little BLittle A的长辈则输出”You are my elder”,如果是同辈则输出”You are my brother”
Sample Input
5
1 3
2 4
3 5
4 6
5 6
6
1 3
2 4
3 5
4 6
5 7
6 7
Sample Output
You are my elder
You are my brother
解题思路
500年前是一家,意味着他们有共同的祖先,所以只要找到他们共同的祖先,然后算出来他们分别属于第几代子孙,就可以知道他们之间的关系了。因为每个人只能有一个父亲,所以可以使用数组下标与元素值来表示孩子与父亲的关系,例如a[4]=8,表示第5个人的父亲是第8个人。
参考代码如下:
       /*
        * you are my brother
        */
       public static void test6(Scanner scanner){
              int n=scanner.nextInt();
              int[] data = new int[n+1];
              for(int i=0;i<n;i++){
                     int a=scanner.nextInt();
                     int b=scanner.nextInt();
                     data[a-1]=b;
              }
              int d1=0;
              int d2=0;
              int temp=1;
              while(data[temp-1]!=0){
                     d1++;
                     temp = data[temp-1];
              }
           temp=2;
              while(data[temp-1]!=0){
                     d2++;
                     temp = data[temp-1];
              }
              if(d1>d2){
                     System.out.println("You are my elder");
              }else if(d1==d2){
                     System.out.println("You are my brother");
              }else{
                     System.out.println("You are my younger");
              }
       }
第三题:Time
Timelimit: 1s Memorylimit:32M
Description
Digital clock use 4 digits to express time, each digit is described by 3*3 characters (including”|”,”_”and” “).now given the current time, please tell us how can it be expressed by the digital clock.
Input
There are several test cases.
Each case contains 4 integers in a line, separated by space.
Proceed to the end of file.
Output
For each test case, output the time expressed by the digital clock such as Sample Output.
Sample Input
1 2 5 6
2 3 4 2
Sample Output
   _ _ _
 | _||_ |_
 ||_ _||_|
_ _     _
 _| _||_| _|
|_ _| ||_
HINT
The digits showed by the digital clock are as follows:
   _ _     _ _ _ _ _ _
 | _| _||_||_ |_   ||_||_|| |
 ||_ _| | _||_| ||_| _||_|
解题思路
从输出结果上看,输出了3行,分别输出上中下3部分,程序中分别输出3部分即可。参考代码如下:
       /*
        * Time
        */
       public static void test8(Scanner scanner){
              String[][] time=new String[][]{
                            {" _ "," "," _ "," _ ","   "," _ "," _ "," _ "," _ "," _ "},
                            {"| |"," |"," _|"," _|","|_|","|_ ","|_ "," |","|_|","|_|"},
                            {"|_|"," |","|_ "," _|"," |"," _|","|_|"," |","|_|"," _|"}};
              int a[]=new int[4];
              for(int i=0;i<4;i++){
                     a[i] = scanner.nextInt();
              }
              for(int i=0;i<3;i++){
                     for(int j=0;j<4;j++){
                            System.out.print(time[i][a[j]]);
                     }
                     System.out.println();
              }           
       }

 

李绪成 CSDN Blog:http://blog.csdn.net/javaeeteacher

 

展开阅读全文

一道acm题!

05-21

哪位高手有好建议?rn第二题大众批萨?(40 分)rn可执行文件: pizza.exern输入文件: pizza.inrn输出文件: pizza.outrn你准备为你和朋友们订一个批萨,他们告诉你每个人希望批萨里有和没有什么材料。当rn然,他们也明白到只有一个批萨,所以没有人期望他所有的要求都得到满足。你能订一个批rn萨满足他们每个人至少一项要求吗?rn你订批萨的那间店子提供如下的批萨口味,你可以在一个批萨中要或不要当中任何一rn口味rnA 凤尾鱼( Anchovies )rnB 黑橄榄( Black Olives )rnC 加拿大熏肉( Canadian Bacon )rnD 方丁大蒜( Diced Garlic )rnE 浓奶酪( Extra Cheese )rnF 鲜花椰菜( Fresh Broccoli )rnG 青胡椒( Green Peppers )rnH 火腿( Ham )rnI 意大利香肠( Italian Sausage )rnJ 加拉佩诺胡椒( Jalapeno Peppers )rnK 波兰熏肠( Kielbasa )rnL 瘦牛肉( Lean Ground Beef )rnM 蘑菇( Mushrooms )rnN 脱脂羊乳酪( Nonfat Feta Cheese )rnO 洋葱( Onions )rnP 胡椒( Pepperoni )2001 年中山大学第四届大学生程序设计竞赛暨第26 届ACM/ICPC 中山大学组队选拔赛rn你的朋友每人给你一行文字描述他们的批萨口味。例如,rn+O-H+P;rn意味着某位朋友将接受一个不含洋葱,或不含火腿,或带有胡椒的批萨。而rn-E-I-D+A+J;rn表示某位朋友将接受一个不含浓奶酪或意大利香肠或方丁大蒜的,或带有凤尾鱼或加拉佩诺rn胡椒的批萨。rn输入rn输入文件pizza.in 有若干行,描述了对一个批萨的约束。一个批萨约束由1 到12rn个口味约束组成,一个口味约束占一行,表示一位朋友的口味。最后一行仅含一个句号“.”,rn表示输入的结束。rn一个口味约束是一组由分号“;”结束的口味要求序列。rn一个口味要求由一个符号( +/-)接着一个从A 到P 的大写字母组成。rn输出rn请将任一种符合要求的批萨的描述输出到文件pizza.out 。一个批萨描述由一个长度rn为10 的字符串“Toppings: ”开始(注意有一个空格),接着是一组按字母序排列的字rn母,列出批萨的口味。例如,一个带有洋葱、凤尾鱼、鲜花椰菜和加拿大熏肉的批萨描述为:rnToppings: ACFOrn如果没有口味的组合可以满足每人至少一项请求,你的程序应输出字符串rn“No pizza can satisfy these requests.”rn输入样例 输出样例rn+A+B+C+D-E-F-G-H;rn-A-B+C+D-E-F+G+H;rn-A+B-C+D-E+F-G+H;rn.rnToppings:rn+A+B+C+D;rn+E+F+F+H;rn+A+B-G;rn+O+J-F;rn+H+I+C;rn+P;rn+O+M+L;rn+M-L+P;rn.rnToppings: CELPrn+A+B+C+D;rn+E+F+F+H;rn+A+B-G;rn+P-O;rn+O+J-F;rn+H+I+C;rn+P; +O;rnNo pizza can satisfy these requests.rn 论坛

没有更多推荐了,返回首页