Find the sum of two descending numbers of integers of int (Java)--2015 Huawei Machine Questions

Source: Internet
Author: User

Title Description: Give an integer (negative number using its absolute value), output the two decrements in this integer (1. The maximum number of decrements; 2. The sum of the maximum number of members in the descending number).

Decrement: The number of descending digits of a number refers to the number of contiguous digits ranging from large to small, and does not contain the same number of adjacent digit sizes.

Maximum decrement: The largest of the values in all decrements of the input integer. such as: 75345323, descending number has: 75,753,53,53,532,32. Then the maximum number of decrements is 753.

The sum of the numbers and the maximum number of decrements: 75345323 of the descending number: 75 of the sum of the =12 (7+5=12), 753 members of the sum of =15 (7+5+3=15), 53 of the sum of =8 (5+3=8), 532 and =10 (5+3+2=10), 32 The sum of the =5 (3+2=5). So the sum of the numbers and the maximum number of decrements is 753.

Output = maximum number of decrements + number of decrements. (1506=753+753)

Run time limit: Unlimited

Memory Limit: Unlimited

Input: an int type integer. Example: 75345323

Output: an int type integer. Example: 1506

Example input: 75345323

Sample output: 1506

1 Importjava.util.ArrayList;2 ImportJava.util.Iterator;3 ImportJava.util.Scanner;4 5  Public classSubtraction {6      Public Static voidMain (string[] args)7     {8Scanner scan =NewScanner (system.in);9String str =scan.nextline ();Ten Dijianshu (str); One              A     } -     Static voidDijianshu (String str) -     { the         Char[] arr=Str.tochararray (); -Arraylist<integer> la=NewArraylist<integer> ();//used to hold the descending number -          for(inti=0;i<arr.length-1;i++)//search for diminishing numbers -         { +             if(arr[i]>arr[i+1]) -             { +String s= "" +arr[i]+arr[i+1]; A La.add (Integer.parseint (s)); at                  for(intj=i+1;j<arr.length-2;j++) -                 { -                     if(arr[j]>arr[j+1]) -                         { -S=s+arr[j+1]; - La.add (Integer.parseint (s)); in                          Break;//prevent the angle label from continuing down -                         } to                      Break;//prevent the angle label from continuing down +                 } -s=NULL; the             }     *         } $ //iterator<integer> it=la.iterator ();Panax Notoginseng //While (It.hasnext ()) { - //System.out.print (It.next () + ""); the //        } +String[] A=NewString[la.size ()];//Convert the elements in the collection into strings and convert them into character arrays, so that the sum of the numbers can be reduced A         intmax=0;//record the maximum number of decrements the          for(intI=0;i<la.size (); i++) +         { -             if(La.get (i) >max) $             { $Max=la.get (i);//find the maximum number of decrements -             } -A[i]=la.get (i). toString ();//Converts an element in a collection into a string the         } -String ss=NULL;Wuyi         intmax2=0;//record the sum of the maximum descending number the          for(String string:a) -{//string into a character array, easy to find the sum of the numbers Wu             if(string!=NULL) -             { About                 Char[] Ch=String.tochararray (); $                 intNum=0; -                  for(inti=0;i<ch.length;i++) -                 { -num=num+ch[i]-48; A                 } +                 if(num>max2) the                 { -Max2=num;//find the sum of the maximum descending number $ss=string;//a string that records the sum of the maximum number of decrements; the                 } the             }  the              the         } -         intsum=max+Integer.parseint (ss); in System.out.println (sum);  the     } the}

Logical thinking:

1, input string and read;

2, judge whether the input is negative; (This step omitted, at the beginning of the compilation of the time added in, and then deleted)

3, the judgment decrements the number; (This step is very critical, logic to clear, I used a nested for loop, changed the wrong time to find a long)

4, the resulting decrement number is re-loaded into a set;

5, judge the maximum decline number;

6, judge the number and the maximum number of decrements.

As a recent brush the topic of beginners, this problem I have been made up to the afternoon from the morning, the problem is not so difficult, but it is in the Internet to see the other answers feel bad, and then the logic of their own rationale began to code up, although the cost of a long time, and changed the wrong one hours, but still very fulfilling. After all, it took so long, as the first post to share to everyone, hope to be useful to everyone.

Find the sum of two descending numbers of integers of int (Java)--2015 Huawei Machine Questions

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.