美文网首页C语言C++程序员
与算法有关的习题二道

与算法有关的习题二道

作者: 知识学者 | 来源:发表于2017-12-13 22:58 被阅读64次

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

题一

要求输入一串不是很长的字符串,在最大的字符后加(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


题二

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

相关文章

  • 与算法有关的习题二道

    在猿问上回答了几道题,其中二题还不错,记录一下 题一 要求输入一串不是很长的字符串,在最大的字符后加(max),字...

  • 练习题可以这样上

    练习课。 课前研究学习单,让孩子选择两道与所学知识有关的好题,并解答。 数学练习课老师提供练习题,让学生按照...

  • 《算法竞赛入门经典第二版》习题2-5分数化小数问题

    这是大三阅读《算法竞赛入门经典第二版》时写的,当时是为了准备算法题:关于《算法竞赛入门经典第二版》习题2-5分数化...

  • 第一章 绪论

    本系列读书笔记参考自数据结构与算法经典问题解析第二版内容,习题答案部分有误,已勘正,部分严格证明参考算法导论第三版...

  • 算法习题

    4 递归与分治 选择问题 例4.9 查找第k个小/大元素 n个元素,元素划分n/5(不带余数),每组五个元素,不足...

  • 2018-11-11 算法练习题

    下面是几道算法练习题:

  • 程序设计练习题

    算法竞赛入门经典 习题1-1平均数 习题1-2温度 习题1-3 习题1-4 习题1-5 习题1-6 习题1-7 习...

  • 数据结构与算法分析_java语言描述_Mark_Allen_We

    下载地址:数据结构与算法分析_java语言描述_Mark_Allen_Weiss著_课后习题答案.pdf

  • 7月份之前的技术学习与计划

    读书计划 基础方面 算法:《算法》,《算法导论》,需要认真理解算法,并独立完成相关习题 语言:《JAVA编程思想》...

  • Pandas 100道练习题(二)

    Pandas 100道练习题(二) 题目逐渐变难(态) DataFrames: beyond the basics...

网友评论

    本文标题:与算法有关的习题二道

    本文链接:https://www.haomeiwen.com/subject/cictwxtx.html