【输入输出篇】Leetcode算法刷题心得

本文档包含一系列编程题的解决方案,主要涉及A+B问题的不同实现方式,以及字符串排序的多种处理方法。从简单的两数相加到处理多个数的累加,再到字符串的排序,展示了不同编程语言如C++和Python的处理技巧。这些题目锻炼了程序员的数据处理和输入输出操作能力。


一、A+B

a = eval(input())
for i in range(a):
    num = list(map(int, input().split()))
    print(num[0] + num[1])
#include<iostream>
using namespace std;
 
int main(){
    int num,a,b;
    cin>>num;
    while(cin>>a>>b){
        cout<<a+b<<endl;
    }
    return 0;
}

二、 A+B(2)

在这里插入图片描述

num = list(map(int, input().split()))
while num != [0,0]:
    print(num[0] + num[1])
    num = list(map(int, input().split()))
#include<iostream>
using namespace std;
 
int main(){
    int a,b;
    while(cin>>a>>b){
        if(a==0 && b==0){
            break;
        }
        cout<<a+b<<endl;
    }
    return 0;
}

三、A+B(3)

在这里插入图片描述

#include<iostream>
using namespace std;
 
int main(){
    int num,temp,sum;
    while(cin>>num){
        if(num == 0) break;
        sum = 0;
        while(true){
            if(cin.get() == '\n') break;
            cin>>temp;
            sum+=temp;
        }
        cout<<sum<<endl;
    }
    return 0;
num = list(map(int, input().strip().split()))
while num[0] != 0:
    print(sum(num[1:]))
    num = list(map(int, input().strip().split()))

四、A+B(4)

在这里插入图片描述

using namespace std;
 
int main()
{
    int total, numCount, sum, number;
    cin >> total;
    while (total --) 
    {
        cin >> numCount;
        sum = 0;
        while (numCount --)
        {
            cin >> number;
            sum += number;
        }
        cout << sum << endl;
    }
    return 0;
}
a = int(input())
for i in range(a):
    num = list(map(int, input().split()))
    print(sum(num[1:]))

五、A+B(5)

在这里插入图片描述

#include<iostream>
using namespace std;
 
int main(){
    int n,temp,sum;
    while(cin>>n){
        sum = 0;
        while(n){
            cin>>temp;
            sum+=temp;
            --n;
        }
        cout<<sum<<endl;
    }
    return 0;
}
while True:
    try:
        nums = list(map(int,input().split()))
        print(sum(nums[1:]))
    except:
        break

六、A+B(6)

在这里插入图片描述

#include<iostream>
using namespace std;
 
int main() {
    int sum = 0;
    while(cin) {
        int x = 0;
        cin>>x;
        sum += x;
        if(cin.get() == '\n'){
            cout<<sum<<endl;
            sum = 0;
        }
    }
    return 0;
}
while True:
    try:
        data=list(map(int,input().split()))
        print(sum(data))
    except:
        break

七、字符串排序(1)

在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;
int main() {
    int n;
    cin >> n;
    vector<string> result;
    string s;
    while (n--) {
        cin >> s;
        result.push_back(s);
    }
    sort(result.begin(), result.end());
    for (const string tmp : result) {
        cout << tmp << " ";
    }
    return 0;
}
n=int(input().strip())
strs=input().split()
strs.sort()
print(" ".join(strs))

八、字符串排序(2)

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
 
int main(){
    vector<string> strs;
    string s;
    while(cin>>s){
        strs.push_back(s);
        if(cin.get()=='\n'){
            sort(strs.begin(),strs.end());
            for(int i=0;i<strs.size();i++){
                cout<<strs[i]<<' ';
            }
            cout<<endl;
            strs.clear();
        }
    }
    return 0;
}
while True:
    try:
        string = list(input().strip().split())
        string.sort()
        print(' '.join(j for j in string))
    except:
        break

九、字符串排序(3)

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
 
int main(){
    string s;
    vector<string> res;
    while(getline(cin, s)){ // 第一个getline保存输入至s
        stringstream ss(s); // 创建一个stringstream 存储s
        string tmp;
        while(getline(ss,tmp,',')){//第二个输入stringstream ss,读取的字符串保存在string类型的tmp中。
         // 以输入遇到回车换行符'\n'或EOF作为一行字符串输入操作结束, 或者遇到设定的字符 ',' 也可作为字符流读取结束。
            res.push_back(tmp);
        }
        sort(res.begin(),res.end());
        cout<<res[0];
        for(int i=1;i<res.size();i++){
            cout<<','<<res[i];
        }
        cout<<'\n';
        res.clear();
    }
    return 0;
}
while True:
    try:
        strs=input().split(',')
        strs.sort()
        print(",".join(strs))
    except:
        break
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值