基本数据类型
数据类型 就像你的身份,比如学生与工人。
每个变量和常量也有自己的“身份”,这指明了它们存储的内容与格式。
基本数据类型一览
整型 | int long long __int128_t |
int long |
int |
浮点型 | float double long double |
float double |
float |
字符型 | char | char | str |
布尔型 | bool | boolean | bool |
基本数据类型范围
C++
如果要使用浮点数,请尽量避免使用 float ,因为它的计算精度太低。
整型 | int | 约 |
|
整型 | long long | 约 |
|
整型 | __int128_t | 约 |
|
浮点型 | float | 6 位有效数位 | |
浮点型 | double | 15 位有效数位 | |
浮点型 | long double | 18 位有效数位 | |
字符型 | char | ||
布尔型 | bool |
Java
如果要使用浮点数,请尽量避免使用 float ,因为它的计算精度太低。
整型 | int | 约 |
|
整型 | long | 约 |
|
浮点型 | float | 6 位有效数位 | |
浮点型 | double | 15 位有效数位 | |
字符型 | char | ||
布尔型 | boolean |
Python
整型 | int | 无限大 | |
浮点型 | float | 15 位有效数位 | |
布尔型 | bool |
以下为各种语言的基本数据类型范围介绍:
C++ : cppreference.com
Java : docs.oracle.com
Python : docs.python.org
基础输入输出
C++
在 C++ 中,我们使用 cin
进行标准输入,使用 cout
进行标准输出。
为了更快地输入输出,请在 main 函数的开头加上两个语句,形如下方代码的第 5 行和第 6 行。
这两个语句,我们称之为 关闭流同步 。
第 5 行是切割 C风格的输入输出(scanf,printf) 和 C++风格的输入输出(cin,cout) 。
代价是,不能再使用C风格的输入输出。
第 6 行是切割 cin 和 cout 。
代价是,所有数据输入完毕之后,才会输出。
如果编译器支持 C++11 及以上,第 6 行应写作 cin.tie(nullptr);
。
cin 在输入浮点数时,会执行一些繁琐的安全检查,导致输入耗时增加。
这个影响在输入大量浮点数的场景下特别显著。
我们建议先将浮点数当作字符串读取进来,然后使用 stod 函数转换成浮点类型。
#include <iostream>
using namespace std;
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(0);
int v_int;
cin>>v_int;
cout<<v_int<<'\n';
long long v_long_long;
cin>>v_long_long;
cout<<v_long_long<<'\n';
float v_float;
cin>>v_float;
cout<<v_float<<'\n';
// We suggest using double instead of float
double v_double;
cin>>v_double;
cout<<v_double<<'\n';
// We suggest reading floating-point numbers as strings
// And then using the stod function to convert them to doubles
// The following is an example of code
/*
double v_tmp_double;
string v_tmp_string;
cin>>v_tmp_string;
v_tmp_double=stod(v_tmp_string);
cout<<v_tmp_double<<'\n';
*/
char v_char;
cin>>v_char;
cout<<v_char<<'\n';
return 0;
}
Java
在 Java 中,我们使用 Scanner
进行标准输入,使用 System.out
进行标准输出。
System.out 有 3 个常用方法,分别是 print,println,printf
。
System.out.print
是不换行输出。
System.out.println
是换行输出。
System.out.printf
是C风格的输出。
import java.util.Scanner;
public class InputAndOutput {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int v_int=sc.nextInt();
System.out.println(v_int);
long v_long=sc.nextLong();
System.out.println(v_long);
float v_float=sc.nextFloat();
System.out.println(v_float);
double v_double=sc.nextDouble();
System.out.println(v_double);
char v_char=sc.next().charAt(0);
System.out.println(v_char);
}
}
Python
在 Python 中,我们使用 input
进行标准输入,使用 print
进行标准输出。
为了更快地输入,请用 sys.stdin.readline
替换掉原生的输入流 input
。
import sys
input=sys.stdin.readline
v_int=int(input())
print(v_int)
v_float=float(input())
print(v_float)
v_str=input()
print(v_str)
进阶输入输出
C++
C++ 的格式化输出需要使用头文件 iomanip
。
fixed
指定输出不以 科学计数法 展示(科学计数法会输出类似 1.34e+25 的东西)。
setprecision
指定输出的有效位数(整数位 与 小数位,不包括小数点)。
两个一起使用,才能指定小数点后的位数。
setw
指定输出的位宽(如果是浮点数,包括小数点),如果设置的位宽小于实际宽度,则不产生任何影响。
setfill
指定输出的补全字符,默认在左边补。
#include <iostream>
#include <iomanip>
using namespace std;
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(0);
double v_double=1.2345678;
cout<<fixed<<setprecision(12)<<v_double<<'\n';
int v_int=123;
cout<<v_int<<' ';
cout<<setw(5)<<setfill('0')<<v_int<<'\n';
return 0;
}
Java
Java 的格式化输出需要使用 System.out.printf
。
括号内的参数格式都是和 C语言一模一样的。
public class FormatIO {
public static void main(String[] args) {
double v_double=1.2345678;
System.out.printf("%.12f\n",v_double);
int v_int=123;
System.out.print(v_int+" ");
System.out.printf("%05d\n",v_int);
}
}
Python
由于 Python 的输入是根据换行符来分割 token ,我们需要对不同情况做不同的处理。
如果在同一行,就用 split
函数分开,同时用 map
批量转换类型。
如果不在同一行,可以写三行输入,也可以构造一个列表表达式,让它自行解包。
Python 的格式化输出可以用 format
,也可以像C语言那样。
区别是用 百分号 替代了参数的第一个 逗号 。
import sys
input=sys.stdin.readline
# In the same row
'''
1 2 3
'''
a,b,c=map(int,input().split())
print(a,b,c)
# Three separate lines
'''
4
5
6
'''
d,e,f=[int(input()) for i in range(0,3)]
print(d,e,f)
v_float=1.2345678
print('%.12f'%v_float)
v_int=123
print(v_int,end=' ')
print('%05d'%v_int)