1. 变量的定义

变量必须先定义,才可以使用。不能重名。
变量定义的方式:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

using namespace std;

int main()
{
int a = 5;
int b, c = a, d = 10 / 2;

return 0;
}

常用变量类型及范围:
1_5e05573e72-4.png

2. 输入输出

整数的输入输出:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

using namespace std;

int main()
{
int a, b;
cin >> a >> b;
cout << a + b << endl;
return 0;
}

字符串的输入输出:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

using namespace std;

int main()
{
int a, b;
cin >> a >> b;
cout << a + b << endl;
return 0;
}

输入输出多个不同类型的变量:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

using namespace std;

int main()
{
int a, b;
string str;

cin >> a;
cin >> b >> str;

cout << str << " !!! " << a + b << endl;

return 0;
}

3. 表达式

整数的加减乘除四则运算:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>

using namespace std;

int main()
{
int a = 6 + 3 * 4 / 2 - 2;

cout << a << endl;

int b = a * 10 + 5 / 2;

cout << b << endl;

cout << 23 * 56 - 78 / 3 << endl;

return 0;
}

1_22d9b8af72-5.png
浮点数(小数)的运算:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>

using namespace std;

int main()
{
float x = 1.5, y = 3.2;

cout << x * y << ' ' << x + y << endl;

cout << x - y << ' ' << x / y << endl;

return 0;
}

整型变量的自增、自减:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>

using namespace std;

int main()
{
int a = 1;
int b = a ++ ;

cout << a << ' ' << b << endl;

int c = ++ a;

cout << a << ' ' << c << endl;

return 0;
}

变量的类型转换:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>

using namespace std;

int main()
{
float x = 123.12;

int y = (int)x;

cout << x << ' ' << y << endl;

return 0;
}

4. 顺序语句

(1) 输出第二个整数:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>

using namespace std;

int main()
{
int a, b, c;
cin >> a >> b >> c;
cout << b << endl;
return 0;
}

(2) 计算 (a + b) * c的值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>

using namespace std;

int main()
{
int a, b, c;

cin >> a >> b >> c;

cout << (a + b) * c << endl;

return 0;
}

(3) 带余除法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

using namespace std;

int main()
{
int a, b;

cin >> a >> b;

int c = a / b, d = a % b;

cout << c << ' ' << d << endl;

return 0;
}

(4) 求反三位数:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>

using namespace std;

int main()
{
int n;
cin >> n;

int a = n % 10;
n = n / 10;
int b = n % 10;
n = n / 10;
int c = n;

cout << a << b << c << endl;

return 0;
}

(5) 交换两个整数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

using namespace std;

int main()
{
int a = 3, b = 4;

int t = a;
a = b;
b = t;

cout << a << ' ' << b << endl;

return 0;
}

(6) 输出菱形
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>

using namespace std;

int main()
{
char c;

cin >> c;

cout << " " << c << endl;
cout << " " << c << c << c << endl;
cout << c << c << c << c << c << endl;
cout << " " << c << c << c << endl;
cout << " " << c << endl;

return 0;
}