洛谷【入门1】顺序结构详解(C++)

P1001 A+B Problem

#include <iostream>
using namespace std;

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

注意不要忘了定义变量名。

P1000 超级玛丽游戏

C语言实现

#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>

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

 用\可以使上下两句语句连续。

不用\可以用“”将一句话一句话括起来。

C++

#include <iostream>
using namespace std;

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

注意在双引号中是一个字符串,而\n是转义字符,不用另加' '号了

顺带一题,使用vs2019会带有自动补齐,按ctrl+z可以取消补齐。

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

#include <iostream>
using namespace std;

int main()
{
    int sum = 0;
    int n = 0;
    cin >> sum >> n;
    cout << sum * n << endl;
    return 0;
}

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

#include <iostream>
using namespace std;

int main()
{
    char ch = 0;
    cin >> ch;
    cout << char(ch-32) << endl;
    return 0;
}

C++中的强制类型转换有两种型势

1.(类型)变量名

2.类型(变量名)

ch-32后会变成ASCII码,再用char进制转换变回字母。

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

#include <cstdio>
using namespace std;

int main()
{
    char a = 0, b = 0, c = 0, d = 0;
    scanf("%c%c%c.%c", &a, &b, &c, &d);
    printf("%c.%c%c%c", d, c, b, a);
    
    return 0;
}
#include <cstdio>
using namespace std;

int main()
{
    int a = 0, b = 0, c = 0, d = 0;
    scanf("%1d%1d%1d.%1d", &a, &b, &c, &d);
    printf("%d.%d%d%d", d, c, b, a);
    
    return 0;
}

假设用%d格式符,那么输入123就会给a。

#include <iostream>
using namespace std;

int main()
{
    int a = 0;
    int b = 0;
    char ch = 0;
    cin >> a >> ch >> b;
    cout << b << ch;
    while(a)
    {
        cout << a % 10;
        a /= 10;
    }
    cout << '\n';
    
    return 0;
}

麻烦的写法。

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

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

int main()
{
    double t = 0.0;
    int n = 0;
    cin >> t >> n;
    cout << fixed << setprecision(3) << t/n << endl << n*2;
    return 0;
}

 fixed与setprecision连用,setprecision可以控制有效位数,而与fixed连用可以控制小数点后的位数,注意头文件<iomanip>(操纵器)。

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

int main()
{
    double t = 0.0;
    int n = 0;
    cin >> t >> n;
    printf("%.3lf\n%d\n", t/n, n*2);
    return 0;
}

这边可以看到这种带控制小数点位数的cout输出没有printf更加华丽!

P1425 小鱼的游泳时间

#include <iostream>
using namespace std;

int main()
{
    int a = 0, b = 0, c = 0, d = 0;
    cin >> a >> b >> c >> d;
    int start = a*60 + b;
    int end = c*60 + d;
    int delta = end - start;
    int e = 0, f = 0;
    e = delta / 60;
    f = delta % 60;
    cout << e << " " << f << endl;
    return 0;
}

在下思路

#include <iostream>
using namespace std;

int main()
{
    int a = 0, b = 0, c = 0, d = 0;
    cin >> a >> b >> c >> d;
    int e = c-a, f = d-b;
    if(f < 0)
    {
        e--;
        f += 60;
    }
    cout << e << " " << f << '\n';
    return 0;
}

同学的思路,注意此处e必定大于0,因为小时最小为1。

这边直接减的前提是在同一天。

P2433 【深基1-2】小学数学 N 合一

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

int main()
{
    int T = 0;
    cin >> T;
    if(T == 1)
    {
        cout << "I love Luogu!" << endl;
    }
    else if(T == 2)
    {
        cout << 2+4 << " " << 10-2-4 << endl;
    }
    else if(T == 3)
    {
        cout << 14/4 << '\n';
        cout << 14-14%4 << '\n';
        cout << 14%4 << '\n';
    }
    else if(T == 4)
    {
        cout << setprecision(6) << 500.0/3 << endl;
    }
    else if(T == 5)
    {
        cout << (220+260) / (20+12) << endl;
    }
    else if(T == 6)
    {
        cout << sqrt(pow(6, 2) + pow(9, 2)) << endl;
    }
    else if(T == 7)
    {
        int balance = 100;
        balance += 10;
        cout << balance << endl;
        balance -= 20;
        cout << balance << endl;
        balance = 0;
        cout << balance << endl;
    }
    else if(T == 8)
    {
        int r = 5;
        const double PI = 3.141593;
        cout << 2*PI*r << endl;
        cout << PI*pow(r, 2) << endl;
        cout << 4*PI*pow(r, 3) / 3 << endl;
    }
    else if(T == 9)
    {
        cout << (((1+1)*2+1)*2+1)*2 << endl;
    }
    else if(T == 10)
    {
        double inc_rate = (240.0-60) / (30-6);
        cout << (60-inc_rate*6 + 10*inc_rate) / 10;
    }
    else if(T == 11)
    {
        cout << 100.0 / 3 << endl;
    }
    else if(T == 12)
    {
        cout << 'M'-65+1 << endl << char('A'+18-1) << endl;
    }
    else if(T == 13)
    {
        const double PI = 3.141593;
        double sum = 4*PI*pow(4, 3) / 3 + 4*PI*pow(10, 3) / 3;
        int a = 0;
        a = pow(sum, 1.0/3);
        cout << a << endl;
    }
    else if(T == 14)
    {
        int i = 0;
        for(i=1; i<=110; i++)
        {
            if(i*(120-i) >= 3500)
            {
                cout << i << endl;
                break;
            }
        }
    }
    return 0;
}

 注意最后一题是要从价格最低的角度考虑的。

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

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

