LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
楼主: wes109

[算法]Google的面试题

[复制链接]
发表于 2008-5-29 15:36:52 | 显示全部楼层
看一看后再说
回复 支持 反对

使用道具 举报

发表于 2008-6-16 21:03:50 | 显示全部楼层
累计除以10
余数或者商为1 ,++
回复 支持 反对

使用道具 举报

发表于 2008-6-24 14:00:50 | 显示全部楼层
#include <iostream>
using namespace std;

int getMaxBate(int i)
{
   int step = 1;
   int tmp = i;
   for(;(tmp/step)>=10;step =step *10);
   return step;
}
int f(int i)
{
   if ((i<10) && (i>0))
      return 1;
   else if (i<=0)
      return 0;

   int step = getMaxBate(i);
   int hcount = f(i/step);
   int lcount = f(step-1);
   int leftCount =  f(i%step);

   if ((i/step) == 1)
   {
       hcount =  (i+1)%step;
       if (hcount ==0) hcount = step;

       return hcount + (lcount *(i/step)+ leftCount);
   }
   else
   {
       return hcount*step+ (lcount *(i/step)+ leftCount);
   }
}


int oneOf(int n)
{
int result =0;
int tmp = n;

    for(;tmp!=0;)
    {
      if ((tmp %10) == 1 )
         result ++;
      tmp = tmp/10;
    }

return result;
}
int f1(int n)
{
int result = 0 ;
   for (int i=0;i<=n;i++)
   {
     result = result + oneOf(i);
   }
return result;
}

int test(int i)
{
   for(i=0;i<10000;i++)
   {
     if (f(i)!=f1(i))
     {
      cout <<"num:"<<i<<endl;
      cout <<"f1("<<i<<"):"<<f1(i)<<" !=f("<<i<<"):"<<f(i)<<endl;
      break;
     }
   }
}
int main()
{
   int i;

   test(10000);
   cout <<"please enter a  number"<<endl;
   cin >>i;
   cout <<"the result is :"<<f(i)<<endl;

   cout <<"the result is :"<<f1(i)<<endl;
   return 0;
}
回复 支持 反对

使用道具 举报

发表于 2008-6-24 14:02:21 | 显示全部楼层
#include <iostream>
using namespace std;

int getMaxBate(int i)
{
   int step = 1;
   int tmp = i;
   for(;(tmp/step)>=10;step =step *10);
   return step;
}
int f(int i)
{
   if ((i<10) && (i>0))
      return 1;
   else if (i<=0)
      return 0;

   int step = getMaxBate(i);
   int hcount = f(i/step);
   int lcount = f(step-1);
   int leftCount =  f(i%step);

   if ((i/step) == 1)
   {
       hcount =  (i+1)%step;
       if (hcount ==0) hcount = step;

       return hcount + (lcount *(i/step)+ leftCount);
   }
   else
   {
       return hcount*step+ (lcount *(i/step)+ leftCount);
   }
}


int oneOf(int n)
{
int result =0;
int tmp = n;

    for(;tmp!=0;)
    {
      if ((tmp %10) == 1 )
         result ++;
      tmp = tmp/10;
    }

return result;
}
int f1(int n)
{
int result = 0 ;
   for (int i=0;i<=n;i++)
   {
     result = result + oneOf(i);
   }
return result;
}

int main()
{
   int i;

   cout <<"please enter a  number"<<endl;
   cin >>i;
   cout <<"the result is :"<<f(i)<<endl;

   cout <<"the result is :"<<f1(i)<<endl;
   return 0;
}
回复 支持 反对

使用道具 举报

发表于 2008-6-24 14:03:40 | 显示全部楼层
两个函数效率相差很大一个是log10(n)
一个是O(n) = n*log10(n);
回复 支持 反对

使用道具 举报

发表于 2008-7-11 15:48:15 | 显示全部楼层
有点意思,我给出了一个递归算法,请参考。

#include <iostream>

using namespace std;

int countOne(int n) {
        if(n < 10)
                return 1;
        int lft = n;
        int tens = 1;
        do {
                lft = lft / 10;
                tens = tens * 10;
        } while(lft > 10);
        if(lft != 1)
                return lft * countOne(tens - 1)  + countOne(n - lft * tens) + tens;
        else
                return countOne(tens -1) + countOne(n - tens) + n - tens + 1;

}


int main()
{
        int n;
        cout << "lease input the number:";
        cin >> n;
        cout << "the number of ones is:" << countOne(n) << endl;
        return 0;
}
回复 支持 反对

使用道具 举报

发表于 2008-7-11 16:49:00 | 显示全部楼层
递归算法:
#include <iostream>

using namespace std;

int countOne(int n) {
        if(n < 10)
                return 1;
        int lft = n;
        int tens = 1;
        do {
                lft = lft / 10;
                tens = tens * 10;
        } while(lft > 10);
        if(lft != 1)
                return lft * countOne(tens - 1)  + countOne(n - lft * tens) + tens;
        else
                return countOne(tens -1) + countOne(n - tens) + n - tens + 1;

}


int main()
{
        int n;
        cout << "lease input the number:";
        cin >> n;
        cout << "the number of ones is:" << countOne(n) << endl;
        return 0;
}
回复 支持 反对

使用道具 举报

发表于 2009-4-22 17:06:21 | 显示全部楼层
int fun( int n )
{
    char str[64];
    int num = 0;
    int i = 0;
    int j = 0;          
   
    for ( i=1; i<=n; i++)
    {
        memset( str, 0x00, sizeof(str) );
        snprintf( str, sizeof(str), "%d", i );
               
        for ( j=0; j<strlen(str); j++ )
        {
            if ( '1' == str[j] )
            {
                num++;
            }              
        }
    }
   
    return num;       
}
回复 支持 反对

使用道具 举报

发表于 2009-4-22 17:08:55 | 显示全部楼层
感觉还是转成字符串后好一点,只需考虑长度,而不需考虑加减乘除问题
回复 支持 反对

使用道具 举报

发表于 2010-1-31 22:33:17 | 显示全部楼层

Java的算不算,最近写java有点上瘾了

  1. /*Consider a function which, for a given whole number n,
  2. returns the number of ones required when writing out all
  3. numbers between 0 and n. For example, f(13)=6. Notice th
  4. -at f(1)=1.
  5. What is the next largest n such that f(n)=n?  
  6. */

  7. public class Google{
  8.         public static void main(String[] args){
  9.                 for(int n=0;n<=1111111110;n++){
  10.                         String txt=Int.toString(n);
  11.                         Byte[] byt=txt.toByte();
  12.                         int result=0;
  13.                         for(int i=0;i<byt.length();i++){
  14.                                 if(byt[i]='1'){
  15.                                         result+=1;
  16.                                 }
  17.                         }
  18.                         System.out.println("When n="+n+",f(n)="+result);
  19.                 }
  20.         }
  21. }
复制代码


这台电脑没有jdk,等下javac看看,好像类型转换哪儿有点问题
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表