跳转至

入门1顺序结构

入门1【顺序结构】

B2002 hello, world!

#include <cstdio>
#include <iostream>

using namespace std;
int main () {
    printf("Hello,World!");
    return 0;
}

B2025 输出字符菱形

#include <cstdio>
#include <iostream>

using namespace std;

int main () {
    printf("  *  \n *** \n*****\n *** \n  *  \n");
    return 0;
}

P1000 超级玛丽游戏

#include <cstdio>
#include <iostream>

using namespace std;

int main () {
    printf("                ********\n"
           "               ************\n"
           "               ####....#.\n"
           "             #..###.....##....\n"
           "             ###.......######              ###            ###\n"
           "                ...........               #...#          #...#\n"
           "               ##*#######                 #.#.#          #.#.#\n"
           "            ####*******######             #.#.#          #.#.#\n"
           "           ...#***.****.*###....          #...#          #...#\n"
           "           ....**********##.....           ###            ###\n"
           "           ....****    *****....\n"
           "             ####        ####\n"
           "           ######        ######\n"
           "##############################################################\n"
           "#...#......#.##...#......#.##...#......#.##------------------#\n"
           "###########################################------------------#\n"
           "#..#....#....##..#....#....##..#....#....#####################\n"
           "##########################################    #----------#\n"
           "#.....#......##.....#......##.....#......#    #----------#\n"
           "##########################################    #----------#\n"
           "#.#..#....#..##.#..#....#..##.#..#....#..#    #----------#\n"
           "##########################################    ############");
    return 0;
}

P1001 A+B Problem

#include <cstdio>
#include <iostream>

using namespace std;

int main () {
    int a, b;
    scanf("%d%d", &a, &b);
    printf("%d", a + b);
    return 0;
}

B2005 字符三角形

#include <cstdio>
#include <iostream>

using namespace std;

int main () {
    char a;
    scanf("%c", &a);
    printf("  %c  \n %c%c%c \n%c%c%c%c%c", a, a, a, a, a, a, a, a, a);
    return 0;
}

P5703 【深基2.例5】苹果采购

#include <cstdio>
#include <iostream>

using namespace std;

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

P5704 【深基2.例6】字母转换

#include <cstdio>
#include <iostream>

using namespace std;

int main () {
    char a;
    cin >> a;
    printf("%c", a - 32);
    return 0;
}

12/21

P5705 【深基2.例7】数字反转 (*)

#include <iostream>
#include <string>

using namespace std;

int main () {
    string a;
    cin >> a;
    for (int i = a.size() - 1; i >= 0; i--) cout << a[i];
   return 0;
}

P5706 【深基2.例8】再分肥宅水

#include <iostream>
#include <cstdio>

using namespace std;

int main () {
    float a;
    int b;
    cin >> a >> b;
    printf("%.3f\n", a / b);
    printf("%d", b * 2);
    return 0;
}

P5708 【深基2.习2】三角形面积

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

int main () {
    double a, b, c;
    cin >> a >> b >> c;
    double p = (a + b + c) / 2;
    double s = sqrt(p * (p - a) * (p - b) * (p - c));
    printf("%.1f", s);
    return 0;
}

P5707 【深基2.例12】上学迟到 (*)

注意ceil()是向上取整

注意要加#include <cmath>的头文件

function 效果
floor() 不大于自变量的最大整数
ceil() 不小于自变量的最大整数
round() 四舍五入到最邻近的整数
fix() 朝零方向取整

%02d就能输出2位补足的数字

#include <iostream>
#include <cmath>
using namespace std;

int main () {
    float s, v;
    cin >> s >> v;
    int duration = ceil(s / v);
    int time = 24 * 60 + 8 * 60 - duration - 10;
    if (time >= 24 * 60) time -= 24 * 60;
    int hour = time / 60;
    int minute = time % 60;
    printf("%02d:%02d", hour, minute);
    return 0;
}

B2029 大象喝水

#include <iostream>
#include <cmath>

using namespace std;

int main () {
    double h, r;
    cin >> h >> r;
    double v = (3.14159 * r * r) * h;
    cout << ceil(20000 / v);
    return 0;
}

P1425 小鱼的游泳时间

#include <iostream>
using namespace std;

int main () {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    int duration = (c * 60 + d) - (a * 60 + b);
    cout << duration / 60;
    cout << " " << duration % 60;
    return 0;
}

P1421 小玉买文具

#include <iostream>
#include <cstdio>
using namespace std;
int main () {
    int a, b;
    cin >> a >> b;
    cout << (10 * a + b) / (10 + 9);
    return 0;
}

P3954 [NOIP2017 普及组] 成绩

#include <iostream>
using namespace std;

int main () {
    int a, b, c;
    cin >> a >> b >> c;
    cout << (int) (0.2 * a + 0.3 * b + 0.5 * c);
    return 0;
}