前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >与算法有关的习题二道

与算法有关的习题二道

作者头像
热心的社会主义接班人
发布2018-04-27 14:26:53
5760
发布2018-04-27 14:26:53
举报
文章被收录于专栏:cs

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

题一

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

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

code

代码语言:javascript
复制
#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;

}

结果

代码语言:javascript
复制
/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

题二

timu.jpg

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

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

code

代码语言:javascript
复制
#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]);

}

结果

代码语言:javascript
复制
/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
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017.12.13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题一
  • 结果
  • 题二
  • code
  • 结果
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档