I would like to summarize the standard input / output in various languages. We will add languages from time to time. Table of Contents
For C and C ++
#include <stdio.h> //c
#include <iostream> //c++
Is assumed to be written in the header. It works not only in C but also in C ++, which is often faster.
Python3
s = input() #s:string
i = int(input()) #i:int
f = float(input()) #f:float
Python2
s = raw_input() #s:string
N = input() #Interpret the input as an expression
C
scanf("%c", &c); //c:char
scanf("%d", &i); //i:int(Decimal number)
scanf("%f", &f); //f:float
scanf("%s", s); //s:String array
C++
std::cin >> a;
* Addition
He commented that the C and C ++ examples can cause unintended results.
There was an article that you explained, so I will post it. Please see here for details.
https://qiita.com/yumetodo/items/238751b879c09b56234b
Python3
l = input().split() #Enter as a string l:list
l = list(map(int, input().split())) #Convert to integer l:list
l = [int(i) for i in input().split()] #Same as above Using list comprehension
#Which is faster, map or list comprehension, depends on the case
Python2
l = raw_input().split() #Enter as a string l:list
l = map(int, raw_input().split()) #Convert to integer l:list
C
for(int i=0,i<N,i++) scanf("%d", &l[i]); //Automatically recognize spaces as delimiters
C++
std::cin >> a; //slow
Python
a, b, c = map(int,input().split())
C
scanf("%d%d%d", &i, &j ,&k); //Automatically recognize spaces as delimiters
Python2
print a
print a, #Change line breaks to spaces
print(a) #Parentheses may or may not be present
Python3
print(a) #I need parentheses
print(a, b, c) #Multiple outputs(Space delimited)
print(a, b, c, sep=',') #Change the break
print(a, end=' ') #Change the last output character
C
printf("%s %s", "Hello", "world"); //'Hello, world'
printf("%c",'a'); //char
printf("%f", 3.14); //float
printf("%d", 334); //int(Decimal)
printf("%10s", "I'm fine."); //Character width(10)Designation
printf("%.2f", 3.14159); //"3,14"Number of digits after the decimal point(2)Designation These two can be set at the same time
printf("%.2e", 114.514); //"1.15e+02"Exponential notation
printf("%05d", 34); //"00034"0 packing
printf("%-10s", "Hello"); //Left
printf("%+d", 10); //"+10"Sign display
C++
std::cout << a << std::endl; //With line breaks Slow
std::cout << a << '\n'; //Fast with line breaks
std::cout << a; //No line breaks
Recommended Posts