一、printf输出格式

注意:使用printf时最好添加头文件 #include <cstdio>

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

using namespace std;

int main()
{
printf("Hello World!");

return 0;
}

  1. Int、float、double、char等类型的输出格式:
    (1) int:%d
    (2) float: %f, 默认保留6位小数
    (3) double: %lf, 默认保留6位小数
    (4) char: %c, 回车也是一个字符,用’\n’表示

    1. Int、float、double、char等类型的输出格式:

    (1) int:%d
    (2) float: %f, 默认保留6位小数
    (3) double: %lf, 默认保留6位小数
    (4) char: %c, 回车也是一个字符,用'\n'表示

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

    using namespace std;

    int main()
    {
    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);

    return 0;
    }

    2. 所有输出的变量均可包含在一个字符串中:

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

    using namespace std;

    int main()
    {
    int a = 3;
    float b = 3.12345678;
    double c = 3.12345678;
    char d = 'y';

    printf("int a = %d, float b = %f\ndouble c = %lf, char d = %c\n", a, b, c, d);

    return 0;
    }

    练习:输入一个字符,用这个字符输出一个菱形:

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

    using namespace std;

    int main()
    {
    char c;
    cin >> c;

    printf(" %c\n", c);
    printf(" %c%c%c\n", c, c, c);
    printf("%c%c%c%c%c\n", c, c, c, c, c);
    printf(" %c%c%c\n", c, c, c);
    printf(" %c\n", c);

    return 0;
    }

    3. 扩展功能

    (1) float, double等输出保留若干位小数时用:%.4f, %.3lf

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    #include <iostream>
    #include <cstdio>

    using namespace std;

    int main()
    {
    float b = 3.12345678;
    double c = 3.12345678;

    printf("%.4f\n", b);
    printf("%.3lf\n", c);

    return 0;
    }

    (2) 最小数字宽度

  2. %8.3f, 表示这个浮点数的最小宽度为8,保留3位小数,当宽度不足时在前面补空格。

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

    using namespace std;

    int main()
    {
    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);

    return 0;
    }
  3. %-8.3f,表示最小宽度为8,保留3位小数,当宽度不足时在后面补上空格

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

    using namespace std;

    int main()
    {
    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);

    return 0;
    }
  4. %08.3f, 表示最小宽度为8,保留3位小数,当宽度不足时在前面补上0

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

    using namespace std;

    int main()
    {
    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);

    return 0;
    }

    二、if 语句

    1. 基本if-else语句

    当条件成立时,执行某些语句;否则执行另一些语句。

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

    using namespace std;

    int main()
    {
    int a;
    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);
    }

    return 0;
    }

    else 语句可以省略:

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

    using namespace std;

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

    if (a > 5)
    {
    printf("%d is big!\n", a);
    printf("%d + 1 = %d\n", a, a + 1);
    }

    return 0;
    }

    当只有一条语句时,大括号可以省略:

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

    using namespace std;

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

    if (a > 5)
    printf("%d is big!\n", a);
    else
    printf("%d is small!\n", a);

    return 0;
    }

    2. 常用比较运算符

    (1) 大于 >
    (2) 小于 <
    (3) 大于等于 >=
    (4) 小于等于 <=
    (5) 等于 ==
    (6) 不等于 !=

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

    using namespace std;

    int main()
    {
    int a, b;
    cin >> a >> b;

    if (a > b) printf("%d > %d\n", a, b);
    if (a >= b) printf("%d >= %d\n", a, b);
    if (a < b) printf("%d < %d\n", a, b);
    if (a <= b) printf("%d <= %d\n", a, b);
    if (a == b) printf("%d == %d\n", a, b);
    if (a != b) printf("%d != %d\n", a, b);

    return 0;
    }

    三、条件表达式

    (1) 与 &&
    (2) 或 ||
    (3) 非 !