1: A + B Problem
题目描述:
A + B Problem输入:
多组测试数据,第一行有两个小于1e9的整数,
对这两个数求和,最后当EOF标志的时候结束。输出:
对这两个数求和的结果样例输入:
2 3
1 9样例输出:
5
10
做加法的时候使用浮点型精度会不准确是吗?两个小于1e9的整数又如何判定?
最后当EOF标志的时候结束。 EOFjava又如何判定?压力好大,简单的一道题都不会。

解决方案 »

  1.   

    acm题 干嘛用面向对象的java写呢   还是面向过程的c来的顺手
      

  2.   

    Integer  i1=new Integer((int) 1e9);
      

  3.   

    package test;public class TestAdd
    {
        public static void main(String[] args)
        {
            float one = 0.98f;
            
            float two = 1.110f;
            
            float three = 2.09f;
            
            System.out.println(one + two == three ? "true" : "false");
        }
    }
      

  4.   

    1 这道题 目考查能否正常从控制台获得输入2 1e9是简化程序,就是在程序里你不用考虑整型运算溢出的问题 3
    回答楼主加法整数运算是不会产生不准确的结果的,但减法的float才会,例如2.0-1.1可以算一下不等于0.9的
    3 检测eof是为了使程序的使用者输入eof便退出程序,而不是使用ctrl+c的方法import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    public class TestCosole {
    public static void main(String[] args){
    System.out.println("请输入两个小于1e9的整数以空格分割,并按回车键以便计算结果!\n退出请输入eof");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String s;
    int[] ab=new int[2];
     String[] ss=null;
    try{
    while(!(s=br.readLine()).equalsIgnoreCase("EOF")){
       ss=s.split("\\s+");
      try{
      if(ss.length!=2)
      System.out.println("请正确输入以一个空格分割的两个整数!\n退出请输入eof");  
      
      else {
      ab[0]=Integer.parseInt(ss[0]);
      ab[1]=Integer.parseInt(ss[1]);
      System.out.println("the result is-->"+(ab[0]+ab[1]));
      }
      }catch(NumberFormatException nfe){
      System.out.println("请正确输入以一个空格分割的两个整数!\n退出请输入eof");
      
      }

    }
    System.out.println("检测到EOF,程序正常退出!");
    }catch(IOException e){

    }


    }
    }基本实现了功能,这是jdk1.4以及的传统实现方式,不是很漂亮.
      

  5.   

    楼主理解错了
    1.多组测试数据,第一行有两个小于1e9的整数,
    2.对这两个数求和,最后当EOF标志的时候结束。
    1是表示测试数据严格遵循的条件
    你的程序里是没必要判断的
    2是表示ctrl+c结束的
    用in.nextToken() != StreamTokenizer.TT_EOF判断就可以的
      

  6.   

    楼主,像这种题目的答案代码最终应该是由专门的测试程序判断正确与否的。
    所以,输入应该是一个指定名称的txt文件,输出也是要必须写入一个由出题人指定名称的txt文件。
    也正因为如此,所以题目上说以EOF判定输入结束,eof不就是“end of file ”吗?
    以BufferedReader 为例,
    BufferedReader in=new BufferedReader(new FileReader("文件名.txt"));
            String s=null;
            while((s=in.readline())!=null){输入处理语句}
    BufferedReader遇到文件末尾会自动返回null,如上述代码,利用此结束输入循环。
      

  7.   


    import java.util.*;
    //@SuppressWarnings("unchecked")public class TestContainer {
    public static void main(String args[]) {
    Collection<Object> c = new HashSet<Object>();
    c.add("Hello");
    c.add(new Name("f1", "l1"));
    c.add(new Integer(100));
    c.remove("Hello");
    c.remove(new Integer(100));
    System.out.println(c.remove(new Name("f1", "l1")));
    System.out.println(c);

    }class Name {
    private String firstName, lastName;
    public Name(String firstName, String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
    } //public String getfirstName() {return firstName;}
    //public String getlastName() {return lastName;}
    public String toString() {return firstName + " " + lastName;}public boolean equals(Object obj) {
    if(obj instanceof Name) {
    Name name = (Name) obj;
    return (firstName.equals(name.firstName))
    && (lastName.equals(name.lastName));

    return super.equals(obj);
    }public int hashCode() {
    return firstName.hashCode(); 

    }
    上次看到有人提问来着,我想知道何时调用equals方法和hashCode方法。
    还有为什么把equals方法和hashCode方法注释起来后,
    打印出
    false                       true
    [f1 l1]的结果?而不是原来的     []
    remove方法为什么会失败?
      

  8.   


    import java.util.*;
    //@SuppressWarnings("unchecked")public class TestContainer {
    public static void main(String args[]) {
    Collection<Object> c = new HashSet<Object>();
    c.add("Hello");
    c.add(new Name("f1", "l1"));
    c.add(new Integer(100));
    c.remove("Hello");
    c.remove(new Integer(100));
    System.out.println(c.remove(new Name("f1", "l1")));
    System.out.println(c);
    }  
    }class Name {
    private String firstName, lastName;
    public Name(String firstName, String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
    }  //public String getfirstName() {return firstName;}
    //public String getlastName() {return lastName;}
    public String toString() {return firstName + " " + lastName;}public boolean equals(Object obj) {
    if(obj instanceof Name) {
    Name name = (Name) obj;
    return (firstName.equals(name.firstName))
    && (lastName.equals(name.lastName));
    }  
    return super.equals(obj);
    }public int hashCode() {
    return firstName.hashCode();  
    }  
    }
      

  9.   

    注释起来后打印出
    false
    [f1 l1]不是原来的
    true
    [ ]