洛谷AT886(Atcoder 886)

由 5+1 发布

题意

题目

stove,他是一个矿工,他现在有7天挖矿时间

然后他有两个地方可以挖矿,沙漠的矿山和平原的矿山

第一行,输入7个数代表再第i天如果在沙漠的矿山拿到的工资

第二行,输入7个数代表再第i天如果在平原的矿山拿到的工资

问stove最多可以赚多少钱?

题目再翻译

stove有7天,他每天都可以去一个地方挖矿。

样例输入

第一行: 输入7个数代表第 i 天1号矿井的工资

第二行: 输入7个数代表第 i 天2号矿井的工资

样例输出

一行: stove最多可以赚取多少钱

思路

在每一天的工资中取最大值,并累加到 sum 中,再输出sum

实际操作

思路是不错的,可是脑子不够快!可能是当时有点仓促,结果 WA 咧!

面对 WA 最有效的方法就是重写一遍!

就这样,我又写了一遍……

AC了!

预处理

a是存储1号矿井的工资的,b是存储2号矿井的工资,sum是累加的总工资

int a[8],b[8],sum=0;

输入处理

for(int i=1;i<8;i++){
    cin>>a[i];
}
for(int i=1;i<8;i++){
    cin>>b[i];
}

计算最大工资

求每天的1号矿井的工资与二号矿井的工资的最大值

for(int i=1;i<8;i++){
    sum+=max(a[i],b[i]);
}

提示

比大小函数最好自己写。

#define max(a,b) (((a)>(b))?(a):(b))

不完全等价于

int max(int a,int b){
    return a>b?a:b;
}

define的可以支持所有类型的数;
max只能是单一类型,并且define比max速度快!

完整代码

#include<iostream>
using namespace std;
#define max(a,b) (((a)>(b))?(a):(b))
int a[8],b[8],sum=0;
int main(){
    for(int i=1;i<8;i++){
        cin>>a[i];
    }
    for(int i=1;i<8;i++){
        cin>>b[i];
    }
    for(int i=1;i<8;i++){
        sum+=max(a[i],b[i]);
    }
    cout<<sum<<endl;
    return 0;
}

2 条评论

  1. 623
    623 · 2022-12-27 20:35

    Heyy there! I know this is kinda ooff toppic but I waas wondering
    whih blog platform are yyou using for thhis site? I'm gettying fed up off Wordpress because I've had problems
    with hackers andd I'm looking at alternatives for another platform.

    I would be fantastic if you could point me in thee directiion oof a gopd platform.

    1. 5+1
      5+1 回复 623 · 2022-12-27 22:17 作者

      Hi, I'm glad to see you here!

      I'm using typecho http://typecho.org. It is an open-source project made by Chinese and you can get it from GitHub https://github.com/typecho/typecho.

      This blog platform needs a web server where you can run PHP and mySQL in.

发表评论