【基础】循环数(java)

题目描述

n 位的一个整数是循环数(cyclic)的条件是:当用一个 1 到 n 之间的整数去乘它时, 会得到一个将原来的数首尾相接循环移动若干数字再在某处断开而得到的数字。也就是说,如果把原来的数字和新的数字都首尾相接,他们得到的环是相同的。只是两个数的起始数字不一定相同。例如,数字 142857 是循环数,因为:
142857 *1 = 142857
142857 *2 = 285714
142857 *3 = 428571
142857 *4 = 571428
142857 *5 = 714285
142857 *6 = 857142

输入

写一个程序确定给定的数是否是循环数。输入包括多个长度为 2 位到 60 位的整数。(注意,先导的0也是合理的输入不应该被忽略,例如 "01"是 2 位数,“1” 是 1 位数。)

输出

对于每一个输入的整数,输出一行表明它是否是循环数。

样例输入

142857
142856
142858
01
0588235294117647

样例输出

142857 is cyclic
142856 is not cyclic
142858 is not cyclic
01 is not cyclic
0588235294117647 is cyclic

解题代码:

import java.math.BigInteger;
import java.util.Scanner;
public class Main {
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        String num_str;
        while(in.hasNextLine())
        {
            num_str = in.nextLine();
            if(fornum(num_str) == true)
                System.out.println(num_str + " is cyclic");
            else
                System.out.println(num_str + " is not cyclic");
        }
    }
    public static boolean fornum(String str)
    {
        int len = str.length();
        String b = String.valueOf(len);
        BigInteger num = new BigInteger(str);
        BigInteger mul = num.multiply(new BigInteger(b));
        String mul_str = mul.toString();
        if(mul_str.length() == len)
        {
            for (int i = 0; i < len; i++)
                if(mul_str.indexOf(str.charAt(i)) == -1)
                    return false;
        }else
        {
            return false;
        }
        return true;
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值