"High-precision JAVA" HDU 5920 Ugly problem

Source: Internet
Author: User
Tags arithmetic

Test instructions

give you a number n (n < 10^1000) and split it into several palindrome (no more than 50) output splitting scheme

Analysis

It is not difficult to think, we can give N minus one less than his biggest palindrome string, so as to construct a minimum number of palindrome string, the method can be directly to the first half reversal paste to the second half, if the original number is larger, then the first half reduced by 1, and then reversed post half
The place to compare pits is if 11 is larger than N, then the first half-1 becomes 00, a special sentence should be 9; If the structure is 101 than N, it should be sentenced to 99.

This question is not much good to say, I would like to record the Java programming experience, will be updated later

Main frame
import java.io.*;// 这个没用过import java.util.*;import java.lang.*;//import java.math.*;import java.text.*;publicclass Main{    publicstaticvoidmain(String[] args)    {        new Scanner(System.in);    }}

The class name here, when submitted to OJ, must be taken as main, but when the machine runs, must be taken as the same name as the file name, otherwise CE

Input
int n = cin.nextInt//读入一个整数BigInteger n = cin.nextBigInteger// 读入一个大整数double n = cin.nextDouble// 读入一个浮点数String n = cin.next// 读入一个字符串

There seems to be a lot of regularity to follow ...

Output
int10;System.out.print("n = "//不换行System.out.println("n = "+n);//换行

Print will output
n = 10
PRINTLN will output
n =
10

Defining data types

A single variable feels less different from C + +
int is the wrapper class for the basic data type Integer is int
Basic data types, divided into Boolean, byte, int, char, long, short, double, float;
To be able to use these basic data types as Object operations, Java introduces a corresponding wrapper type for each basic data type
For example, when sorting requires custom collation, only the wrapper type
Defines the array as

Type [] s = new Type [55];

And when you use the BigInteger method to convert a string to BigInteger

BigInteger n = new BigInteger(s,10);

The first argument is a string, the second argument is the corresponding binary, and for this new, I'm not too understanding

Compare size

Here you need to understand the difference between compareto and = =
In layman's terms, = = is strongly equal, must be equal to the same object on both sides (which can be interpreted as the same address) to return True
and A.compareto (b) returns true as long as the values of A and B are equal
The specific comparison method, if the data type has a defined comparison rule, according to the comparison rules, such as String by dictionary order, by numerical size
The return value of the CompareTo represents the result of the comparison, and if A>b returns a positive number, A and B return equal to 0, A<B returns negative numbers

Some operations on string strings

Refer to String for details.
string is not equal to char[], and its value cannot be changed after it is created
Char[] Turn string

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

Take string subscript i to J substring

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

Take the character of the string s subscript i

char c  = s.charAt(i);

Comparison of two string sizes

s1.compare(s2);
Some operations of BigInteger

For more details, refer to BigInteger
Arithmetic

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

Bit arithmetic

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)
Some operations of BigDecimal

Because I didn't use bigdecimal, just throw the link.
BigDecimal

Shell operations

This is not to be a Java knowledge point, run Java under the shell, standard read-in and file reading, it should be better understood

javac name.java // 编译java name // 运行,标准读入,标准输出java name <data.in //运行,文件读入,标准输出java name >data.out // 运行,标准读入,文件输出java name <data.int >data.out // 运行,文件读入,文件输出
STL (Collections)

Java also has a similar c++stl, is a class called collections, something too much, not very familiar, first drop the link
Collections
Tell me about the sorting method

    • Sort in natural order (small to large)

    • Order of custom order (large to small)

      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();   // 实例化一个Comparator对象     Arrays.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.*;p ublic 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 < && N.compareto (biginteger.valueof (0)) >0) {String t = n.tostring ()                ;                int lenth = T.length ();                    if (lenth = = 1) {Ans+=1;s[ans] = t;                Break                    } 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--)         T1+=t2.charat (i);                    BigInteger m = new BigInteger (t1,10);                        if (N.compareto (m) <0) {if (T1.compareto ("one") = = 0) T1 = "9";                        else if (T1.compareto ("101") = = 0) T1 = "99";                            else{m = new BigInteger (t2,10);                            m = M.subtract (Biginteger.one);                                if (M.compareto (biginteger.zero) = = 0) {t1 = "";                            T1 + = T.charat (LENTH/2);                                } else{t2 = "";                                T2+=m.tostring ();                                 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);                }}}//Ans+=1;                S[ans] = T1;                m = new BigInteger (t1,10);            n = n.subtract (m);                    } System.out.println ("Case #" +case+ ":");                    System.out.println (ANS);        for (int i = 1;i<=ans;i++) System.out.println (S[i]); }    }}

High precision JAVA HDU 5920 Ugly problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.