N为正整数,有一个函数f(N),返回0到N之间出现的数字"1"的个数。比如f(12)=5,现在f(1)=1,求100000以内的,使得f(N)=N的所有N?要求:算法简洁高效

解决方案 »

  1.   


    编程之美上的一道题
    public static int f(int n) {
    int count = 0;
    int factor = 1;
    int lowerNum, currNum, higherNum;

    while(n / factor != 0) {
    lowerNum = n % factor;
    currNum = (n/factor)%10;
    higherNum = n/(factor*10);

    switch (currNum) {
    case 0 :
    count += higherNum * factor;
    break;
    case 1 :
    count += higherNum * factor + lowerNum + 1;
    break;
    default:
    count += (higherNum + 1) * factor;
    break;
    }
    factor *= 10;
    }
    return count;
    }
      

  2.   


    谢谢2楼积极回帖
    我所要的不是f(int n)方法……
      

  3.   

    那你题出错了吧!
    f(N)=N?
    也只能是 f(1)=1
    别的没答案了吧!
      

  4.   

    int 就是整数的意思啊,楼主不是要算法吗
    三楼的算法对的啊
      

  5.   

    package testjava;
    public class Main {    public static void main(String[] args) {
            System.out.println(test(1000000));
        }
        public static int test(int n){
            int count=0;
            for(int i=1;i<=n;i++){
                String str = i+"";
                for(int j=0;j<str.length();j++){
                    if(str.charAt(j)=='1'){
                        count++;
                    }
                }
            }
            return count;
        }
    }
      

  6.   


    高效的不会,笨方法遍历,
    既然遍历,下边效率也一样,容易理解 public static void f(int n) {
    int tmp, count = 0; for(int i=1; i<=n; i++){
    tmp = i;
    while(tmp > 0) {
    if(tmp%10 == 1)
    count ++;
    tmp /= 10;
    }
    if(count == i) {
    System.out.println("f(" + i + ")=" + i + "\t");
    }
    }
    }
      

  7.   


    public class Main {    public static void main(String[] args) {
            System.out.println(getMarkNum(12121));
        }
         public static int getMarkNum(int n){
         String str=String.valueOf(n);
         int count=0;
         for (int i = 0; i < str.length(); i++) {
                char ch = str.charAt(i);
                if(ch=='1')
                count++;
            }
            return count;
        }}不知道楼主是不是这个意思?
      

  8.   


    遍历的数太少 10000000内
    f(199981)=199981
    f(199982)=199982
    f(199983)=199983
    f(199984)=199984
    f(199985)=199985
    f(199986)=199986
    f(199987)=199987
    f(199988)=199988
    f(199989)=199989
    f(199990)=199990
    f(200000)=200000
    f(200001)=200001
    f(1599981)=1599981
    f(1599982)=1599982
    f(1599983)=1599983
    f(1599984)=1599984
    f(1599985)=1599985
    f(1599986)=1599986
    f(1599987)=1599987
    f(1599988)=1599988
    f(1599989)=1599989
    f(1599990)=1599990
    f(2600000)=2600000
    f(2600001)=2600001
      

  9.   


     修改一下 呵呵 刚才忘记一个循环了  现在我想可以了!
    public class Main {    public static void main(String[] args) {
            System.out.println(getMarkNum(100000));
        }
       public static int getMarkNum(int n){
            int count=0;
            for(int i=1;i<=n; i++)
            {
             String str=String.valueOf(i);
             for (int j = 0; j < str.length(); j++) {
                char ch = str.charAt(j);
                if(ch=='1')
                count++;
            }
            }
            return count;
        }
    }
      

  10.   


    public class Test28 {
    public static void main(String[] args) {
    int count = 0;
    int sum = 0;
    for(int i = 1; i < 100000001; i++) {
    sum += fx(i);
    if(sum == i) {
    show(i);
    count++;
    }
    }
    System.out.println("100000000以内有"+count+"个数满足f(N)=N");
    }
    /*N为正整数,有一个函数f(N),返回0到N之间出现的数字"1"的个数。
      *比如f(12)=5,现在f(1)=1,求100000以内的,使得f(N)=N的所有N?
      *要求:算法简洁高效*/
    public static int f(int n) {
    return 0; // 未实现
    }
    public static int fx(int n) { //求N中有几个1
    int result = 0;
    while(n > 0) {
    if(n % 10 == 1) result++;
    n = n / 10;
    }
    return result;
    }
    public static void show(int n) {
    System.out.println("f("+n+")="+n+"\t");
    }
    }15楼说的好,满足这个要求的数在后面。我找了100000000(一亿)中的这样的数,只有40个。可以运行下我写的程序。机子一般,用不了一分钟的。