Java programming beauty of the algorithm

Source: Internet
Author: User

1. Looking for a post water king

Scene:

Tango is a pilot project for Microsoft Research Asia. The Institute's staff and interns are very fond of exchanging water on tango. Legend, Tango has a big "water king", he not only like to post, but also to reply to each other ID hair posts. Anecdotal rumors that the "Water king" posted more than half of the total number of posts. If you have a list of all the posts (including replies) on the current forum, where the ID of the post author is also in the table, can you quickly find out the legendary tango water king?

Topic:

Now there is an array, known as a number of more than half of the number, please use O (n) complexity algorithm to find this number

public class MainClass {/** * @param args */public static void main (string[] args) {String id= ""; int ntimes,i; string[] ids = new string[]{"2", "8", "8", "8", "4", "3", "8", "8", "8", "3"}; for (i=ntimes=0;i<10;i++) {if (ntimes==0) {id = Ids[i];ntimes=1;} else{if (Id.equals (Ids[i])) {ntimes++;} else{ntimes--}} } System.out.println (ID); }

Reference Note:

An array that satisfies the requirements has the following properties, assuming that the ID appears more than half of the number, then taking two different numbers from the array (whether or not it contains the "Water King" id), the number of IDs in the remaining number is more than half.

2. The number of 1

Given a decimal positive integer n, write down all integers starting with 1, to N, and then count all the "1" that appear.
For example:
n=2, write down 1, 2. There were only 1 "1".
N=12, we'll write down 1,2,3,4,5,6,7,8,9,10,11,12. In this way, the number of 1 is 5.
The problem is:
1. Write a function f (n) that returns the number of "1" that appears between 1 and N, such as f (12) = 5.
2. The maximum number of N that satisfies the condition "f (N) =n" within the range of 32-bit integers.

Package com; Import java.lang.reflect.InvocationTargetException; Import Java.math.BigDecimal; /** * 1 times * @author Midlee/public class Number1count {public static void main (string[] args) {System.out.println (fi Ndnum (123)); public static integer FindNum (integer n) {integer count = 0; Integer factor = 1; Integer lownum = 0; Integer curnum = 0; Integer highnum = 0; while (n/factor!=0) {lownum = n (n/factor) *factor; curnum = (n/factor)%10; highnum = n/(factor*10); switch (curnum) {case 0: Count + = Highnum * Factor;break; Case 1:count + + Highnum * factor + lownum+1;break; Default:count + + (Highnum + 1) * factor;break; } factor *=10; return count; } }

Reference Note:

Let's look at the 1-digit number first.

If n=3, then all numbers from 1 to 3:1, 2, 3, only single-digit digits may appear 1, and only 1 times, further can be found if N is Single-digit, if n>=1, then f (n) is equal to 1, if n=0, then f (n) is 0.

Let's look at the 2-digit number.

If n=13, then all numbers from 1 to 13:1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, single-digit and 10 digits may have 1, we can take them apart, a single-digit 1 times two: 1 and 11, 10 digits appear 1 times 4:10, 11, 12 and 13, so F (N) =2+4=6. Note that 11 of this number in the 10-bit and single-digit appear 1, but 11 is exactly in the Single-digit 1 and 10 in 1 is calculated two times, so no special treatment, is right. Consider the situation of n=23, it and n=13 a bit different, 10-bit 1 times 10 times, from 10 to 19, Single-digit 1 of the number 1, 11, 21, so F (N) =3+10=13. Through the analysis of the two digits, we found that 1 of the number of digits is not only related to single-digit digits, also related to the 10-digit number: If n's single-digit number is greater than or equal to 1, the number of digits appearing at 1 is 10 digits plus 1; if n is single-digit 0, a digit of 1 is equal to 10 digits in number. and the number of 1 on the 10-digit number is not only related to the 10-digit number, also related to single-digit digits: If the 10 digits equals 1, the number of 1 on the 10-digit number is single-digit plus 1, or 10 if the 1-digit number is greater than 10.

For example:

F (13) = Single-digit number of 1 and 10 digits appearing 1 =2+4=6;

F (23) = Single-digit Number of 1 and 10 digits appearing 1 =3+10=13;

F (33) = Single-digit Number of 1 and 10 digits appearing 1 =4+10=14;

......

F (93) = Single-digit number of 1 and 10 digits appearing 1 =10+10=20;

Then analyze the 3-digit number.

If n=123:

The number of digits appearing at 1 is 13:1,11,21,...,91,101,111,121

The number of 10-bit occurrences of 1 is 20:10~19,110~119

The number of hundreds appearing 1 is 24:100~123

F (123) = the number of 1 in Single-digit + 10 bits appearing 1 and the number of hundreds appearing 1 times =13+20+24=57;

Similarly, we can analyze the 4-digit, 5-digit number again. Readers can write a summary of what is different.

Based on some of the previous attempts, we derive a general calculation of F (n) from N:

Suppose N=abcde, where a, B, C, D, e are the digits of the decimal number n. If you want to calculate the number of 1 on the Hundred, it will be affected by three factors: the number on the hundred, the number below the Hundred (low), and the number above the Hundred (higher).

If the number on the hundred is 0, you can know that the number of hundreds of 1 can be determined by a higher level, such as 12013, you can know that the hundreds of 1 may be 100~199,1 100~1 199,2 100~2 199,...,11 100~11 199, a total of 1200. That is, it is determined by a higher number (12) and equals a higher number (12) * The current number of digits (100).

If the number on the hundred is 1, it can be seen that the number of 1 on the hundred is not only affected by the higher level, but also by the low impact, that is, the higher and lower the common decision. For example, for 12113, with a higher impact, the number of hundred 1 is 100~199,1 100~1 199,2 100~2 199,...,11 100~11 199, a total of 1200, as in the first case, equal to higher digits (12) * Current digits (100). It is also affected by the low, a 1 of the situation is 12 100~12 113, a total of 114, equal to the low number (113) +1.

If the number on the hundred is greater than 1 (that is, 2~9), the number of 1 that may occur on the hundred is determined only by a higher order, such as 12 213, the probability of a hundred 1 is 100~199,1 100~1 199,2 100~2 199,...,11 100~11 199,12 100~12 199, a total of 1300, and equal to the higher number +1 (12+1) * Current digits (100).

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.