程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> Ural 1095 Nikifor 3 數論

Ural 1095 Nikifor 3 數論

編輯:C++入門知識

題意:給你一個數,其中包含數字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.2cto.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 = 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(); 
      } 
    } 


作者:wmn_wmn

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved