poj1047

高精度,java做

View Code
import java.io.*;
import java.util.*;
import java.math.*;

public class Main {
    
    public static boolean equal(String a, String b, int x)
    {
        int len = a.length();
        for (int i = 0; i < len; i++)
            if (a.charAt(i) != b.charAt((i + x) % len))
                return false;
        return true;
    }
    
    public static boolean ok(String a, String b)
    {
        for (int i = b.length(); i < a.length(); i++)
            b = "0" + b;
        for (int i = 0; i < b.length(); i++)
            if (equal(a, b, i))
                return true;
        return false;
    }
    
    public static void main(String[] args) throws FileNotFoundException
    {
//        Scanner cin = new Scanner(new FileInputStream("t.txt"));
        Scanner cin = new Scanner(System.in);
        while (cin.hasNext())
        {
            String st = cin.next();
            BigInteger a = new BigInteger(st);
            BigInteger b;
            boolean cycle = true;
            int n = st.length();
            for (int i = 1; i <= n; i++)
            {
                b = a.multiply(BigInteger.valueOf(i));
                if (!ok(st, b.toString()))
                {
                    cycle = false;
                    break;
                }
            }
            if (cycle)
                System.out.println(st + " is cyclic");
            else
                System.out.println(st + " is not cyclic");
        }
    }
}
原文地址:https://www.cnblogs.com/rainydays/p/2853533.html