C語言面試演算法題(二)

來源:互聯網
上載者:User

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;
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.