编程之美2.4 1的数目

//题目:寻找1~n中的数字1出现的次数
//解法1:检查每一个数字中1出现的次数,并相加
public class Main {  
	  
    public static void main(String[] args) throws Exception {  
    	System.out.println(findOneNum(999));
    }  
  
    public static int findOneNum(int n) throws Exception {  
    	int result = 0;
        for(int i = 1;i<=n;i++){
        	System.out.println(i);
        	result = result+findOneNumHelper(i);
        }
        return result;
    }  
    
    public static int findOneNumHelper(int num){
    	int result = 0;
    	while(num!=0){
    		if(num%10 == 1){
    			result++;
    		}
    		num = num/10;
    	}
    	return result;
    }
  
}

//解法2:分析数字的最高位和之后每一位在最高位不变情况下出现1的次数相加,并采用递归的方式检查删除第一位后的数字情况
public class Main {  
	  
    public static void main(String[] args) throws Exception {  
    	System.out.println(findOneNum(999));
    }  
  
    public static int findOneNum(int n) throws Exception {  
    	String str = String.valueOf(n);
    	return findOneNumHelper(str);
    }  
    
    public static int findOneNumHelper(String str){
    	if(str == null || str.length() == 0){
    		return 0;
    	}
    	int firstNumCount = 0;
    	int firstNum = str.charAt(0)-'0';
    	if(str.length() == 1 && firstNum >= 1){					//为了避免num = 1这种情况,会在下面的注释处出错
    		return 1;
    	}
    	if(str.length() == 1 && firstNum == 0){
    		return 0;
    	}
    	if(firstNum>1){
    		firstNumCount = (int)Math.pow(10,str.length()-1);
    	}else if(firstNum == 1){
    		firstNumCount = Integer.parseInt(str.substring(1))+1;		//会出错
    	}
    	int secondNumCount = firstNum*(str.length()-1)*(int)Math.pow(10,str.length()-2);
    	int iterNumCount = findOneNumHelper(str.substring(1));
    	return firstNumCount+secondNumCount+iterNumCount;
    }
  
}

//解法3:依次讨论每一位出现1的次数
public class Main {  
	  
    public static void main(String[] args) throws Exception {  
    	System.out.println(findOneNum(3421));
    }  
  
    public static int findOneNum(int n) throws Exception {  
    	int factor = 1;
    	int lowNum = 0;
    	int curNum = 0;
    	int highNum = 0;
    	int result = 0;
    	if(n<=0){
    		return 0;
    	}
    	while(n/factor!=0){
    		lowNum = n-n/factor*factor;		//低位数字
    		curNum = (n/factor)%10;			//当前位数字
    		highNum = n/(factor*10);		//高位数字
    		if(curNum == 0){				//当前位为0,出现1次数由高位决定
    			result+=highNum*factor;
    		}else if(curNum == 1){			//当前位为1,出现1次数由高位和低位决定
    			result+=highNum*factor+lowNum+1;
    		}else{							//当前位大于1,出现1次数由高位决定
    			result+=(highNum+1)*factor;
    		}
    		factor = factor*10;				//前移一位
    	}
    	return result;
    }  
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值