String s = "674654564684654654654564......"很长,怎样判断里面的数是否能整除7,要具体代码实现
对,但是现在不用BigDecimal和BigInteger,用算法实现
不好办,只有弄成个长整形,然后 BigInteger%7看能不能=0
public class Divid7 { public static void main(String[] args) { String s="89002000003051054612638768513216549837"; final int bits=10; //split the string to a array by bits of this value, this value can by a suitable integer, only for small the maximium number of Long; if(!s.matches("\\d*")){ System.out.println("the string is not a number"); //check the string is viald; System.exit(-1); } long l[]=new long[s.length()%bits==0?s.length()/bits:s.length()/bits+1]; //how many array member will be created?
//set value for every member of Long array for(int i=0; i < l.length; i++){ l[i]=Long.parseLong(s.substring(i*bits, ((i*bits+bits)<s.length()?(i*bits+bits):s.length())));
这个package org.dudenglan;public class MyTest { /** * @param args */ public static void main(String[] args) { String num = "690602367"; System.out.print(getnum(num) == 0 ? "能被7整除" : "不能"); } static int index = 4; static String mystr; static int mi = 0; public static int getnum(String num) { int length = num.length(); int ii; for (int i = 0; i < length / 4; i++) { ii = Integer.parseInt(mi + num.substring(index - 4, index)); mi = ii % 7; mystr = num.substring(index); index += 4; } ii = Integer.parseInt(mi + num.substring(index - 4, length)); return ii % 7; } }
楼上的怕是不行. Integer 有那么大吗?提供一个思路: String s = "674654564684654654654564......"; int j=0 for(int i = 0;i<s.length;i++){ j=j*10+Integer.parseInt(s.charAt(i)); j=j%7; } if (j==0);//是7的位数 else;//不是
public static void main(String[] args) {
String s="89002000003051054612638768513216549837";
final int bits=10; //split the string to a array by bits of this value, this value can by a suitable integer, only for small the maximium number of Long;
if(!s.matches("\\d*")){
System.out.println("the string is not a number"); //check the string is viald;
System.exit(-1);
}
long l[]=new long[s.length()%bits==0?s.length()/bits:s.length()/bits+1]; //how many array member will be created?
//set value for every member of Long array
for(int i=0; i < l.length; i++){
l[i]=Long.parseLong(s.substring(i*bits, ((i*bits+bits)<s.length()?(i*bits+bits):s.length())));
}
long mod=0,temp=1;
String a;
for(int i=0; i<l.length; i++){
a=""+l[i];
if(i+1<l.length){
for(int j=0; j<bits; j++){
temp *= 10;
}
}else{
if(s.length()%bits==0){
for(int j=0; j<bits; j++){
temp *= 10;
}
}else{
for(int j=0; j<s.length()%bits; j++){
temp *= 10;
}
}
}
mod=(l[i]+temp*mod)%7;
temp=1;
}
if (mod == 0){
System.out.println("the number can be divided by 7");
}else{
System.out.println("the number CANNOT be divided by 7");
}
System.out.println("the mod number is:" +mod);
// System.out.println(Long.parseLong(s)%7); //check when a number can expressed by a Long
}}
2.从高位开始对每个long数据内的成员long除7求模.
3.将得出的模做为下一个long的起始值假如模是2,字符串分组后的长度为5,则为2*100000(共5个0),再加下一个数组内的long后,再开始除7求模.求得模后再次做同样的步骤,只至到最后一个数据成员.求出来的模就是实际上的模.也请能用biginteger来检测的同学检测一下
/**
* 判断一个字符串里的整数是否可以被一个整数整除.
*
* @author Aeolus
*
*/
public class Division
{
/**
* 判断字符串dividend里的整数是否可以被divisor整除.<br>
* 将dividend以1,000,000拆分放在整型数组里,从高位依次除divisor,将余数加在下一位上,直到最后一位若能除尽divisor,则返回true.
*/
public static boolean judge(String dividend, int divisor)
{
int length = dividend.length() / 6;
int[] arrDividend = new int[length + 1];
for (int i=0; i<length; i++)
{
arrDividend[i] = Integer.parseInt(dividend.substring(dividend.length()-6*(i+1), dividend.length()-6*i));
}
arrDividend[length] = Integer.parseInt(dividend.substring(0, dividend.length()-6*length));
for (int i=arrDividend.length-1; i>0; i--)
{
arrDividend[i-1] += arrDividend[i]%divisor;
}
if (arrDividend[0]%divisor == 0)
{
return true;
}
else
{
return false;
}
}
public static void main(String[] args)
{
System.out.println(Division.judge("777777777777777777777777777777777777721", 7));
}
}
实现:
public static boolean divisibleBy7(String input) {
if (input.charAt(0) == '-') {
input = input.substring(1);
}
StringBuilder buffer = new StringBuilder(input);
for (int i = input.length() - 1; i > 2; i--) {
char c1 = buffer.charAt(i);
char c2 = buffer.charAt(i - 1);
int i1 = c1 - '0';
int i2 = c2 - '0'; buffer.setLength(buffer.length() - 1);
if (i1 < 5) {
if (i2 < i1 * 2) {
buffer.setCharAt(i - 1, (char)(i2 + 10 - i1 * 2 + '0'));
int j = i - 2;
while (j >= 0) {
char c3 = buffer.charAt(j);
int i3 = c3 - '0';
if (i3 == 0) {
buffer.setCharAt(j, '9');
j--;
if (j < 0) {
throw new RuntimeException("Invalid number" + input);
}
continue;
} else {
buffer.setCharAt(j, (char)(i3 - 1 + '0'));
break;
}
}
} else {
buffer.setCharAt(i - 1, (char)(i2 - i1 * 2 + '0'));
}
} else {
char c3 = buffer.charAt(i - 2);
int i3 = c3 - '0';
i3 = i3 *10 + i2;
if (i3 < i1 * 2) {
int temp = i3 + 100 - i1 * 2;
buffer.setCharAt(i - 1, String.valueOf(temp).charAt(1));
buffer.setCharAt(i - 2, String.valueOf(temp).charAt(0));
int j = i - 3;
while (j >= 0) {
char c4 = buffer.charAt(j);
int i4 = c4 - '0';
if (i4 == 0) {
buffer.setCharAt(j, '9');
j--;
if (j < 0) {
throw new RuntimeException("Invalid number" + input);
}
continue;
} else {
buffer.setCharAt(j, (char)(i4 - 1 + '0'));
break;
}
}
} else {
int temp = i3 - i1 * 2;
if (temp > 9) {
buffer.setCharAt(i - 1, String.valueOf(temp).charAt(1));
buffer.setCharAt(i - 2, String.valueOf(temp).charAt(0));
} else {
buffer.setCharAt(i - 1, String.valueOf(temp).charAt(0));
buffer.setCharAt(i - 2, '0');
}
}
}
}
int result = Integer.valueOf(buffer.toString());
return result%7 == 0;
}
测试代码:
public static void test() {
for (int i = 0; i < 10000; i++) {
long l = new Random().nextLong();
String input = String.valueOf(l);
for (int j = 0; j < 100; j++) {
l = Math.abs(new Random().nextLong());
input = input + String.valueOf(l);
}
System.out.println(i + ":" + input);
BigInteger bInt = new BigInteger(input);
if ((bInt.mod(new BigInteger("7")).intValue() == 0) != divisibleBy7(input)) {
System.out.println(input + " error");
break;
}
}
}
System.out.println(foo("123457",0,0,3) == 0 );
public static int foo(String str, int offset, int div, int num){
int tmp = div*10+(str.charAt(offset)-'0');
if(offset == str.length() - 1){
return tmp - tmp/num*num;
}
else{
return foo(str,offset+1,tmp-tmp/num*num, num);
}
}
* @param args
*/
public static void main(String[] args) {
String num = "690602367";
System.out.print(getnum(num) == 0 ? "能被7整除" : "不能");
} static int index = 4;
static String mystr;
static int mi = 0; public static int getnum(String num) {
int length = num.length();
int ii;
for (int i = 0; i < length / 4; i++) {
ii = Integer.parseInt(mi + num.substring(index - 4, index));
mi = ii % 7;
mystr = num.substring(index);
index += 4;
}
ii = Integer.parseInt(mi + num.substring(index - 4, length));
return ii % 7;
}
}
674654564684654654654564 % 7
674654564684654654654-564=674654564684654090674654564684654090 % 7
674654564684654-090=674654564684564674654564684564 % 7
674654564684-564=...最后得到 xxx % 7
转成整数计算一下就可以了
基本就是四位整数减三位整数
//若一个整数的个位数字截去,再从余下的数中,减去个位数的2倍,如果差是7的倍数,则原数能被7整除。如果差太大或心算不
//易看出是否7的倍数,就需要继续上述「截尾、倍大、相减、验差」的过程,直到能清楚判断为止。例如,判断133是否7的倍
//数的过程如下:13-3×2=7,所以133是7的倍数;又例如判断6139是否7的倍数的过程如下:613-9×2=595 , 59-5×2
//=49,所以6139是7的倍数,余类推。
public class Test { public static void main(String[] args) throws Exception {
String string = "58226000001014050500005105020500";
System.out.println(isOk(string));
} static boolean isOk(String str) {
if (!str.matches("^[\\d]+$"))// 如果是非数字字符串,返回false
{
System.out.println("非数字字符串");
return false;
}
return Integer.parseInt(get(str)) % 7 == 0;
} static String get(String str) {
while (str.startsWith("0"))
str = str.substring(1);
int length = str.length();
if (length < 3)
return str;
else {
int a = Integer.parseInt(str.substring(length - 1)) * 2;// 最后一位*2
StringBuilder str2 = new StringBuilder(str.substring(0, length - 3));// 取前n-3位
int b = Integer.parseInt(str
.substring(length - 3, str.length() - 1))
- a;// 倒数第2、3位-尾数*2
if (b >= 10) {
str2.append(b);
}
else if (b >= 0) {
str2.append("0").append(b);
}
else {// 小于0,向前取一位
int len = str2.length();
for (int i = len - 1; i >=0; i--) {
if (str2.charAt(i) > 48) {// 判断前一位是否>0(0的char值为48),如果
// >0,降一
str2.replace(i, i + 1, Integer.parseInt(str2.charAt(i)
+ "")
- 1 + "");
break;
}
else {
str2.replace(i, i + 1, "9");// 当前位是0,替换成9
}
}
if (b > -90) {
str2.append(100 + b);
}
else if (b > -100)
str2.append("0").append(100 + b);
}
str = null;
System.gc();// 这里有必须建议虚拟机垃圾回收
return get(str2.toString());
}
}
}
代码:
public class DividedBy7 {
//给定一个长度任意的数字串(如:"674654564684654654654564......"很长)
//返回被7除后的余数.返回-1表示参数d不是数字串.
static int[] t={0,3,6,2,5,1,4};
public static int dividedBy7(String d)
{ int s=0;
char ch=0;
for(int i=0;i<d.length();i++)
{
if(!Character.isDigit(ch=d.charAt(i))) return -1;//不是数字
s=( t[s]+(ch-'0')%7 )%7;
}
return s;//返回余数
}
public static void main(String[] args) {
String s="674654564684654654654564"+
"746545646846546546545646"+
"846546546545645646846599"+
"465465465456456468465927"+
"546545645646846595368568";
System.out.println(s+"除以7的余数:"+dividedBy7(s));
}
}程序运行结果:
6746545646846546546545647465456468
4654654654564684654654654564564684
6599465465465456456468465927546545
645646846595368568除以7的余数:5
int ok=0;
int i=0;
String tmp;
int k=0;
for(i=0;i<str.length()-1;i++){//截取每一个取余数,加到下一个前面再取余数
tmp=str.substring(i, i+1);
k=Integer.parseInt(tmp);
ok=ok*10+k;
ok=ok%7;
}
tmp=str.substring(i);
k=Integer.parseInt(tmp);
ok=ok*10+k;
ok=ok%7;
System.out.println(ok);
为什么这样定义数组,那要是判断能否被6整除又怎么定义int[],
还有s=( t[s]+(ch-'0')%7 )%7;不是很懂
哈哈,经典
若要判断能否被k整除(2<=k<=9),则定义int[]是:
static int[] t=new int[k];
for(int i=;i<k;i++)
{
t[i]=i*10 % k;
}只好按上边定义好了数组t,则数字串s除以k的余数一定按下边循环:int r=0;//余数
for(int i=0;i<s.length();i++)
{
r=( t[r]+(d.charAt(i-'0')%7 ) %7;
//循环计算余数r(只用最基本的+,-,%,不用乘法,这样永远不会溢出)
//这是由一个数学公式来的.(展开叙述要打许多字了)
}
这个循环结果,r就是除以k的余数了.
r=0;//余数
for(int i=0;i <s.length();i++)
{
r=( t[r]+( s.charAt(i)-'0' )%7 ) %7;
//循环计算余数r(只用最基本的+,-,%,不用乘法,这样永远不会溢出)
//这是由一个数学公式来的.(展开叙述要打许多字了)
}
这个循环结束,r就是除以k的余数了.
int[] mod = { 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1 };
int s = 0;
char ch = 0;
for (int i = 0; i < str.length(); i++) {
ch = str.charAt(i);
s = mod[high[s] + (ch - '0')];
}
System.out.println(s);
这个其实是逐位计算余数,其中high[s]其实意思是等同于 s * 10 % 7,
mod[high[s] + (ch - '0')] 等同于 (s * 10 + (ch - '0')) % 7
这些是为了方便不用计算mod运算,提高速度
public static int g(String str, int x) {
return g(str, 0, 0, x);
} private static int g(String str, int start, int number, int x) {
if (start >= str.length()) {
return number % x;
}
number = (number * 10 + str.charAt(start) - '0') % x;
return g(str, start + 1, number, x);
}
收藏O(∩_∩)O哈哈~
Integer in=Integer.parseInt(s);
if(in/7==0){
System.out.println("整除");
}
else {
System.out.println("不能整除");}
String s = "674654564684654654654564......";
int j=0
for(int i = 0;i<s.length;i++){
j=j*10+Integer.parseInt(s.charAt(i));
j=j%7;
}
if (j==0);//是7的位数
else;//不是