Ural 1095 Nikifor 3 数论
题意:给你一个数,其中包含数字1 2 3 4,让你对这个数的数字重新排列,使其目的数能被7整除。思路:经过计算发现,1234有24种全排列,对7取余,其中包含了0,1,2,3,4,5,6这7种情况。所以,只需要计算除去1 2 3 4以外的数对7取余的结果,然后让后面的1234对其补余即可。需要注意一种特殊情况如1001234,在去1234时,把第一个1去掉了,这样的后果是求出的去掉1234后的数为1.为防止这种情况,可以最后补0。本来以为用到大数,所以用java写的。其实用C++完全可以搞定。。
代码:
[cpp] www.zzzyk.com
import java.util.*;
import java.math.BigInteger;
public class Main {
public static void main(String[] args){
Scanner cin = new Scanner(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 =
补充:软件开发 , C++ ,