北大的在线测试
题目在:http://poj.org/problem?id=1061
我用java写的,给的结果就是:Time Limit Exceeded,我知道java的运行速度比较慢,谁能帮我有话一下,可以在那个系统中通过,分就全部是谁的了?
贴上我的代码:import java.util.*;
public class Main {
public static void main(String[] args) throws Exception{
int x=0,y=0,m=0,n=0,l=0,count = 0;
Scanner cin = new Scanner(System.in);
x = cin.nextInt();
y = cin.nextInt();
m = cin.nextInt();
n = cin.nextInt();
l = cin.nextInt();

if(m==n){
System.out.println("Impossible");
return;
}
while (x!=y) {
count++;
x+=m;
y+=n;
if (x>l-1) {
x=x%l;
}
if (y>l-1) {
y=y%l;
}
}
System.out.println(count);
}
}

解决方案 »

  1.   

    更像是算法的问题,看看这个行不行.
    http://www.iteye.com/topic/480856
      

  2.   

    我重写了代码,想用相对速度来解决,结果:wrong answer,不明白为什么?
    import java.util.*;
    public class Main {
    public static void main(String[] args) throws Exception{
    int x=0,y=0,m=0,n=0,l=0,count = 0;
    Scanner cin = new Scanner(System.in);
    x = cin.nextInt();
    y = cin.nextInt();
    m = cin.nextInt();
    n = cin.nextInt();
    l = cin.nextInt();

    if(m==n){
    System.out.println("Impossible");
    return;
    }
    /*while (x!=y) {
    count++;
    x+=m;
    y+=n;
    if (x>l-1) {
    x=x%l;
    }
    if (y>l-1) {
    y=y%l;
    }
    }*/
    else if(m>n){
    if(x<y){
    count=(y-x)/(m-n);
    }else{//题目说过了 x!=y
    count=(l-x+y)/(m-n);
    }
    }
    else if(m<n){
    if(x<y){
    count=(l-y+x)/(n-m);
    }else{
    count=(x-y)/(n-m);
    }
    }
    System.out.println(count);
    }
    }