99_颤抖吧!(string切片)(string int 相互转换)

 

 

题目特点:输入几个数字,个数不确定。输入不同需求不同

涉及知识:<sstream>(get到了新知识)   ,string 与 int 转换    string依据空格切片

代码:

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
void display1(int x)
{
    for(int i=1;i<=x;i++){
        cout<<i;
        if(i!=x){
            cout<<" ";
        }
    }
    cout<<endl;
}
void display2(int x,int y)
{
    if(x<=y){
        for(int i=x;i<=y;i++){
            cout<<i;
            if(i<y){
                cout<<" ";
            }
        }
    }else{
        for(int i=x;i>=y;i--){
            cout<<i;
            if(i>y){
                cout<<" ";
            }
        }
    }
    cout<<endl;
}
void display3(int x,int y,int t)
{
    if(x<=y){
        for(int i=x;i<=y;){
            cout<<i;
            i = i+t+1;
            if(i<=y){
                cout<<" ";
            }
        }
    }else{
        for(int i=x;i>=y;){
            cout<<i;
            i = i-t-1;
            if(i>=y){
                cout<<" ";
            }
        }
    }
    cout<<endl;
}
 
int main()
{
    string str;
    while(getline(cin,str)){
        if(str=="0"){
            return 0;
        }else{
            istringstream its(str);
            stringstream stream;
            int arr[1000],t=0;
            string s;
            while(its>>s){
                stream<<s;
                stream>>arr[t++];
                stream.clear();
            }
            if(t==1){
                display1(arr[0]);
            }
            else if(t==2){
                display2(arr[0],arr[1]);
            }
            else if(t==3){
                display3(arr[0],arr[1],arr[2]);
            }
        }
    }
    return 0;
}

⑤代码备注: 看起来很复杂(其实是繁杂),逻辑很清晰:1 输入一串字符串(老师点的数字),然后依据空格切片把数字存在整数数组中,再根据数组个数调用三个不同的函数(分别对应老师点名三种需求)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值