Given a n is a positive integer a, such as a=2147483647, of which each digit Numbers in 0 ~ 9 were counting on (also called frequency), use said, then a=2147483647 count results as follows:
If given a positive integer P, for every integer (0, P] in this way against the frequency to count Numbers 0 to 9, you can get in [0, P] all number of 0 ~ 9 in total number, further can count the frequency of the occurrence, 0 ~ 9
Note: in front of the integer 0, is not included in the zero frequency, prefix 0 for integer is meaningless, namely 0123, direct written in 123,
P=10, for example, is the number of occurrence in [0, 10] 0,1,2,3,4,5,6,7,8,9,10, where 0 and 1 2 times, each 2 ~ 9 each,
Please use cycle programming and P=Integer. MAX_VALUE; (P=2147483647), within the scope of [0, P] of 0 ~ 9 each number in the number of frequency and frequency,
Requirements: temporarily need not the other structures such as arrays, direct use of a set of variables,
CodePudding user response:
Don't use an array? There are regular, can need not circulation, must use cycle, low efficiencypublic class Sample {
Public static void main (String [] args) {
Long n0=0, n1=0, n2=0, n3=0, n4 interchange=0, n5=0, n6=0, n7=0, the n8=0, the n9=0;
Double sum=1;//total occurrences
N0 + +;//statistics 0 appeared
for(int i=1; iInt j=I;
While (j> 0 {
The switch (10) j % {
Case 0: n0 + +; break;
Case 1: n1 + +; break;
Case 2: n2 + +; break;
Case 3: n3 + +; break;
Case 4: n4 interchange + +; break;
Case 5: n5 + +; break;
Case 6: n6 + +; break;
Case 7: n7 + +; break;
Case 8: n8 + +; break;
Case 9: the n9 + +; break;
}
}
J/=10;
sum++;
}
System. Out. Printf (" 0-9 in total frequency: % 0 f \ n ", sum);
System. The out. Printf (" present frequency during the % d: % d, frequency of: %. 4 f % s \ n ", 0, n0, n0 * 100/sum, "%");
System. The out. Printf (" present frequency during the % d: % d, frequency of: %. 4 f % s \ n ", 1, n1, n1 * 100/sum, "%");
System. The out. Printf (" present frequency during the % d: % d, frequency of: %. 4 f % s \ n ", 2, n2, n2 * 100/sum, "%");
System. The out. Printf (" present frequency during the % d: % d, frequency of: %. 4 f % s \ n ", 3, n3, n3 * 100/sum, "%");
System. The out. Printf (" present frequency during the % d: % d, frequency of: %. 4 f % s \ n ", 4, n4 interchange, n4 interchange * 100/sum, "%");
System. The out. Printf (" present frequency during the % d: % d, frequency of: %. 4 f % s \ n ", 5, n5, n5 * 100/sum, "%");
System. The out. Printf (" present frequency during the % d: % d, frequency of: %. 4 f % s \ n ", 6, n6, n6 * 100/sum, "%");
System. The out. Printf (" present frequency during the % d: % d, frequency of: %. 4 f % s \ n ", 7, n7, n7 * 100/sum, "%");
System. The out. Printf (" present frequency during the % d: % d, frequency of: %. 4 f % s \ n ", 8, the n8, n8 * 100/sum, "%");
System. The out. Printf (" present frequency during the % d: % d, frequency of: %. 4 f % s \ n ", 9, the n9, n9 * 100/sum, "%");
}
}