Question: give you a number, which contains numbers 1 2 3 4, so that you can rearrange the number so that the number can be divisible by 7.
Idea: After calculation, we found that 1234 of them are arranged in 24 full order, and 7 are the remainder, including, and 6. Therefore, you only need to calculate the result of 7 Remainder of the number pair except 1 2 3 4 4, and then let the following 1234 complete the remainder. Pay attention to a special case, such as 1001234. When 1234 is removed, the first 1 is removed. The consequence is that the number after 1234 is removed is 1. to prevent this situation, you can add 0 at last. I used to write large numbers in java. In fact, it can be done with C ++ ..
Code:
[Cpp] www.2cto.com
Import java. util .*;
Import java. math. BigInteger;
Public class Main {
Public static void main (String [] args ){
Cin = new partition (System. in );
Int numcase;
Numcase = cin. nextInt ();
For (int j = 1; j <= numcase; ++ j ){
BigInteger n;
Int flag1 = 0, flag2 = 0, flag3 = 0, flag4 = 0;
N = cin. nextBigInteger ();
String ss = n. toString ();
BigInteger x = new BigInteger ("0 ");
BigInteger x1 = new BigInteger ("1 ");
BigInteger x2 = new BigInteger ("2 ");
BigInteger x3 = new BigInteger ("3 ");
BigInteger x4 = new BigInteger ("4 ");
BigInteger x5 = new BigInteger ("5 ");
BigInteger x6 = new BigInteger ("6 ");
BigInteger sum = x;
Char ch [] = ss. toCharArray ();
Int cntzero = 0;
For (int I = 0; I <ss. length (); ++ I ){
If (ch [I] = '1' & flag1 = 0 ){
Flag1 = 1;
Continue;
}
Else if (ch [I] = '2' & flag2 = 0 ){
Flag2 = 1;
Continue;
}
Else if (ch [I] = '3' & flag3 = 0 ){
Flag3 = 1;
Continue;
}
Else if (ch [I] = '4' & flag4 = 0 ){
Flag4 = 1;
Continue;
}
Else if (ch [I] = '0 '){
Cntzero ++;
Continue;
}
Int y = ch [I]-'0 ';
BigInteger z = new BigInteger ("" + y );
BigInteger zz = new BigInteger ("10 ");
Sum = sum. multiply (zz );
Sum = sum. add (z );
}
BigInteger p = new BigInteger ("10000 ");
Sum = sum. multiply (p );
Ss = sum. toString ();
// System. out. println (ss );
BigInteger q = new BigInteger ("7 ");
BigInteger mm = sum. mod (q );
Mm = q. subtract (mm );
Ss = mm. toString ();
// System. out. println (ss );
BigInteger ans;
If (mm. compareTo (q) = 0 ){
Ans = new BigInteger ("3241 ");
Sum = sum. add (ans );
Ss = sum. toString ();
System. out. print (ss );
}
Else if (mm. compareTo (x1) = 0 ){
Ans = new BigInteger ("1324 ");
Sum = sum. add (ans );
Ss = sum. toString ();
System. out. print (ss );
}
Else if (mm. compareTo (x2) = 0 ){
Ans = new BigInteger ("1234 ");
Sum = sum. add (ans );
Ss = sum. toString ();
System. out. print (ss );
}
Else if (mm. compareTo (x3) = 0 ){
Ans = new BigInteger ("2341 ");
Sum = sum. add (ans );
Ss = sum. toString ();
System. out. print (ss );
}
Else if (mm. compareTo (x4) = 0 ){
Ans = new BigInteger ("1243 ");
Sum = sum. add (ans );
Ss = sum. toString ();
System. out. print (ss );
}
Else if (mm. compareTo (x5) = 0 ){
Ans = new BigInteger ("1342 ");
Sum = sum. add (ans );
Ss = sum. toString ();
System. out. print (ss );
}
Else if (mm. compareTo (x6) = 0 ){
Ans = new BigInteger ("2134 ");
Sum = sum. add (ans );
Ss = sum. toString ();
System. out. print (ss );
}
For (int k = 0; k <cntzero; ++ k)
System. out. print (0 );
System. out. println ();
}
}
}
Author: wmn_wmn