在平行的洛谷世界,有 A,B,C 三座城市,它们构成了洛谷三角洲。
两两城市之间各有一条双向道路。也就是说,一共有三条道路,A 市与 B 市之间有一条道路,A 市与 C 市之间有一条道路,B 市与 C 市之间有一条道路。
由于每条道路的拥堵程度不同,因此通过一条路需要的时间也不同。对于每一条路,双向的拥挤程度相同(即经过 A 市与 B 市之间的一条道路,由 A 市到 B 市和由 B 市到 A 市所需的时间相同)。
通过 A 市与 B 市之间的道路需要 x 分钟,通过 B 市与 C 市之间的道路需要 y 分钟,通过 C 市与 A 市之间的道路需要 z 分钟。
由于业务需要,洛谷站长需要在三个市之间频繁往返。如果能够知道任意两个市之间通行的最短时间,那么他能够极大地提高工作效率。
所以他现在想知道:从 A 市到 B 市,从 B 市到 C 市,从 A 市到 C 市分别至少需要多少时间(单位:分钟)
少打一个换行符。
if (y + z > x){
cout << x << "\n";
}
else{
cout << y + z << "\n";
}
if (x + z > y){
cout << y << "\n";
}
else{
cout << x + z << "\n";
}
if (x + y > z){
cout << z;
}
else{
cout << x + y;
}
#include<bits/stdc++.h>
using namespace std;
long long x, y, z;
int main(){
cin >> x >> y >> z;
if (y + z > x){
cout << x << "\n";
}
else{
cout << y + z << "\n";
}
if (x + z > y){
cout << y << "\n";
}
else{
cout << x + z << "\n";
}
if (x + y > z){
cout << z;
}
else{
cout << x + y;
}
return 0;
}
有一只小鱼,它平日每天游泳 250 公里,周末休息(实行双休日),假设从周 x 开始算起,过了 n 天以后,小鱼一共累计游泳了多少公里呢?
没有考虑周六。
直接用x来存储星期几,只要不是周末就加250,x加一,周天时将x设为一。
for (int i = 1; i <= n; i++){
if (x != 6 && x != 7){
sum += 250;
}
if (x == 7){
x = 1;
}
else{
x++;
}
}
#include<bits/stdc++.h>
using namespace std;
long long x, n, sum;
int main(){
cin >> x >> n;
for (int i = 1; i <= n; i++){
if (x != 6 && x != 7){
sum += 250;
}
if (x == 7){
x = 1;
}
else{
x++;
}
}
cout << sum;
return 0;
}
津津上高中了。她在自己的妈妈的魔鬼训练下,成为了一个神犇,每次参加一次 OI 比赛必拿 Au 虐全场。每次她拿到一个 Au 后就很高兴。假设津津不会因为其它事高兴,并且她的高兴会持续 T 天(包含获奖当天。就算在高兴的时候再次拿到 Au,他的高兴也只能维持包括这次拿奖之日起 T 天,而不是额外增加 T 天的高兴时间,除非之后再拿奖)。请你帮忙检查一下津津接下来的的日程安排,要参加 n 场比赛,看看接下来的几天,津津会累计开心多久?
数组开小了,没有统计最后一天。
由于最后一天一定能开心满t天,所以ans初始化为t。
ans = t;
所以循环要从1到n - 1,每次循环判断这个金牌是否能开心满t天,如果能,ans加t,如果不能,ans加a[i + 1] - a[i]。
for (int i = 1; i < n; i++){
if (a[i + 1] - a[i] >= t){
ans += t;
}
else{
ans += a[i + 1] - a[i];
}
}
#include<bits/stdc++.h>
using namespace std;
long long n, t, ans, a[200005];
int main(){
cin >> n >> t;
ans = t;
for (int i = 1; i <= n; i++){
cin >> a[i];
}
for (int i = 1; i < n; i++){
if (a[i + 1] - a[i] >= t){
ans += t;
}
else{
ans += a[i + 1] - a[i];
}
}
cout << ans;
return 0;
}
小杨发现了 n 个宝箱,其中第 i 个宝箱的价值是 a[i],小杨可以选择一些宝箱放入背包并带走,但是小杨的背包比较特殊,假设小杨选择的宝箱中最大价值为 x,最小价值为 y,小杨需要保证 x−y≤k,否则小杨的背包会损坏。小杨想知道背包不损坏的情况下,自己能够带走宝箱的总价值最大是多少。
先对宝箱进行从小到大排序,再求出前缀和:
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; i++){
sum[i] = sum[i - 1] + a[i];
}
再使用双重循环枚举最大与最小值,如果符合要求,就算出对应的价值,并打擂台:
for (int i = 1; i <= n; i++){
for (int j = i; j <= n; j++){
if (a[j] - a[i] <= k){
long long res = sum[j] - sum[i - 1];
ans = max(ans, res);
}
}
}
#include<bits/stdc++.h>
using namespace std;
long long n, k, ans, a[1005], sum[1005];
int main(){
cin >> n >> k;
for (int i = 1; i <= n; i++){
cin >> a[i];
}
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; i++){
sum[i] = sum[i - 1] + a[i];
}
for (int i = 1; i <= n; i++){
for (int j = i; j <= n; j++){
if (a[j] - a[i] <= k){
long long res = sum[j] - sum[i - 1];
ans = max(ans, res);
}
}
}
cout << ans;
return 0;
}