2017北邮机试——字符串操作

本文详细介绍了使用C++实现的一系列算法操作,包括数组元素的反转、替换、填充、排序和求和,针对输入的数据进行多种操作并输出处理结果。通过具体实例展示了不同算法的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

 

 

输入:

9 8
1 2 3 4 5 6 7 8 9
1 4 8
5 7 9
2 2 5 3
5 5 9
3 5 8 3
4 3 8
5 7 9
5 1 9

输出:

18
26
20
40

算法实现:

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int s[100];
    int n,m;
    int type,l,r,lenx;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        cin>>s[i]; 
    }
    while(m>0)
    {
        cin>>type;
        if(type==1)
        {
            cin>>l>>r;
            while(l<=r)
            {
                int a=s[l];
                s[l]=s[r];
                s[r]=a;
                l++;
                r--;
            }
    
        }
        else if(type==2)
        {
            cin>>l>>r>>lenx;
            for(int i=l;i<=(l+lenx-1);i++)
            {
                int a=s[i];
                s[i]=s[lenx+i];
                s[lenx+i]=a; 
            }
            
        
            
        }
        else if(type==3)
        {
            cin>>l>>r>>lenx;
            for(int i=l;i<=r;i++)
            {
                s[i]=lenx;
            }    
            
        
        }
        else if(type==4)
        {
            cin>>l>>r;
            sort(s+l,s+r+1);
        
        }
        else
        {
            int count=0;
            cin>>l>>r;
            for(int i=l;i<=r;i++)
            {
                count+=s[i];
            }
            

            cout<<count<<endl;
        }
        m--; 
    } 
    
    return 0;
}
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一字清华

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

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

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

打赏作者

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

抵扣说明:

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

余额充值