Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

疯了完全没理由啊

Posted by ilmlj at 2014-02-05 16:37:30 on Problem 1003
对于第1003题为什么这样是答案是错误的呢??
我找了运行可以通过的答案,几乎没差别啊??
求教大神啊

下面是我的答案:
public class Main{
	public static void main(String[] args){
		String str;
		float c,sum = 0;
		int i;
		Scanner scanner = new Scanner(System.in);
		str = scanner.nextLine();
		while(!str.equals("0.00")){
			c = Float.valueOf(str).floatValue();
			for(i=1;sum<c;i++){
				sum = sum + 1f/(i+1);
			}
			System.out.printf("%d card(s)\n",i-1);
			str = scanner.next();
		}
		
	}
}


下面是测试可通过的代码:


import java.util.*;   
  
public class Main {   
  
    public static void main(String[] args) {   
        Scanner cin = new Scanner(System.in);   
        String str;   
        int maxCardNum = 0;   
           
        while(cin.hasNext())   
        {   
            str = cin.nextLine();   
            if(str.equals("0.00"))   
                break;   
               
            float len = Float.valueOf(str).floatValue();   
               
            maxCardNum = getCardNum(len);   
            System.out.println(maxCardNum + " card(s)");   
        }   
  
    }   
       
    private static int getCardNum(float len)   
    {   
        float value = 0;   
        int index = 2;   
           
        while (value < len)   
        {   
            value += 1.0/index;   
            index++;   
        }   
               
        return index-2;   
    }   
  
}  


这差在哪呢?

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator