uvaoj 10055 java版

来源:互联网 发布:mac 触摸板 鼠标方向 编辑:程序博客网 时间:2024/04/30 10:54

在家闲的无聊,想到自己编程能力太弱,所以在看刘汝佳的《算法竞赛入门经典》,从书上看到在uvaoj的AOAPC上有书上的配套练习,所以我就变看书边做题。今天是我第一次做uvaoj的题目,第一道id是10055。先上题目:

Problem A

Hashmat the brave warrior

Input: standard input

Output: standard output

 

Hashmat is a brave warrior who with his group of young soldiers moves from one place to another to fight against his opponents. Before fighting he just calculates one thing, the difference between his soldier number and the opponent's soldier number. From this difference he decides whether to fight or not. Hashmat's soldier number is never greater than his opponent.

Input

The input contains two integer numbers in every line. These two numbers in each line denotes the number of soldiers in Hashmat's army and his opponent's army or vice versa. The input numbers are not greater than 2^32. Input is terminated by End of File.

 

Output

 For each line of input, print the difference of number of soldiers between Hashmat's army and his opponent's army. Each output should be in seperate line.

 

Sample Input:

10 1210 14100 200

 

Sample Output:

24

100

题目本身是很简单的,要注意的地方有2点:

1.输入的最大值为2^32,要用long类型

2.Hashmat的部队人数肯定是最少的,不要被输入样例迷惑,以为一定是人数少的在前或者Hashmat的部队人数在前

ps.还有一个要注意的就是针对java来说的,因为题目中说明 输入的最后是EOF(Input is terminated by End of File),在windows的cmd中就是CTRL+C,以此可以测试程序的正确性。

先附上AC的程序

import java.util.Scanner;import java.util.NoSuchElementException;public class Main{public static void main(String[] args){excu();}public static void excu(){        Scanner in = new Scanner(System.in);        try{        while(true){                String str = in.nextLine();                if(str.length() != 0){                    String[] data = str.split(" ");                    System.out.println(Math.abs(Long.valueOf(data[1])-Long.valueOf(data[0])));                }else break;       }        }catch(NoSuchElementException e){        }            }}

第一点要声明的就是在uvaoj中提交的程序不管是直接复制上去还是上传代码上去,公共类名一定要是Main,不然就会出现Compilation error,为了测试这个我就贡献了2个CE(Compilation error)

第二点应该是针对java来说的,我是使用Scanner读取数据的,然后在输入数据结束时会出现EOF,这时候由于使用nextLine()方法就会出现NoSuchElementException异常,然后uvaoj就会告诉你Runtime error,如果你只是抛出异常那么肯定是RE(Runtime error)了,所以我采用了try{}catch语句捕捉了异常,然后没有进行其他操作,我怕有其他操作就成了unAC了,就这道题我就有5个unAC了,5555....如果有人其他方法可行请告诉我,谢谢。之后就可以通过了。

程序实在是写的有够烂的,运行时间2.646,我看到别人的都是0.0xx,0.00x的,悲催

慢慢学习好好努力!

2014-1-27

补:

偶然看到这个方案也可以,代码简单,就是时间变长了,为2.895

ps:由于我不知道系统给出的运行时间对于相同代码的误差为多少(主要是没那么无耻和无聊去刷AC率,同时我觉得误差应该不会那么大),所以下面的时间只是一个参考

import java.util.Scanner;public class Main{public static void main(String[] args){        Scanner in = new Scanner(System.in);        String str;                 while(in.hasNext()){            str = in.nextLine();            String[] data = str.split(" ");            System.out.println(Math.abs(Long.valueOf(data[1])-Long.valueOf(data[0])));        }    }}
然后我稍微优化了一下,时间为2.708

import java.util.Scanner;public class Main{public static void main(String[] args){        Scanner in = new Scanner(System.in);        String str;        int d;                 while(in.hasNext()){            str = in.nextLine();            d = str.indexOf(' ');            System.out.println(Math.abs(Long.valueOf(str.substring(0,d))-Long.valueOf(str.substring(d+1,str.length()))));        }    }}
总之,用java写的话,时间实在是太长,以后要多多注意时间的问题,如果有人有更好的方案请告诉我,谢谢!

0 0
原创粉丝点击