编程之美2.4 1的数目

来源:互联网 发布:编辑部的故事 知乎 编辑:程序博客网 时间:2024/05/16 19:49
//题目:寻找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;    }      }

原创粉丝点击