一道google的面试题。

wifewifewife 2009-10-26 11:30:52
问题:编写一个程序,输入一个 n, 输出从1到这个数字之间的出现的1的个数,比如f(13)等于6; f(9)等于1;
编写一个程序,得出最先使得 f(n)等于n的整数n;
下面是我自己编写的程序,不知道哪位有没有更好,更高效的方法。。
public static void main(String[] args)
{
long n = 200000;
int count = 1;

for (int i = 1; i <= n; i++)
{
int l = (int) (Math.log10(i) + 1);//n的位数
long s = (long) Math.pow(10, l - 1);//10的n次幂
for (int j = 1; j <= l - 1; j++)
{
if (i % s == 1)
{
count++;
}
}
for (int k = 1; k <= l - 1; k++)
{
if (i / s == 1)
{
count++;
}
}
if (count == i)
{
System.out.println("count = " + count + ", number = " + i);
}
}
答案是:
count = 1, number = 1
count = 176, number = 176
count = 224, number = 224
count = 1378, number = 1378
count = 3247, number = 3247
count = 43281, number = 43281
...全文
518 37 打赏 收藏 转发到动态 举报
写回复
用AI写文章
37 条回复
切换为时间正序
请发表友善的回复…
发表回复
jazzbond 2009-10-28
  • 打赏
  • 举报
回复
jeopaine 2009-10-28
  • 打赏
  • 举报
回复
不懂,什么意思呀
什么鬼哈哈 2009-10-28
  • 打赏
  • 举报
回复
public class ContainNumber {

/**
* @param args
*/
public static void main(String[] args) {
ContainNumber number = new ContainNumber();
System.out.println(number.search(1909090));
}


public int search(int _n)
{
int N = _n/10;
int a1 = _n%10,a2;
int x = 1;
int ten = 10;
int c = a1 == 0?0:1;
while(N > 0)
{
a2 = N%10;
if(a2 == 0);
else if(a2 == 1)c = a1 + 1 + x + c;
else c = a2*x + c + ten;
a1 = 10*a1 + a2;
N /=10;
x = 10*x + ten;
ten *= 10;
}
return c;
}
}


时间复杂度O(lgn)
wifewifewife 2009-10-28
  • 打赏
  • 举报
回复
[Quote=引用 29 楼 flfna 的回复:]
package project;
public class Count1Num{
public static void main(String[] args)
{
int total = 0;
for (long i = 1; i < 2000000000; i++) {
    long n = i;
    while (n > 0) {
        if (n % 10 == 1)
            total++;
        n /= 10;
    }
    if (i == total)
        System.out.println("i = " + i + ", total = " + total);
}
    System.out.println(total);
}
}

i < 2000000000 你的电脑性能算不!
[/Quote]
这个可以用O(n * ..)算出来的呀.在同一台电脑上分别测试的话基本没什么影响.
wifewifewife 2009-10-28
  • 打赏
  • 举报
回复
这个可以用O(n * ..)算出来的呀.在同一台电脑上分别测试的话基本没什么影响.
Jack_Skipper 2009-10-28
  • 打赏
  • 举报
回复
package project;
public class Count1Num{
public static void main(String[] args)
{
int total = 0;
for (long i = 1; i < 2000000000; i++) {
long n = i;
while (n > 0) {
if (n % 10 == 1)
total++;
n /= 10;
}
if (i == total)
System.out.println("i = " + i + ", total = " + total);
}
System.out.println(total);
}
}

i < 2000000000 你的电脑性能算不!
wifewifewife 2009-10-28
  • 打赏
  • 举报
回复
[Quote=引用 26 楼 tfsict2008 的回复:]
微软面试题,编程之美上面有讲
[/Quote]
刚看了一下这本书,书上的代码如下:

int n = 2000000000;
int iCount = 0;
int iFactor = 1;
int iLowerNum = 0;
int iCurrNum = 0;
int iHigherNum = 0;
while (n / iFactor != 0) {
iLowerNum = n - (n / iFactor) * iFactor;
iCurrNum = (n / iFactor) % 10;
iHigherNum = n / (iFactor * 10);
switch (iCurrNum) {
case 0:
iCount += iHigherNum * iFactor;
case 1:
iCount += iHigherNum * iFactor + iLowerNum + 1;
break;
default:
iCount +=(iHigherNum+1)*iFactor;
break;
}
iFactor *=10;
}
System.out.println("count = " + iCount);

运行效率比7楼和19楼的要快40000到50000倍.
不知道大家还有没有更优,更快的算法.
qjcslgnhwayagain 2009-10-28
  • 打赏
  • 举报
回复
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Num1
{
public static void main(String []args)
{
System.out.println("请输入数字N:");
try
{
BufferedReader bufread=new BufferedReader(new InputStreamReader(System.in));
String input=bufread.readLine();
bufread.close();
int result=Integer.parseInt(input);
StringBuffer sf=new StringBuffer();
for(int i=1;i<=result;i++)
{
sf.append(i);
}
int num1=0;
for(int i=0;i<sf.length();i++)
{
if(sf.charAt(i)=='1')
num1++;
}
System.out.println("the results is:"+num1);

}
catch(IOException e)
{
e.printStackTrace();
}
}

}
tfsict2008 2009-10-28
  • 打赏
  • 举报
回复
微软面试题,编程之美上面有讲
zhuzeitou 2009-10-28
  • 打赏
  • 举报
回复
恩,7楼的是从1开始算上去的,这个类似于直接套公式………………
但如果要找到某区间内最小的一个满足条件值或者直接全部输出的话,这个方法似乎也并不一定高效啊
lz12366007 2009-10-28
  • 打赏
  • 举报
回复
[Quote=引用 32 楼 wt_yao_monkey 的回复:]
public class ContainNumber {

/**
* @param args
*/
public static void main(String[] args) {
ContainNumber number = new ContainNumber();
System.out.println(number.search(1909090));
}


public int search(int _n)     
{     
    int N = _n/10;     
    int a1 = _n%10,a2;     
    int x = 1; 
    int ten = 10; 
    int c = a1 == 0?0:1; 
    while(N > 0)     
    {     
        a2 = N%10; 
        if(a2 == 0); 
        else if(a2 == 1)c = a1 + 1 + x + c;     
        else c = a2*x + c + ten;     
        a1 = 10*a1 + a2;       
        N /=10;     
        x = 10*x + ten; 
        ten *= 10;
    }     
    return c;     
}
}


时间复杂度O(lgn)
[/Quote]

很强的算法。。。。没有遍历所有的数据。。。
wifewifewife 2009-10-28
  • 打赏
  • 举报
回复
[Quote=引用 32 楼 wt_yao_monkey 的回复:]
public class ContainNumber {

/**
* @param args
*/
public static void main(String[] args) {
ContainNumber number = new ContainNumber();
System.out.println(number.search(1909090));
}


public int search(int _n)
{
    int N = _n/10;
    int a1 = _n%10,a2;
    int x = 1;
    int ten = 10;
    int c = a1 == 0?0:1;
    while(N > 0)
    {
        a2 = N%10;
        if(a2 == 0);
        else if(a2 == 1)c = a1 + 1 + x + c;
        else c = a2*x + c + ten;
        a1 = 10*a1 + a2;
        N /=10;
        x = 10*x + ten;
        ten *= 10;
    }
    return c;
}
}


时间复杂度O(lgn)
[/Quote]
是的,这是方法避开了从1到n的遍历,是非常高效的算法.比7楼的算法要快上40000多倍.
zhuzeitou 2009-10-27
  • 打赏
  • 举报
回复
只输出一个数那么就在输出后break就好了啊
py330316117 2009-10-27
  • 打赏
  • 举报
回复
题没看懂,按lz题的意思应该是只输出一个数啊
义布寿水 2009-10-27
  • 打赏
  • 举报
回复
呵呵,提没读懂,这个函数关系没搞清
zhuzeitou 2009-10-27
  • 打赏
  • 举报
回复
int total = 0;
for (int i = 1; i < 200000; i++) {
int n = i;
while (n > 0) {
if (n % 10 == 1)
total++;
n /= 10;
}
if (i == total)
System.out.println("i = " + i + ", total = " + total);
}


输出结果:
i = 1, total = 1
i = 199981, total = 199981
i = 199982, total = 199982
i = 199983, total = 199983
i = 199984, total = 199984
i = 199985, total = 199985
i = 199986, total = 199986
i = 199987, total = 199987
i = 199988, total = 199988
i = 199989, total = 199989
i = 199990, total = 199990

lz的程序貌似有问题啊………………
弘石 2009-10-27
  • 打赏
  • 举报
回复
首先需要总结4楼所说的公式,其次要证明在n之前没有一个数使f(x)=x
vvflying 2009-10-27
  • 打赏
  • 举报
回复
不懂
r4141496091 2009-10-27
  • 打赏
  • 举报
回复
你错了,1-10只有2个1,100只有20个1,200只有140个1左右,哪里176有176个1,难道176出现1的次数还比200出现的1的次数多,你这样找一下吧,很有规律的
wifewifewife 2009-10-27
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 r4141496091 的回复:]
你错了,1-10只有2个1,100只有20个1,200只有140个1左右,哪里176有176个1,难道176出现1的次数还比200出现的1的次数多,你这样找一下吧,很有规律的
[/Quote]
我写的程序有问题,但你也说错了,1到100已经有21个1了。接下来更大的数就不用再说了。
加载更多回复(17)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