Github:https://github.com/frank-cq/mytest
Question 30th: Enter an integer n, from 1 to N, the decimal representation of the n integer 1 occurrences. For example, enter 12, from 1 to 12 these integers contain 1 of the numbers have 1,10,11 and 12, 11 have appeared 5 times.
Analysis
This is a few numbers to look at and you will find similar permutations of numbers, such as the number 2016, which counts from right to left:
- When the digit is 1 o'clock, the left side is 0~201, that is, there are 202 combinations of methods;
- When 10 bit is 1 o'clock, the left is 0~20, and the right at this time to pay attention is not only take 0~6, should be divided into two cases, when the left to take 0~19, the right can take to 0~9, a combination can take 20*10 way; When the left to 20 o'clock, the right can only take 0~6 7 ways, Then, when the 10-bit is 1 o'clock, 207 combinations can be taken.
- When the Hundred is 1 o'clock, the left obviously can only take 0~1, the right can take 0~99, a combination of 2*100 way;
- When the thousand is 1 o'clock to the right can be taken to 0~999, a total of 1000 ways.
In addition, there are 1609 combinations of four cases. From the above analysis can be introduced a calculation formula, take each bit in three kinds of circumstances to judge.
Code
Packagetest030;/** * Created by CQ on 2015/7/4. * Question 30th: Enter an integer n, from 1 to n the decimal representation of the n integer 1 occurrences of the number of occurrences. For example, enter 12, * from 1 to 12 these integers contain 1 of the numbers have 1,10,11 and 12, 11 have appeared 5 times. */ Public class Test030 { //Enumeration method Public Static int Getnumofoneenum(intN) {intCount =0; for(intI=1; i<=n; i++) { for(intJ=i; J>0; J/=Ten){if(j%Ten==1) {count++; } } }returnCount }//Similar to the method of arranging combinations, add each bit as a possible combination of 1 o'clock other bits Public Static int Getnumofone(intN) {if(N <1){return 0; } String tmp = Integer.tostring (n);intdigits = Tmp.length (), Count =0, pre =0, Suf =0, Curdigit =0, TMP1 =Ten, TMP2 =1; for(intI=1; i<=digits; i++) {//Current bit before partPre = N/TMP1;//Current bit after sectionSuf = N%TMP2;//Current bitCurdigit = N%TMP1/TMP2;the number of bits before the current bit *10^ part of the current bit if(Curdigit = =0) {count + = PRE*TMP2; }the number of bits after the current bit *10^ the current bit + the current bit after part +1 Else if(Curdigit = =1) {count + = PRE*TMP2 +suf+1; }//(before the current bit + 1) *10^ the number of bits after the current bit Else{count + = (pre+1) *TMP2; } TMP2 = TMP1; TMP1 *=Ten; }returnCount } Public Static void Main(string[] args) {LongStartTime = System.nanotime (); System.out.println ("Enumeration method Run Result:"+getnumofoneenum ( .));LongEndTime = System.nanotime (); System.out.println (The enumeration method executes the+ (Endtime-starttime) +"NS"); StartTime = System.nanotime (); System.out.println ("group Legit Run Result:"+getnumofone ( .)); EndTime = System.nanotime (); System.out.println ("The combination method was executed"+ (Endtime-starttime) +"NS"); }}
Execution results
tothe‘127.0.0.1:25621‘‘socket‘fromthe‘127.0.0.1:25621‘‘socket‘枚举法运行结果:1609730781 ns组合法运行结果:160974105with0
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Question 30th: Number of occurrences of 1 in integers from 1 to n