首页 > 试题广场 >

大整数的因子

[编程题]大整数的因子
  • 热度指数:11897 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
已知正整数k满足2<=k<=9,现给出长度最大为30位的十进制非负整数c,求所有能整除c的k.

输入描述:
若干个非负整数c,c的位数<=30
每行一个c


输出描述:
每一个c的结果占一行
1) 若存在满足 c%k == 0 的k,输出所有这样的k,中间用空格隔开,最后一个k后面没有空格。
2) 若没有这样的k则输出"none"

注意整数溢出问题
不要对-1进行计算
示例1

输入

30
72
13
-1

输出

2 3 5 6
2 3 4 6 8 9
none
import java.util.Scanner;
import java.math.BigInteger;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            String str = in.next();
            if (str.equals("-1")) {
                break;
            }
            BigInteger c = new BigInteger(str);
            boolean find = false;
            for (int k = 2; k <= 9; k++) {
                if (c.mod(new BigInteger(String.valueOf(k)))
                        .equals(new BigInteger("0"))) {
                    find = true;
                    System.out.print(k);
                    for (int k1 = k + 1; k1 <= 9; k1++) {
                        if (c.mod(new BigInteger(String.valueOf(k1)))
                                .equals(new BigInteger("0"))) {
                            System.out.print(" " + k1);
                        }
                    }
                    System.out.println();
                    break;
                }
            }
            if (!find) {
                System.out.println("none");
            }
        }
    }
}

编辑于 2024-01-30 20:54:12 回复(0)
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
    public static void main(String args[])
    {
        Scanner in=new Scanner(System.in);
        while(in.hasNext())
        {            
            BigInteger b=in.nextBigInteger();                                            
                int len=0;
                for(int j=2;j<=9;j++)
                {
                    BigInteger t=BigInteger.valueOf(j);
                    if(b.remainder(t).equals(BigInteger.ZERO))
                    {
                        len++;
                        System.out.print(j+" ");
                    }
                }
                if(len==0)
                {
                    System.out.println("none");
                }else
                {
                    System.out.println();
                }   
        }
    }
}

发表于 2020-04-16 20:54:11 回复(0)
import java.math.BigInteger;
import java.util.Scanner;

public class Main {
  
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        
        while(in.hasNext()){
            BigInteger n = in.nextBigInteger();              //从键盘获取一个大数值
            int sum = 0;
            int flag = 0;
            int index = 0;                                   //定义index表示是否存在被除尽的数
            
            for(int i = 2; i <= 9; i++){            
              BigInteger a = new BigInteger(String.valueOf(i));
              BigInteger num = n.mod(a);
              BigInteger b = new BigInteger("0");
              if(num.compareTo(b) == 0){
                  index = 1;
                  sum++;                                     //定义sum记录一共有多少个值能被除尽
              }
            }
            
            for(int i = 2; i <= 9; i++){
              BigInteger a = new BigInteger(String.valueOf(i));
              BigInteger num = n.mod(a);
              BigInteger b = new BigInteger("0");
              if(num.compareTo(b) == 0){
                  flag++;
                  System.out.print(flag != sum ? i + " " : i);
              }
              
            }
            
            if(index == 0){
                System.out.println("none");
            }

        }
        in.close();
    }
}
发表于 2020-03-02 22:28:52 回复(0)
import java.math.BigInteger;
import java.util.*;

public class Main {
    public  static  void  main(String[] args) {
       Scanner input = new Scanner(System.in);
        while (input.hasNext()) {
            String c = input.nextLine();
            boolean flag = true;
            if ("-1".equals(c)) {
                break;
            } else {
                BigInteger big = new BigInteger(c);
                for (int i = 2; i <= 9; i++) {
                    if (big.mod(BigInteger.valueOf(i)).intValue() == 0) {
                        if (!flag) {
                            System.out.print(" ");
                        }
                        System.out.printf("%d", i);
                        flag = false;
                    }
                }
            }
            if (flag) {
                System.out.println("none");
            } else {
                System.out.println();
            }
        }
    }
}

发表于 2020-02-28 09:39:59 回复(0)
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        while (reader.hasNext()) {
            String input = reader.next();
            ArrayList<Integer> res = new ArrayList<>();
            for (int divisor = 2; divisor <= 9; ++divisor) {
                int mod = 0;
                for (int j = 0; j < input.length(); ++j) {
                    int digit = input.charAt(j) - '0';
                    mod = (mod * 10 + digit) % divisor;
                }
                if (mod == 0)
                    res.add(divisor);
            }
            StringBuilder sb = new StringBuilder();
            if (res.size() > 0) {
                for (int i: res) {
                    sb.append(i+" ");
                }
                System.out.println(sb.substring(0, sb.length()-1).toString());
            } else {
                System.out.println("none");
            }
        }
    }
}

发表于 2018-04-07 22:27:03 回复(0)
 直接用除法,思路就是上一位取余数*10加上当前位除以k,
 一直到所有位判断完毕,这个其实就是手算除法的模拟过程
import java.util.Scanner;

/*
 * 
 *直接用除法,思路就是上一位取余数*10加上当前位除以k,
 *一直到所有位判断完毕,这个其实就是手算除法的模拟过程
 * 
 * */
public class BigNumer {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String num = in.nextLine();
if (num.equals("-1")) {
break;
}
process(num);
}
in.close();
}

static void process(String num) {
StringBuffer buffer = new StringBuffer();
for (int k = 2; k < 10; k++) {
if (isContaineFactor(num, k)) {
buffer.append(k + " ");
}
}
if (buffer.length() > 0) {
buffer.delete(buffer.length() - 1, buffer.length());
}
else {
buffer.append("none");
}
System.out.println(buffer.toString());
}

static boolean isContaineFactor(String num, int k) {
int num1 = 0;
int num2 = 0;
int len = num.length();
for (int i = 0; i < len; i++) {
num1 = (num.charAt(i) - '0') + num2 * 10;
num2 = num1 % k;
}
return num2 == 0 ? true : false;
}
}

发表于 2016-07-21 12:05:46 回复(0)

问题信息

难度:
6条回答 9999浏览

热门推荐

通过挑战的用户

查看代码
大整数的因子