Article From:https://www.cnblogs.com/greenty1208/p/9127498.html


Give you a numeric n (n < 10^1000) and split it into several palindrome strings (no more than 50) to output the split scheme.


It is not hard to think that we can reduce the maximum number of palindrome strings to n each time, so that we can make the least number of palindrome strings. The method can put the first half of the reverse to the latter half. If the number is larger than the original number, then the front half is reduced by 1, and then the reverse is attached to the latter half.
The place of comparison is that if the 11 is larger than N, then the first half of the -1 becomes 00, and it should be 9. If the structure is 101 larger than N, it should be judged to be 99.

There is not much to say about this question. I want to record here.javaThe programming experience will be updated in the future

body framework”

import java.io.*; // This is no use
import java.util.*;
import java.lang.*; //
import java.math.*;
import java.text.*;
public class Main
    public static void main(String[] args)
        Scanner cin = new Scanner(System.in);

The class name here must be taken as Main when submitted to OJ, but when the machine is running, it must be taken as the same name as the filename, otherwise it will be CE.


int n = cin.nextInt(); //Read into an integer
BigInteger n = cin.nextBigInteger(); // Read into a large integer
double n = cin.nextDouble(); // Read a floating-point number
String n = cin.next(); // Read a string

It looks very regular here…

int n = 10;
System.out.print("n = "+n); //nowrap
System.out.println("n = "+n);//Change line

print Will output
n = 10
println Will output
n =

define data types

Single variable feeling is not very different from c++
int The basic data type Integer is a wrapper class of int
The basic data types are divided into Boolean, byte, int, char, long, short, double and float.
In order to treat these basic data types as objects, Java introduces corresponding packing types for each basic data type.
For example, when sorting requires custom collation rules, only packages can be used.
Define the array as

Type [] s = new Type [55];

And using the BigInteger method to convert the string to BigInteger.

BigInteger n = new BigInteger(s,10);

The first parameter is a string, and the second parameter is the corresponding number. For this new, I don’t quite understand it now.

comparative size”

Here you need to understand the difference between compareTo and = =.
Popularly speaking, = = is strong equality, must be equal sign on both sides of the same object (can be understood as the same address) will return to True
A.compareTo (b) returns to True as long as the A and b values are equal.
A specific comparison method, if the data type has the definition of comparison rules, according to the comparison rules, such as String, according to the dictionary order, according to the numerical value.
compareToThe return value indicates the result of comparison. If a&gt, B returns positive numbers, a and B equals 0, a< B returns negative.

string String operations.

We can refer to String. specifically
StringIt is not equivalent to char[], and its value can not be changed after it is created.
char[] Turn String

char data[] = {'a', 'b', 'c'};
String str = new String(data);

The subscript of String is a substring from I to j

String subs = s.substring(i,j+1);

Take the character of the string s to subscribe to the I

char c  = s.charAt(i);

Compare two string sizes



We can refer to BigInteger specifically
Four operations

a = a.add(b); // a+=b;
a = a.subtract(b); // a-=b
a = a.multiply(b); // a*=b;
a = a.divide(b); //a/=b;
a = a.remainder(b) ; // a%=b;

Bit operation

a = a.and(b); // a = a&b;
a = a.or(b); // a = a|b;
a = a.xor(b) // a = a^b;
a = a.shiftLeft(n) // a = (a<<n);
a = a.shiftRight(n) // a=(a>>n)


Because I haven’t used BigDecimal yet, toss the link first

shell operation”

This is not a knowledge of Java, running Java under shell, reading standard files and reading files. It should be well understood.

javac name.java // CompileJava name / / run, standard reading, standard outputJava name < data.in / / run, file read in, standard outputJava name > data.out / / run, standard reading,File outputJava name < data.int > data.out / / run, file read in, file output

STL (Collections)

javaThere is also a c++STL like existence. It is a class called Collections. There are too many things, not very familiar with them.
Talk about the sorting method

  • Order in natural order (from small to large)

    nt[] a = new int[15];
    int n = cin.nextInt();
    for(int i = 1;i<=n;i++) a[i] = cin.nextInt();
  • Custom order (from large to small) Order

    import java.io.*;
    import java.lang.*; 
    import java.util.*;
    import java.math.*;
    import java.text.*;
    public class test
    public static void main(String[] args)
        Scanner cin = new Scanner(System.in);
        Integer[] a = new Integer[15];
        int n = cin.nextInt();
        for(int i = 1;i<=n;i++) a[i] = cin.nextInt();
        Comparator <Integer> c = new Mycomparator();   // Instantiate a Comparator objectArrays.sort (a, 1, n+1, c);For (int i = 1; i< =n; i++) System.out.print (a[i]+)"");System.out.println ();}}Class Mycomparator implements Comparator < Integer>{Public int compare (Integer x, Integer y){If (x > y) return -1;If (x< y) return 1;Return 0;}}

AC code”

import java.util.Scanner;
import java.math.*;
import java.text.*;
public class Main
    public static void main(String[] args)
        Scanner cin = new Scanner(System.in);
        int T = cin.nextInt();
        String [] s = new String [55];
        for(int Case=1;Case<=T;Case++)
            BigInteger n = cin.nextBigInteger();
            int ans = 0;
            while(ans < 50 && n.compareTo(BigInteger.valueOf(0))>0 )
                String t = n.toString();
                int lenth = t.length();
                if(lenth == 1){
                    ans+=1;s[ans] = t;
                    String t1 = "";
                    String t2 = "";
                    int mid = lenth/2-1;
                    for(int i = 0;i<=mid;i++)
                        t2 += t.charAt(i);
                    t1 = t2;
                    if(lenth %2!=0) t1 += t.charAt(lenth/2);
                    for(int i = mid;i>=0;i--)
                    BigInteger m = new BigInteger(t1,10);
                        if(t1.compareTo("11") == 0) t1 = "9";
                        else if(t1.compareTo("101") == 0) t1 = "99";
                            m = new BigInteger(t2,10);
                            m = m.subtract(BigInteger.ONE);
                            if(m.compareTo(BigInteger.ZERO) == 0){
                                t1 = "";
                                t1 += t.charAt(lenth/2);
                                t2 = "";
                                mid = t2.length()-1; 
                                t1 = t2;
                                if(lenth%2 != 0)
                                t1 += t.charAt(lenth/2);
                                for(int i = mid;i>=0;i--)   t1+=t2.charAt(i);
                s[ans] = t1;
                m = new BigInteger(t1,10);
                n = n.subtract(m);
            System.out.println("Case #"+Case+":");
                    for(int i = 1;i<=ans;i++) System.out.println(s[i]);