cin cout
#include <iostream>
using namespace std;
int main()
{
int x;
cin >> x;
cout << x;
return 0;
}
printf
#include <cstdio>
using namespace std;
int main()
{
int x;
scanf("%d", &x);
printf("%d\n", x);
printf("%10d\n", x); // 占10个字符宽度,靠右对齐
printf("%-10d\n", x); // 占10个字符宽度,靠左对齐
double y;
scanf("%lf", &y);
printf("%lf\n", y);
printf("%.2lf\n", y); //小数点后保留2位
return 0;
}
// 如果输出是两个int,中间有一个空格
int a = 1, b = 2;
printf("%d %d", a, b);
// 如果输出是两个int,中间有一个加号
int a = 1, b = 2;
printf("%d+%d", a, b);
// 如果只是输出一句话,可以这样
printf("Hello world\n");
// 在双引号里面,是所见即所得
// %d 称之为,format specifier, 格式化符号
// % 后面跟什么,是不同变量类型,跟不同的东西
// 常见的,%d int,%lf double,%c 字符,%s 字符串
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
char c;
//cin >> c;
//cout << c << '\n';
// a b c A B C * - 1 0
scanf("%c", &c);
printf("%c\n", c);
return 0;
}
// 学习一下字符数组、一维字符数组、二维字符数组
// string类
#include <bits/stdc++.h>
using namespace std;
char c;
string s1;
char s2[110];
char s3[110][110];
int main(){
// 以字符为例
cin >> c;
cout << c << '\n';
// 读入string类
cin >> s1;
cout << s1 << '\n';
// 读入字符数组
scanf("%s", s2);
printf("%s\n", s2);
// 读入二维字符数组
int n;
cin >> n;
for (int i = 0; i < n; i++) scanf("%s", s3[i]);
for (int i = 0; i < n; i++) printf("%s\n", s3[i]);
return 0;
}
// 占用字符宽度
// int类型 占多少个字符宽度
cin >> n;
printf("%8d %8d\n", n, n);
printf("%-8d %-8d\n", n, n);
// 保留小数点的问题
// double 类型保留小数点后几位
printf("%.20lf\n", 1.0 / 3);
// 一般就用printf解决保留小数点几位的问题,不用setprecision,主要是记不住
// 竞赛当中,很少使用不用setprecision
double x = 3.1415926;
cout << x << '\n';
cout << setprecision(5) << x << '\n';
cout << setprecision(4) << x << '\n';
cout << setprecision(3) << x << '\n';
cout << setprecision(2) << x << '\n'; // 3.1
cout << setprecision(1) << x << '\n'; // 3
// setprecision 不计算小数点
// #include <iomanip>
// 与 setw 字段宽度不同的是
// setprecision 的精度设置将保持有效,直到更改为其他值为止
// 流操作符 setw 可用于建立指定宽度的打印区域
// 空格“填充”在前面,所以它被认为是右对齐的
// 左对齐也能搞,麻烦
// 用一回,写一回
int y;
cin >> y;
cout << setw(8) << y << setw(8) << y << '\n';
scanf
注意看,这个参数是 point to already allocated objects,指向了一个已经存在的对象
#include <bits/stdc++.h>
using namespace std;
int main(){
int x;
scanf("%d", &x); // &取地址符号
printf("%d\n", x);
return 0;
}
/*
输入:123
输出:123
*/
#include <bits/stdc++.h>
using namespace std;
int main(){
int x;
scanf("%d", x);
printf("%d\n", x);
return 0;
}
// 编译报错
std03.cpp:7:14: warning: format specifies type 'int *' but the argument has type 'int'
[-Wformat]
scanf("%d", x);
~~ ^
#include <bits/stdc++.h>
using namespace std;
int main(){
int x;
int *px = &x;
scanf("%d", px);
printf("%d\n", x);
return 0;
}
/*
输入:123
输出:123
*/
#include <bits/stdc++.h>
using namespace std;
int main(){
int x;
int *px = &x;
scanf("%d", *px); //如果这样写
printf("%d\n", x);
return 0;
}
//编译报错
std03.cpp:8:14: warning: format specifies type 'int *' but the argument has type 'int'
[-Wformat]
scanf("%d", *px);
#include <bits/stdc++.h>
using namespace std;
int a[10];
int main(){
for (int i = 0; i < 5; i++)
scanf("%d", a + i);
//scanf("%d", &a[i]); //这两句话作用是一样的
return 0;
}
//读入字符串
#include <bits/stdc++.h>
using namespace std;
char s[10];
int main(){
scanf("%s", s); //这个时候,写的是数组名称,数组名本身就是一个指针
return 0;
}
// 在读入数据的时候,充分利用格式化的优点
// 输入数据1980-12-1
#include <bits/stdc++.h>
using namespace std;
int year, month, day;
int main(){
scanf("%d-%d-%d", &year, &month, &day);
printf("%d %d %d\n", year, month, day);
return 0;
}
// 上图,解释了,scanf里面有空格不影响的原因
// 还有scanf里面有具体符号时候,匹配的问题
// 还有具体限制读入一个字符宽度的数字
#include<bits/stdc++.h>
using namespace std;
int main(){
int a, b, c;
scanf("%1d%1d%1d", &a, &b, &c);
printf("%d %d %d\n", a, b, c);
return 0;
}
/*
123
*/
例题,1982:【19CSPJ普及组】数字游戏
// 这个就可以利用scanf的性质处理
例题,计算三角形面积
//海伦公式
p = (a+b+c) / 2;
s = sqrt(p * (p-a) * (p-b) * (p-c));
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
double x1, y1, x2, y2, x3, y3, a, b, c, p, s;
scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3);
a = sqrt((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1));
b = sqrt((y3-y1)*(y3-y1)+(x3-x1)*(x3-x1));
c = sqrt((y2-y3)*(y2-y3)+(x2-x3)*(x2-x3));
p = (a+b+c)/2;
s = sqrt(p*(p-a)*(p-b)*(p-c));
printf("%.2lf",s);
return 0;
}
例题,等差数列末项计算
//等差数列求和公式
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a1, a2, n;
cin >> a1 >> a2 >> n;
int d = a2 - a1;
int an = a1 + (n - 1) * d;
cout << an << endl;
return 0;
}
例题,计算2的幂
//幂次,方 的叫法
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
int res = 1;
while (n--)
{
res *= 2;
}
cout << res << endl;
return 0;
}
例题,苹果和虫子
//还是题意,剩余多少完整的苹果
//也要注意苹果很少,时间很长的情况,会是什么结果【*】
整数除法
int x = 1, y = 2;
cout << x / y << '\n';
// 会输出 0
// 整数除以整数,会自动向下取整
cout << (x + y - 1) / y << '\n';
// 这样做会得到一个向上取整的结果
double z = 1.0 * x / y;
// 这样能得到一个小数结果