关于判断无限大数字字符串能否被6整除问题的解决方法
import java.util.Random; public class TestBigString { /*** @param args*/ public void test(String s) {//将字符串转化成数组char[] a = s.toCharArray();test1(a); } public void test1(char[] a) {int temp1 = 0;String tem="";for (char c : a) {//便利数组从高位开始 对6取余数,然后余数*10+下一位继续相同操作 tem = String.valueOf(c); temp1 = temp1 * 10 + Integer.parseInt(tem); temp1 = temp1 % 6;}if (temp1 != 0) {//判断余数 如果是0 能整除 System.out.println("不能被6整除");} else System.out.println("能被6整除"); } public static StringBuffer generate() {StringBuffer bf=new StringBuffer();for (long i = 0; i < 10000000; i++) { Random rand = new Random(); int temp = rand.nextInt(10); bf.append(temp);}return bf; } public static void main(String[] args) {// System.out.println(Long.parseLong(s)%6);Long a = System.currentTimeMillis();String s = TestBigString.generate().toString();Long b = System.currentTimeMillis();System.out.println(b-a);//测试 时间System.out.println(s.length());TestBigString tbs = new TestBigString();Long c = System.currentTimeMillis();tbs.test(s);Long e = System.currentTimeMillis();System.out.println(e-c);//测试计算用的时间2-3秒 }}
页:
[1]