牛客网 ACM 模式 输入输出

昨晚做深信服笔试,第一次碰到ACM模式…结果输入输出调不明白,凉的很彻底。

牛客自己的练习场

一、Python

python 中 input() 函数不管输入是什么,输出一定为str,所以要进行强制转换,可以用int(x)或者map(int, x)

strip()函数用于去掉字符串两端的某重复字符

# 例:去掉前后多个空格
x = input().strip().split()
  1. 只有一行输入,只用input()
num = list(map(int, input().split(" ")))
print(num)

# 知道输入元素的固定个数时,还可以一次性赋给两个元素
v, w = map(int, input().split())
  1. 多行输入输出,用while True,加try except判断
while True:
	try:
		num = list(map(int, input().split(" ")))
		print(sum(num))
	except:
		break
  1. 复合型,第一行是一个数,后面几行是数组
x = int(input())
for i in range(x):
	num = list(map(int, input().split(" ")))
	print(sum(num))
  1. 多行输入,满足某个条件退出
while True:
    try:
        num = list(map(int,input().split(" ")))
        if num[0] == num[1] == 0:
            break
        print(sum(num)) 
    except:
        break
  1. 输入为列表形式(带括号)
s = input()   # '[1,2,3]'
s = eval(s)   # [1,2,3] 此时已经转为列表,里面存的也都是int
  1. 输出需要空格或者逗号分割,用join()函数
t = int(input())
num = list(input().split(" "))
num.sort()
print(" " .join(num))

二、C++

C++的两种常用输入模式:

  • cin >> x
    自动跳过 \t\s\n 取数

  • getline(cin, x)
    可以读入\t\s,但遇到\n停止读取,且读取完后丢弃末尾换行符

  1. 读取一行多个数(不知道几个)
#include<bits/stdc++.h>
using namespace std;

int main(){
	int n = 0;
	while (cin >> n){
		int sum = 0;
		int tmp;
		for(int i = 0; i < n; i++){
			cin >> tmp;
			
		}
		cout << x << endl;
	}
	return 0;
}
  1. 多行输入输出(固定数量)
#include<bits/stdc++.h>
using namespace std;

int main() {
    int a, b;
    while (cin >> a >> b) { // 注意 while 处理多个 case
        // 64 位输出请用 printf("%lld")
        cout << a + b << endl;
   }
   return 0;
}
  1. 多行输入输出(数量未知)
#include<bits/stdc++.h>
using namespace std;

int main(){
	int n;
	while (cin >> n){
		int sum = 0;
		while (cin.get() != '\n'){
			int tmp;
			cin >> tmp;
			sum += tmp;
		}
		cout << sum << endl;
	}
	return 0;
}
  1. 复合型,第一行是一个数,后面几行是数组
#include<bits/stdc++.h>
using namespace std;

int main() {
    int a, b, c;
    cin >> a;
    while(cin >> b >> c) {
        cout << b + c << endl;
    }
    return 0;
}
  1. 输入为复合型,第一行n,第二行字符串
#include<bits/stdc++.h>
using namespace std;

int main(){
	int n;
	cin >> n;
	vector<string> res(n);
	for(int i = 0; i < n; i++){
		cin >> res[i];
	}
	sort(res.begin(), res.end());
	for(int i = 0; i < n; i++){
		cout << res[i] << " ";
	} 
	return 0;
}
  1. 输入为多行空格分开的字符串
#include <bits/stdc++.h>
using namespace std;
 
int main() {
    string s;
    vector<string> res;
    while(cin >> s){
        res.push_back(s);
        if(cin.get() == '\n'){
            sort(res.begin(),res.end());
            for(auto c:res) cout << c << " ";
            cout << endl;
            res.clear();
        }
    }
    return 0;
}
  1. 输入为多行逗号分开的字符串
#include <bits/stdc++.h>
using namespace std;
 
int main() {
    string s;
    vector<string> res;
    while(cin >> s){
        stringstream ss(s);
        string tmp;
        while(getline(ss, tmp, ',')){
        	res.push_back(tmp);
        }
        sort(res.begin(),res.end());
        for(auto& r : res){
        	cout << r << " ";
        }
        cout << endl;
    }
    return 0;
}
  1. 输入为列表形式(带括号)
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值