Netease youdao competition's first question (Java)

Source: Internet
Author: User

Problem Statement (250)
????
Given a positive integer n. you can modify any digit in the N decimal representation to get the number in another decimal representation, the new number you get after modification must be strictly less than N (this new number can have some leading 0 ). return the maximum number after modification as required.
Definition
????
Class:
Changedigit
Method:
Change
Parameters:
Int
Returns:
Int
Method signature:
Int change (int n)
(Be sure your method is public)
????

Constraints
-
N is a positive integer between 1 and 1,000,000,000.
Examples
0)

????
123
Returns: 122
After modifying one of them, we can get the following numbers less than N: 023,103,113,120,121, and 122. The maximum number is 122.
1)

????
94040
Returns: 94030

2)

????
999999999
Returns: 999999998

3)

????
1000000000
Returns: 0
In this example, we only have one solution, replacing the first '1' with '0' to get "0000000000 ", this is a decimal value of 0 (with 9 leading zeros ).
4)

????
4321000
Returns: 4320000

 

 

 

Public class first {
Public static void main (string [] ARGs ){
First F = new first ();
System. Out. println (F. Change (999999999 ));
}

Public int change (int n ){
Int bit = 0;
Int P = 10;
Int temp = N;
While (true ){
If (TEMP % 10! = 0 ){
Return N-(P/10 );
}
Temp/= 10;
P * = 10;
}

}
}

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.