Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
有Java的程序高手麻烦检查一下问题出在哪里?谢谢 QQ:464223952import java.io.*; import java.util.*; public class Main { public static void main(String[] args)throws Throwable{ BufferedReader br=new BufferedReader( new InputStreamReader( new FileInputStream("src/my/input.txt"))); String s;String[] ss; s=br.readLine(); ss=s.split(" ",5); int x=Integer.parseInt(ss[0]); int y=Integer.parseInt(ss[1]); int m=Integer.parseInt(ss[2]); int n=Integer.parseInt(ss[3]); int l=Integer.parseInt(ss[4]); x%=l;y%=l; x+=l;y+=l; x%=l;y%=l; //把dis限定在[0,l)范围内 int dis=y-x; dis%=l;dis+=l;dis%=l; //把mov限定在[0,l)范围内 int mov=n-m; mov%=l;mov+=l;mov%=l; Object result=null; if(dis==0)result=0; else if(mov==0)result="Impossible"; else{ //求最大公约数 int com=common(mov,l); //dis不能整除mov和l的最大公约数则无解 if(dis%com!=0)result="Impossible"; else{ //除以最大公约数化到最简 dis/=com; mov/=com; l/=com; //对不定方程dis+X*mov=Y*l求解,即Y从1~mov依次取值看X是否能得到整数, //能整除则返回X的值,由数论知识知一定能找到 for(int i=1;i<=mov;i++){ if((l*i-dis)%mov==0){ result=(l*i-dis)/mov; break; } } } } System.out.println(result); br.close(); } //计算最大公约数的函数 static int common(int a,int b){ if(a==0)return b; else if(b==0)return a; if(a>=b)return common(a%b,b); else return common(a,b%a); } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator