C/C++基礎整理(3)

1:寫一個函數,它的原形是int continumax(char *outputstr,char *intputstr)
功能:
在字符串中找出連續最長的數字串,並把這個串的長度返回,並把這個最長數字串付給其中一個函數參數outputstr所指內存。
例如:”abcd12345ed125ss123456789”的首地址傳給intputstr後,函數返回9,outputstr所指的值爲123456789

int continumax(char *outputstr, char *inputstr)
{
   char *in = inputstr, *out = outputstr, *temp, *final;
   int count = 0, maxlen = 0;

   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(int i = 0; i < maxlen; i++)
  {
    *out = *final;
    out++;
    final++;
  }
  *out = '\0';
  return maxlen;
}

2、用指針的方法,將字符串“ABCD1234efgh”前後對調顯示

#include <stdio.h>
#include <string.h>
#include <dos.h>
int main()
{
    char str[] = "ABCD1234efgh";
    int length = strlen(str);
    char * p1 = str;
    char * p2 = str + length - 1;
    while(p1 < p2)
    {
        char c = *p1;
        *p1 = *p2;
        *p2 = c;
        ++p1;
        --p2;
    }
    printf("str now is %s\n",str);
    system("pause");
    return 0;
}

3、有一分數序列:1/2,1/4,1/6,1/8……,用函數調用的方法,求此數列前20項的和

#include <stdio.h>
double getValue()
{
    double result = 0;
    int i = 2;
    while(i < 42)
    {
        result += 1.0 / i;//一定要使用1.0做除數,不能用1,否則結果將自動轉化成整數,即0.000000
        i += 2;
    }
    return result;
}
int main()
{
    printf("result is %f\n", getValue());
    system("pause");
    return 0;
}

4:有一個數組a[1000]存放0–1000;要求每隔二個數刪掉一個數,到末尾時循環至開頭繼續進行,求最後一個被刪掉的數的原始下標位置。
以7個數爲例:
{0,1,2,3,4,5,6,7} 0–>1–>2(刪除)–>3–>4–>5(刪除)–>6–>7–>0(刪除),如此循環直到最後一個數被刪除。

#include<iostream>
using namespace std;
#define null 0
struct node
{
   int data;
   node* next;
};
int main()
{
      node* head=new node;
      head->data=0;
      head->next=null;
      node* p=head;
      for(int i=1;i<1000;i++)
      {
          node* tmp=new node;
          tmp->data=i;
          tmp->next=null;
          head->next=tmp;
          head=head->next;
     }
     head->next=p;
     while(p!=p->next)
     {
        p->next->next=p->next->next->next;
        p=p->next->next;
     }
     cout<<p->data;
     return 0;
}

5:

void test2() 
{ 
   char string[10], str1[10]; 
   int i; 
   for(i=0; i<10; i++) 
   { 
      str1[i] = 'a'; 
   } 
   strcpy( string, str1 ); 
} 

解答: str1不能在數組內結束:因爲str1的存儲爲:{a,a,a,a,a,a,a,a,a,a},沒有’\0’(字符串結束符),所以不能結束
strcpy( char *s1,char *s2)他的工作原理是,掃描s2指向的內存,逐個字符付到s1所指向的內存,直到碰到’\0’,因爲str1結尾沒有’\0’,所以具有不確定性,不知道他後面還會有什麼。
正確應如下

void test2() 
{ 
   char string[10], str1[10]; 
   int i; 
   for(i=0; i<9; i++) 
   { 
      str1[i] = 'a'+i; //把abcdefghi賦值給字符數組
   } 
   str[i]='\0';//加上結束符
   strcpy( string, str1 ); 
}

6: 寫出程序把一個鏈表中的接點順序倒排

typedef struct linknode
{
       int data;
       struct linknode *next;
}node;
//將一個鏈表逆置
node *reverse(node *head)
{
      node *p,*q,*r;
      p=head;
      q=p->next;
      while(q!=NULL)
      {
         r=q->next;
         q->next=p;
         p=q;
         q=r;
     }

      head->next=NULL;
      head=p;
      return head;
}

7:寫出程序刪除鏈表中的所有接點

void del_all(node *head)
{
     node *p;
     while(head!=NULL)
     {
        p=head->next;
        free(head);
        head=p;
     }
     cout<<"釋放空間成功!"<<endl;
}

8:兩個字符串,s,t;把t字符串插入到s字符串中,s字符串有足夠的空間存放t字符串

void insert(char *s, char *t, int i)
{
      char *q = t;
      char *p =s;
      if(q == NULL)return;
      while(*p!='\0')
      {
         p++;
      }
      while(*q!=0)
      {
        *p=*q;
        p++;
        q++;
      }
    *p = '\0';
}

9:斐波拉契數列遞歸實現的方法如下:

int  Funct( int n )
{
   if(n==0) return 1;
   if(n==1) return 1;
   retrurn  Funct(n-1) + Funct(n-2);
}

10:兩個字符串,s,t;把t字符串插入到s字符串中,s字符串有足夠的空間存放t字符串

void insert(char *s, char *t, int i)
{
   memcpy(&s[strlen(t)+i],&s[i],strlen(s)-i);
   memcpy(&s[i],t,strlen(t));
   s[strlen(s)+strlen(t)]='\0';
}

11:請編寫一個 C 函數,該函數在給定的內存區域搜索給定的字符,並返回該字符所在位置索引值。

int search(char *cpSource, int n, char ch)
{
         int i;
         for(i=0; i<n && *(cpSource+i) != ch; ++i);
         return i;
}

12:找出兩個字符串中最大公共子字符串,如”abccade”,”dgcadde”的最大子串爲”cad”

int GetCommon(char *s1, char *s2, char **r1, char **r2)
{
       int len1 = strlen(s1);
       int len2 = strlen(s2);
       int maxlen = 0;

     for(int i = 0; i < len1; i++)
     {
         for(int j = 0; j < len2; j++)
         {
            if(s1[i] == s2[j])
            {
               int as = i, bs = j, count = 1;
               while(as + 1 < len1 && bs + 1 < len2 &&   s1[++as] == s2[++bs])
              count++;

           if(count > maxlen)
           {
              maxlen = count;
              *r1 = s1 + i;
              *r2 = s2 + j;
           }
         }
    }
}

13:怎麼判斷鏈表中是否有環?

bool CircleInList(Link* pHead)
{
      if(pHead = = NULL || pHead->next = = NULL)//無節點或只有一個節點並且無自環
      return (false);
      if(pHead->next = = pHead)//自環
      return (true);
      Link *pTemp1 = pHead;//step 1
      Link *pTemp = pHead->next;//step 2
      while(pTemp != pTemp1 && pTemp != NULL && pTemp->next != NULL)
      {
          pTemp1 = pTemp1->next;
          pTemp = pTemp->next->next;
      }
      if(pTemp = = pTemp1)
        return (true);
        return (false);
}
發佈了40 篇原創文章 · 獲贊 5 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章