Chinaunix首页 | 论坛 | 博客
  • 博客访问: 59113
  • 博文数量: 10
  • 博客积分: 214
  • 博客等级: 入伍新兵
  • 技术积分: 120
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-19 19:09
文章分类

全部博文(10)

文章存档

2012年(10)

我的朋友

分类:

2012-11-02 21:14:28

原文地址:C语言面试算法题(二) 作者:frankzfz

1.写一个函数,它的原形是int continumax(char *outputstr,char *intputstr)
功能:
   在字符串中找出连续最长的数字串,并把这个串的长度返回,并把这个最长数字串付给其中一个函数参数outputstr所指内存。例如:"abcd12345ed125ss123456789"的首地址传给intputstr后,函数将返回
9,outputstr所指的值为123456789。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int FindMax_NumStr(char *outputstr,char *inputstr)
{
  char *in = inputstr,*out = outputstr,*temp;
  char *final;
  int count = 0;
  int maxlen = 0;
  int i;
  while(*in!='\0')
  {
    if(*in > 47 && *in < 58)
    {
      for(temp = in;*in> 47 && *in <58;in++)
        count++;
    }
    else
      in++;
    if(maxlen < count)
    {
      maxlen = count;
      count = 0;
      final = temp;
    }
  }

  for(i =0;i<maxlen;i++)
  {
    *out = *final;
    out++;
    final++;
  }
  *out = '\0';
  return maxlen;
}

void main(void)
{
  char input[]="abc123def123456eec123456789dd";
  char output[50] = {0};
  int maxlen;
  maxlen = FindMax_NumStr(output,input);
  printf("the str %s\n",output);
  printf("the maxlen is %d\n",maxlen);
}

2.求1000!的未尾有几个0;
   求出1->1000里,能被5整除的数的个数n1,能被25整除的数的个数n2,能被125整除的数的个数n3,能被625整除的数的个数n4.1000!末尾的零的个数=n1+n2+n3+n4;
   只要是末尾是5的数它乘以一个偶数就会出现一个0,而末尾是0的数乘以任何数也都会出现0
而末尾是0的如果是一个0肯定能被5整除,两个0肯定能被25整数,以此类推3个0就能被5的三次方整除,也就是125
   1000!就是1-1000数的相乘,能被5整除的所有数分别乘以一个偶数就会出现这些个的0,而例如100,既能被5整除,也能被25整除,所以就是两个0
   1000,既能被5,25,也能被125整除,所以算三个0
    例如是10!=1*2*3*4*5*6*7*8*9*10,里面有两个数能被5整除,就是10和5,而
5随便乘以一个偶数就出现一个0,而10乘以其它数也会出现一个0,所以10!会有两个0

#include <stdio.h>
#define NUM 1000

int find5(int num)
{
  int ret = 0;
  while(num%5==0)
  {
    num/=5;
    ret++;
  }
  return ret;
}

int main(void)
{
  int result = 0;
  int i;
  for(i=5;i<=NUM;i+=5)
    result +=find5(i);
  printf("the total zero number is %d\n",result);
  return 0;
}

3。编写一个 C 函数,该函数在一个字符串中找到可能的最长的子字符串,且该字符串是由同一字符组成的。

char * search(char *cpSource, char ch)
{
         char *cpTemp=NULL, *cpDest=NULL;
         int iTemp, iCount=0;
         while(*cpSource)
         {
                 if(*cpSource == ch)
                 {
                          iTemp = 0;
                          cpTemp = cpSource;
                          while(*cpSource == ch)
++iTemp, ++cpSource;
                          if(iTemp > iCount)
iCount = iTemp, cpDest = cpTemp;
        if(!*cpSource)
break;
                 }
                 ++cpSource;
}
return cpDest;
}


阅读(1729) | 评论(0) | 转发(0) |
0

上一篇:ARM的流水线与PC值的关系

下一篇:2012-12-27

给主人留下些什么吧!~~