牛客网&赛码网 输入输出格式 python&C++

Note:

你的程序需要stdin(标准输入)读取输入,然后stdout(标准输出)来打印结果,举个例子,你可以使用c语言的scanf或者c++的cin来读取输入,然后使用c语言的printf或者c++的cout来输出结果。

一次处理多个case,所以代码需要循环处理,一般通过while循环来出来多个case。

C++

以下例子为空格隔开,使用cin、cout读入读出。赛码网与牛客网一致。

1)单行输入

#include<iostream>
using namespace std;
int main(){int m,n,k;cin>>m>>n;cout<<m<<endl;cout<<n<<endl;return 1;
}

2)多行输入,每一行是一个测试样例,不确定样例个数,所以用while()循环读取

#include <iostream>
using namespace std;
int main() {int a,b;while(cin >> a >> b)//注意while处理多个casecout << a+b << endl;
}

3)多个测试用例,每个测试用例多行
输入包含多组测试用例。对于每组测试用例:第一行包含两个整数N和M,在接下来的M行内,每行包括3个整数。要求按照输入格式输出。
已知每个测试用例有M组输入,就可以用for(int i = 0; i < M; i++)来读取M组输入。

#include  <iostream>
using namespace std;
int main()
{int N, M;// 每组第一行是2个整数,N和M,至于为啥用while,因为是多组。while(cin>> N >> M) {cout << N << " " << M << endl;// 循环读取“接下来的M行”for (int i=0; i<M; i++) {int a, b, c;cin >> a >> b >> c;cout << a << " " << b << " " << c << endl;}}return 0;
}

4) 固定个数&格式的输入,需一次全部读入;固定格式的输出,按照题目要求设计。
输入:getline()
getline每遇到一个行分割符都会返回一次;
getline()函数从输入流中提取字符并将其附加到字符串对象中,直到遇到分隔字符为止;
在执行此操作时,字符串对象str中先前存储的值将被输入字符串(如果有的话)替换。
istream& getline(istream& is,
string& str, char delim);

istream& getline (istream& is, string& str); //an delimitation character is by default new line(\n)character

stringstream: 例如cin,输入流
string:字符串

example:

#include <iostream>
#include <sstream>
using namespace std;string input = "abc,def,ghi";
istringstream ss(input);
string token;int main(){while(getline(ss, token, ',')) {cout << token << endl;}return 0;
}

output:

abc
def
ghi

将input string用istringstream转成input stream,token用于保存每个被“,”分隔开的一个小输入(e.g abc),以string的形式,每次遇到“,”都会停止读取输入并输出token的值。token的值每次从ss中读取都会被覆盖。

Examples:

  1. 字符串排序(1)
#include<iostream>
#include<vector>
#include<string>
#include <algorithm>
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]; if(i < n-1){cout << " ";}}return 0;
}
  1. 字符串排序(2)

    第一种方法:使用get(),读到换行符\n时停止读取,开始输出。输出完后,若还有输入,继续读取。
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
int main()
{vector<string> a;string s;while(cin>>s){a.push_back(s);if(cin.get()=='\n'){sort(a.begin(),a.end());for(int i=0;i<a.size();i++){cout<<a[i];if(i<a.size()-1) cout << " ";}cout<<endl;a.clear();}}return 0;
}

