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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员的资料库

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值