为了账号安全,请及时绑定邮箱和手机立即绑定

与算法有关的习题二道(猿问上回答的)

标签:
C C++ 数据结构

在猿问上回答了几道题,其中二题还不错,记录一下

题一

要求输入一串不是很长的字符串,在最大的字符后加(max),字符串没有空格,只在第一个出现最大的字符后加(max)。
例如
输入 a b z d
输出 a b z(max) d

思路,
1.0 首先把字符串变成字符数组,
2.0 在找出最大字符串位置,
3.0 最后添加(max),把字符数组变成字符串

code

#include <iostream>
#include<string>
using namespace std;
void display(char ch[],int n);
int locate(char ch[],int n);
int main() {

    string str;
    std::cout << "put into string" << std::endl;
    cin>>str;
    //测量字符串数组长度
    int len=str.length();
    cout<<"leng of string:"<<len;

    char *p; //动态数组分配
    p=new char[len];
 //字符串变成字符数组
    for(int i=0; i<len; i++)
    {
         p[i]=str[i];

    }

    cout<<endl;

    display(p,len);
    cout<<endl;

    int loc=locate(p,len);
    cout<<"最大位置:"<<loc;

    cout<<endl;

  char *p1=new char[len+5];

    for(int i=0; i<loc+1; i++)
    {
        p1[i]=p[i];

    }

   p1[loc+1]='('; p1[loc+2]='m';
    p1[loc+3]='a'; p1[loc+4]='x'; p1[loc+5]=')';

    for(int i=loc+6; i<len+5; i++)
    {
        p1[i]=p[i-5];
    }

//字符数组变成字符串
    str=p1;
    cout<<endl;
    cout<<" 合并为:"<<str;
    return 0;
}

//打印字符数组
void display(char ch[],int n)
{
    for(int i=0; i<n; i++)
        cout<<ch[i]<<" ";
}

//找到最大字符位置
int locate(char ch[],int n)
{
    char max=0;
    for(int i=1; i<n; i++)
    {
        if(ch[max]<ch[i])
            max=i;
    }

    return  max;

}

结果

/home/dfzxk/CLionProjects/untitled3/cmake-build-debug/untitled3
put into string
zsdfhyuztryu
leng of string:12
z s d f h y u z t r y u 
最大位置:0

 合并为:z(max)sdfhyuztryu
Process finished with exit code 0

<hr>

题二

timu.jpg

哪位提问者给的是倒着的。。。。。

思路,申请二个数组存放奇数数组和偶数数组,主要设计数组动态分配问题

code

#include <stdio.h>
#include "stdlib.h"
const  int N=10;
void  display(int arr[],int n);
int main() {

    int arr[N];
    int i,*p;
    p=arr;

    printf("请输入任意10个整数");
    for(i=0; i<N; i++)
        scanf("%d",p+i);
/*
  for(i=1; i<11; i++)
      arr[i-1]=i;
   */

    display(arr,10);
printf("\n");
    int count1=0;
    p=arr;
    for( i=0; i<N; i++)
    {
        if(*(p+i)%2==0)
            count1++;
    }

  //  printf("count1=%d\n",count1);

    int *arr1,num1,j;
    arr1=(int *)malloc(sizeof(int)*count1);
    p=arr;
    num1=0;
    for( j=0; j<N; j++)
    {
        if(*(p+j)%2==0)
        {
            arr1[num1]=arr[j];
            num1++;
        }
    }
printf("偶数数组:");
   display(arr1,count1);
 printf("\n");

    int *arr2,k;
    arr2=(int *)malloc(sizeof(int)*(N-count1));
    p=arr;
    num1=0;
    for(k=0;k<N;k++)
    {
        if(*(p+k)%2!=0)
        {
            arr2[num1] = arr[k];
            num1++;

        }
    }

    printf("奇数数组:");
    display(arr2,N-count1);
    printf("\n");
    return 0;
}

void  display(int arr[], int n)
{
  int i;
  for(i=0; i<n; i++)
 printf("%5d",arr[i]);

}

结果

/home/dfzxk/CLionProjects/untitled1/cmake-build-debug/untitled1
请输入任意10个整数89
45
86
21
33
58
121
100
99
77
   89   45   86   21   33   58  121  100   99   77
偶数数组:   86   58  100
奇数数组:   89   45   21   33  121   99   77
熟悉慕课,以后到这里玩了。
点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
全栈工程师
手记
粉丝
73
获赞与收藏
369

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消