本帖最后由 hdsjsql 于 2014-2-25 22:12 编辑
写代码不容易,求技术分
- public class Count {
- public static void main(String[] args) {
- long before = System.currentTimeMillis();
- long count = getSum();
- long after = System.currentTimeMillis();
- System.out.println("1到400亿中1的个数为:"+count);
- // System.out.println(count+"==");
- System.out.println("time:"+(after-before));
-
- }
-
- public static long getSum(){
- long sum = 0;
- long c;
- for(int i=1;i<=11;i++){
- c = test(i);
- System.out.println("当400亿中出现"+i+"个位置是1时");
- System.out.println("1的个数为:"+c);
- System.out.println();
- sum+=c;
- }
-
- return sum;
- }
-
- public static long test(int x){
- int t,w;
- long y=1,z=1,c=1,d=1;
- long s1=1,s2=1;
- long sum1,sum2;
-
- System.out.println("当400亿中出现"+x+"个位置是1时");
- System.out.println("最高位不为1");
-
-
- for(int i=1,j=0;i<=x;i++,j++){
- y*=10-j;
- }
- t=x;
- while(t>0){
- c*=t;
- t--;
- }
-
- s1=y/c;
- System.out.println("s1:"+s1);
- sum1=s1*3;//最高位只能取0,2,3
- for(int i=0;i<10-x;i++){
- sum1 *=9;
- }
- System.out.println("1的个数为:"+sum1);
-
-
- System.out.println("最高位为1");
- for(int i=1,j=0;i<=x-1;i++,j++){
- z*=10-j;
- }
- w=x-1;
- while(w>0){
- d*=w;
- w--;
- }
- s2=z/d;
- System.out.println("s2:"+s2);
- sum2=s2;
- for(int i=0;i<10-x+1;i++){
- sum2 *=9;
- }
- System.out.println("1的个数为:"+sum2);
-
- return (sum1+sum2)*x;
- }
- }
复制代码
|
|