int main()
{
    double a = 0.0, b = 0.0, c = 0.0;
    double p = 0.0;
    double s = 0.0;
    cin >> a >> b >> c;
    p = (a+b+c) / 2;
    s = sqrt(p*(p-a)*(p-b)*(p-c));
    cout << fixed << setprecision(1) << s << endl;
    return 0;
}

 用printf更简单,cout练习一下小数点后的。

P1421 小玉买文具

#include <iostream>
using namespace std;

int main()
{
    int a = 0, b = 0;
    cin >> a >> b;
    cout << (a*10+b) / (19);
    
    return 0;
}

元转换成角

#include <iostream>
using namespace std;

int main()
{
    double a = 0.0, b = 0.0;
    cin >> a >> b;
    int c = (a+b/10) / 1.9;
    cout << c;
    
    return 0;
}

角转换成元,注意两个浮点数相除结果是浮点数,所以要先放入整型的数字当中。

 P5709 【深基2.习6】Apples Prologue / 苹果和虫子

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

int main()
{
    int m = 0, t = 0, s = 0;
    cin >> m >> t >> s;
    if(t == 0)
    {
        cout << 0 << endl;
    }
    else if(s % t == 0)
    {
        cout << max(m - s/t, 0) << endl;
    }
    else
    {
        cout << max(m - s/t - 1, 0) << endl;
    }

    return 0;
}

这题坑很多,首先是除数为0时的情况,不需要时间就能吃苹果,那s是0就可以吃完所以苹果。

2是判断是否有余数,有余数则说明拿着苹果在啃呢,注意一定是判断余数!

3是s的时间是不是很长,能把苹果吃完,如果个数<0,则没有苹果了。

max()这个函数的头文件:<algorithm>(算法)

二法

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

int main()
{
    int m = 0, t = 0, s = 0;
    cin >> m >> t >> s;
    if(t == 0)
    {
        cout << 0;
        return 0;
    }
    int sur_num = m - ceil((double)s/t);
    if(sur_num < 0)
    {
        cout << 0;
    }
    else
    {
        cout << sur_num;
    }
    return 0;
}

关于二法,我犯了几个愚蠢的错误,1是我竟用m与0比较,m是相对不变的量,应该是剩余的苹果数是否<0(吃完了)或者=0(正好吃完)。

2是强制类型转换的错误,我们看看两者区别1(double)(s/t) 和2(double)s/t,1是s和t先进行整型运算再转换,2是s先转换成浮点型再进行浮点型运算。

P2181 对角线 

#include <iostream>
using namespace std;

int main()
{
    unsigned long long n = 0;
    cin >> n;
    unsigned long long count = n*(n-1)/2 * (n-2)/3 * (n-3)/4;
    cout << count << endl;
    return 0;
}

 本题n最大为100000,再相乘会是一个很大的非负整数,所以得用无符号的long long来划分内存空间。

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

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

int main()
{
    int s = 0, v = 0;
    cin >> s >> v;
    int t = ceil(s*1.0/v) + 10;//所需要的时间
    int sum = 8*60 + 24*60;//总时间
    sum -= t;//要出发的时间
    if(sum >= 24*60)
    {
        sum -= 24*60;
    }
    int h = sum / 60;
    int m = sum % 60;
    if(h < 10)
    {
        cout << 0 << h << ":";
    }
    else
    {
        cout << h << ":";
    }
    if(m < 10)
    {
        cout << 0 << m;
    }
    else
    {
        cout << m;
    }
    return 0;
}

 这题要考虑是否要所需要的时间是否超过八小时考虑。如果要出发的时间超过了24小时则说明所需要的时间小于8小时。

二法

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

int main()
{
    int s = 0, v = 0;
    cin >> s >> v;
    int t = ceil(s*1.0/v) + 10;//所需要的时间
    int sum = 8*60 + 24*60;//总时间
    sum -= t;//要出发的时间
    if(sum >= 24*60)
    {
        sum -= 24*60;
    }
    int h = sum / 60;
    int m = sum % 60;
    if(h < 10)
    {
        if(m < 10)
        {
            cout << 0 << h << ":0" << m; 
        }
        else
        {
            cout << 0 << h << ":" << m;
        }
    }
    else
    {
        if(m < 10)
        {
            cout << h << ":0" << m;
        }
        else
        {
            cout << h << ":" << m;
        }
    }
    
    return 0;
}

三法

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

int main()
{
    int s = 0, v = 0;
    cin >> s >> v;
    int t = ceil(s*1.0/v) + 10;//所需要的时间
    int sum = 8*60 + 24*60;//总时间
    sum -= t;//要出发的时间
    if(sum >= 24*60)
    {
        sum -= 24*60;
    }
    int h = sum / 60;
    int m = sum % 60;
    printf("%02d:%02d\n", h, m);
    
    return 0;
}

可以看到这里用printf非常清爽~

 P3954 [NOIP2017 普及组] 成绩

#include <iostream>
using namespace std;

int main()
{
    int A = 0, B = 0, C = 0;
    cin >> A >> B >> C;
    cout << A*0.2 + B*0.3 + C*0.5 << endl;
    return 0;
}

我用C语言犯了一个有趣的错误

先放正确的

#include <stdio.h>

int main()
{
    int a = 0, b = 0, c = 0;
    scanf("%d %d %d", &a, &b, &c);
    printf("%.0lf\n", a*0.2 + b*0.3 + c*0.5);
    
    return 0;
}

我把%.0lf弄成了%d,浮点数在内存中的存储与整型的不同,所以出错了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值