• 牛客网 ACM 模式 输入输出


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

    牛客自己的练习场

    一、Python

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

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

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

    二、C++

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

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

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

    1. 读取一行多个数(不知道几个)
    #include
    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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    1. 多行输入输出(固定数量)
    #include
    using namespace std;
    
    int main() {
        int a, b;
        while (cin >> a >> b) { // 注意 while 处理多个 case
            // 64 位输出请用 printf("%lld")
            cout << a + b << endl;
       }
       return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    1. 多行输入输出(数量未知)
    #include
    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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    1. 复合型,第一行是一个数,后面几行是数组
    #include
    using namespace std;
    
    int main() {
        int a, b, c;
        cin >> a;
        while(cin >> b >> c) {
            cout << b + c << endl;
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    1. 输入为复合型,第一行n,第二行字符串
    #include
    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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    1. 输入为多行空格分开的字符串
    #include 
    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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    1. 输入为多行逗号分开的字符串
    #include 
    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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    1. 输入为列表形式(带括号)
  • 相关阅读:
    基于JAVA旅游景区预约管理系统计算机毕业设计源码+系统+mysql数据库+lw文档+部署
    QT5到QT6产生的一些变化【QT环境搭建篇】
    Nacos注册中心细节分析
    STC 51单片机51——直流电机PWM
    位图的基本原理以及应用
    【AD-NeRF】音频驱动人脸NeRF
    一个好的产品如何定义?
    实验八 数据处理与多项式处理(matlab)
    Springboot自身缓存集合Redis的实现方式
    【Golang开发面经】B站(两轮技术面)
  • 原文地址:https://blog.csdn.net/weixin_41206209/article/details/126656319