第二种方法:用getline(cin, s, ’\n’)一次读一行,每行都由若干字符串组成,字符串间由空格‘ ’隔开。那么可以读取这整个字符串,读到空格时说明上一个字符串读完了,将字符串存到string temp里,再将temp存入vector里,clear temp。

         if(s[i]!=' '){temp.push_back(s[i]);//读取字符}else{//读到空格,将temp中存的string如aaa存入vectorres.push_back(temp);temp.clear();}
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>using namespace std;
int main()
{string s;while(getline(cin,s,'\n')){string temp;vector<string> res;for(int i=0;i<s.size();i++){if(s[i]!=' '){temp.push_back(s[i]);}else{res.push_back(temp);temp.clear();}}res.push_back(temp);clear(temp);sort(res.begin(), res.end());for(int i=0;i<res.size();i++)//print out vector<string>{cout<<res[i];if(i < res.size()-1) cout<<' ';//between strings, print ' '}cout<<endl;//at the end, print \n   }return 0;}
  1. 字符串排序(3)
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
int main(){string s;vector<string> str;while(cin >> s){string tmp = "";for(int i = 0; i < s.size(); ++ i){if(s[i] == ','){str.push_back(tmp);tmp = "";}else{tmp += s[i];}}str.push_back(tmp);if(cin.get() == '\n'){sort(str.begin(), str.end());for(int i = 0; i < str.size() - 1; i ++){cout << str[i] << ",";}//除了最后一个字符外,都是按“字符+逗号”的格式,所以循环到i < str.size() - 1cout << str[str.size() - 1] << endl;//最后一个字符只输出字符str.clear();}}return 0;
}
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
int main(){string s;vector<string> str;while(cin >> s){string tmp = "";for(int i = 0; i < s.size(); ++ i){if(s[i] == ','){str.push_back(tmp);tmp = "";}else{tmp += s[i];}}str.push_back(tmp);if(cin.get() == '\n'){sort(str.begin(), str.end());//与上面的代码相比,只改动了输出部分。换了种写法,完全一个意思。for(int i=0;i<str.size();i++){cout<<str[i];if(i<str.size()-1) cout << ",";}cout<<endl;str.clear();}}return 0;
}

Python3

1) 赛码网

以下例子为空格隔开,input()读入,print()读出。

单个输入,单个输出

input = int(input())
output = input
print(str(output))

单行多个输入,单行多个输出,空格分割 (多行输入,每一行是一个测试样例)

inputs = list(map(int, input().split(" ")))
m, n = inputs[0], input[1]
print(str(m), str(n))

多个测试案例,每个测试案例多行

while 1:nm = list(map(int,input().split(" ")))N = nm[0]M = nm[1]print(str(n)+' '+str(m))for i in range(m):abc = list(map(int, input().split(" ")))a, b, c = abc[0], abc[1], abc[2]print(str(a)+' '+str(b)+' '+str(c))

2) 牛客网

牛客网建议使用sys.stdin.readline()获取输入,使用print()进行输出。
输入用空格隔开。如要用,隔开,用.split(",")。

多行输入,每一行是一个测试样例

import sys
for line in sys.stdin:a = line.split()print(int(a[0]) + int(a[1]))

多个测试用例,每个测试用例有多行
输入包含多组测试用例。对于每组测试用例:第一行包含两个整数N和M,在接下来的M行内,每行包括3个整数。要求按照输入格式输出。

import sys
for line in sys.stdin:n, m = map(int, line.strip().split())print(n, m)for i in range(m):for line in sys.stdin:a, b, c = map(int, line.strip().split())print(a, b, c)

牛客网赛码网 输入输出格式 pythonC++相关推荐

  1. 牛客网赛码网 输入输出格式

    目录 牛客网&赛码网输入输出 牛客网 单行输入 多行输入,每一行是一个测试样例 多个测试用例,每个测试用例有多行 赛码网 单个输入,单个输出 单行多个输入,单行多个输出,空格分割 多个测试案例 ...

  2. 牛客网赛码网输入输出总结(python版)

    1. 单行输入: n, m = map(int, sys.stdin.readline().strip().split()) 对该行中所有元素取整型后赋给等式左边若干参数 2. 多行输入: 先获取第一 ...

  3. 牛客网 赛码网 js输入输出

    牛客网 详细地址 let readline = require('readline') const r1 = readline.createInterface({input: process.stdi ...

  4. 赛码网和牛客网python输入输出要求

    1. 牛客网推荐使用sys.stdin.readline()形式输入,输出用print() ''' 输入: 2 3 分别赋给n, m并且转换为int类型 ''' import sysn, m = ma ...

  5. js牛客网、赛码网输入输出

    js牛客网.赛码网输入输出 牛客网在线判题系统JavaScript(V8)使用 输入 单行输入 只有一行输入或者一个 var line = read_line(); 只有一行数组 var line = ...

  6. 赛码网输入输出格式和练习题

    单行输入 单行输入 每行输入三个值 Scanner input = new Scanner(System.in);while(input.hasNextInt()){ //用input.hasNext ...

  7. 【赛码网 牛客网】输入输出总结(python版)

    1. 单行输入 n, m = map(int, sys.stdin.readline().strip().split()) 对该行中所有元素取整型后赋给等式左边若干参数 2. 多行输入: 先获取第一行 ...

  8. 赛码网输入输出总结+真题演练

    赛码网输入输出总结 前言 类型一:测试组数不固定,每组三行数据 踩坑记录1 踩坑记录2 类型2 测试组数不定,输入数据中有指定行数的多行输入(赛码网找老乡题最完美答案) 类型三:一组数据,有指定行数的 ...

  9. 赛码网算法: 上台阶 ( python3实现 、c实现)

    上台阶 题目描述 有一楼梯共m级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第m级,共有多少走法?注:规定从一级到一级有0种走法. 输入输入数据首先包含一个整数n(1<=n<=1 ...

最新文章

  1. Two Sum [easy] (Python)
  2. Java6.0中Comparable接口与Comparator接口详解
  3. 初步了解学习将传统单机应用改造成Dubbo服务的过程
  4. 【机器学习】降维技术-PCA
  5. linux运行多个c文件路径,linux c的连接库和怎么同时编译多个源程序
  6. 云视频会议对初创公司的益处
  7. WPS自己用来看的 方便记录
  8. 计算机鼠标老跳动,电脑鼠标总跳是怎么回事?
  9. 【模拟器】网工福音!华三模拟器HCL升级,部分功能超越eNSP和EVE!
  10. J2me实现的wap浏览器
  11. python matplotlib 矢量图svg emf
  12. 龙芯2f笔记本安装gentoo
  13. Linux下载GEO数据,教你3种方法下载NCBI GEO数据
  14. 小米手机不能发短信的一种解决方法--重新设置短信中心号码
  15. Java利用libreOffice(jodconverter)将office(ppt,Excel,word,text)文档转换成pdf
  16. 超详细傻瓜iPhone自定义来电铃声教程
  17. [读书]~偷得浮生半日闲
  18. Origin画图技巧之3D锥形图
  19. java深入理解深拷贝和浅拷贝的区别 如何实现深拷贝和浅拷贝
  20. 论文笔记 AAAI 2021|what the role is vs. What plays the role: Semi-supervised Event Argument Extraction v

热门文章

  1. Windows下,文件(夹)选择/打开对话框的三种创建方式
  2. 实现移动端查看控制台
  3. Windows Server 安装 Adobe Flash Player
  4. 9_python笔记-函数
  5. 单摆测重力加速度的算法(Python)
  6. 学生问我25-30K得面试题能不能帮忙,这我不得上,爬取某网站电影视频内容
  7. python中area是什么意思_python之懒惰属性(延迟初始化)
  8. 用应用软件UCCW Widget制作出属于你独一无二的梦幻桌面吧!
  9. 前端高效开发框架技术(疫情会不会大暴发 听听钟南山怎么说)
  10. 安装widows XP时蓝屏代码0X0000007B的问题的解决方案