Ural 1095 Nikifor 3 数论

来源:http://acm.timus.ru/problem.aspx?space=1&num=1095

题意:给你一个数,其中包含数字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++完全可以搞定。。

代码:

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 = 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();
      }
	}
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值