#include#includeusingnamespace std;intmain(){int a =3;float b =3.12345678;double c =3.12345678;char d ='y';printf("%d\n", a);printf("%f\n", b);printf("%lf\n", c);printf("%c\n", d);return0;}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1.2 所有输出的变量均可包含在一个字符串中
#include#includeusingnamespace std;intmain(){int a =3;float b =3.12345678;double c =3.12345678;char d ='y';printf("int a = %d,float b = %f\n,double c = %lf, char d = %c\n",a, b, c, d);return0;}
#include#includeusingnamespace std;intmain(){int t;
cin >> t;int hours = t /3600;int minutes = t %3600/60;int seconds = t %60;printf("%d:%d:%d\n",hours, minutes, seconds);return0;}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1.3 扩展功能
float, double 等输出保留若干位小数时用:%.4f, %3lf
#include#includeusingnamespace std;intmain(){float b =3.12345678;double c =3.12345678;printf("%.4f\n",b);printf("%.3lf\n",c);return0;}
1
2
3
4
5
6
7
8
9
10
11
12
13
最小数字宽度
%8.3f, 表示这个浮点数的最小宽度为 8,保留 3 位小数,当宽度不足时在前面补空格
#include#includeusingnamespace std;intmain(){int a =3;float b =3.12345678;double c =3.12345678;printf("%5d\n",a);printf("%8.4f\n",b);printf("%7.3lf\n",c);return0;}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
%-8.3f,表示最小宽度为 8,保留 3 位小数,当宽度不足时在后面补上空格
#include#includeusingnamespace std;intmain(){int a =3;float b =3.12345678;double c =3.12345678;printf("%-5d\n",a);printf("%-8.4f\n",b);printf("%-7.3lf\n",c);return0;}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
%08.3f, 表示最小宽度为 8,保留 3 位小数,当宽度不足时在前面补上0
#include#includeusingnamespace std;intmain(){int a =3;float b =3.12345678;double c =3.12345678;printf("%05d\n",a);printf("%08.4f\n",b);printf("%07.3lf\n",c);return0;}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2. if 语句
2.1 基本 if-else 语句
当条件成立时,执行某些语句;否则执行另一些语句
#include#includeusingnamespace std;intmain(){int a =3;
cin >> a;if(a >5){printf("%d is big!\n",a);printf("%d + 1 = %d\n",a ,a+1);}else{printf("%d is small!\n",a);printf("%d - 1 = %d\n",a ,a-1);}return0;}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
else 语句可以省略
#include#includeusingnamespace std;intmain(){int a =3;
cin >> a;if(a >5){printf("%d is big!\n",a);printf("%d + 1 = %d\n",a ,a+1);}return0;}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
当只有一条语句时,大括号可以省略
#include#includeusingnamespace std;intmain(){int a =3;
cin >> a;if(a >5)printf("%d is big!\n",a);elseprintf("%d is small!\n",a);return0;}
#include#includeusingnamespace std;intmain(){int a, b;
cin >> a >> b;if(a > b)
cout << a << endl;else
cout << b <<endl;return0;}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2.1.3 练习3
If-else 语句内部也可以是 if-else 语句
输入三个整数,输出三个数中最大的那个
#include#includeusingnamespace std;intmain(){int a, b, c;
cin >> a >> b >> c;if(a > b){if(a > c) cout << a << endl;else cout << c << endl;}else{if(b > c) cout << b << endl;else cout << c << endl;}return0;}
#include#includeusingnamespace std;intmain(){int a, b, c;
cin >> a >> b >> c;if(a > b && a > c) cout << a << endl;elseif(b > a && b > c) cout << b << endl;else cout << c << endl;}return0;}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
3.2 练习2
用一条 if 语句,判断闰年
#include#includeusingnamespace std;intmian(){int year;
cin >> year;if(year %100==0&& year %400==0|| year %4==0&& year %100!=0) cout <<"yes"<< endl;else cout <<"no" endl;}}return0;