日期:2014-05-20 浏览次数:20785 次
public static void main(String[] args) { BigInteger big = new BigInteger("54354358093245892583457839730957349857394857367935384895"); System.out.println("余数是"+big.divideAndRemainder(BigInteger.valueOf(7))[1].toString()); }
------解决方案--------------------
public static void main(String[] args) { String str = "54354358093245892583457839730957349857394857367935384895"; int n= 7; String yushu = ""; if(str.length()<5){ yushu=String.valueOf(Integer.parseInt(str)%n); }else{ for (int i = 0; i < str.length()/5; i=i+5) { String temp = yushu+str.substring(i, i+5); int cloum = Integer.parseInt(temp); int yu = cloum%n; if(yu!=0){ yushu=String.valueOf(yu); }else{ yushu=""; } } } if(yushu.length()<=0||"0".equals(yushu)){ System.out.println("可以被整除"); }else{ System.out.println("不可以被整除"); } }
------解决方案--------------------
题目翻译一下:有不定大小的数字x,求与x之间差值最小的7的公约数,给出差值大小
例:
x = 15
设y,且y%7=0
答案就是 x+(y-x)
小于long长度的数字直接用long计算
大于long长度的算法没想好,哈哈
------解决方案--------------------
public static void main(String[] args) { final int leg=18; StringBuilder str=new StringBuilder("49793821479387619794872314840"); int n=7; long count=0; while(true){ long num; if(str.length()<=leg){ num=Long.parseLong(str.substring(0, str.length())); count+=num%n; break; }else{ num=Long.parseLong(str.substring(0, leg)); count+=num%n; str.delete(0, leg); } } System.out.println(count%n==0?"可以":"不可以"); }
------解决方案--------------------