小鱼的航程

题目描述

有一只小鱼,它上午游泳150公里,下午游泳100公里,晚上和周末都休息(实行双休日),假设从周x(1<=x<=7)开始算起,请问这样过了n天以后,小鱼一共累计游泳了多少公里呢?

输入输出格式

输入格式:

输入两个整数x,n(表示从周x算起,经过n天,n在long int范围内)。

输出格式:

输出一个整数,表示小鱼累计游泳了多少公里。

输入:3 10 输出:2000

解法1:很好的if...else用法

#include<cstdio>
using namespace std;
int main()
{
    int x;
    unsigned long long n,sum=0;  //题目的坑 按题目要求做
    scanf("%d %llu",&x,&n);
    for(int i=0;i<n;i++){  //循环次数
        if(x!=6&&x!=7)
            sum+=250;
        if(x==7)
            x=1;
        else
            x++;
    }
    printf("%llu",sum);
}

解法2:巧用数组来代表各个位置的值。

#include <cstdio>
using namespace std;
#define N 7
int main(){
    int a[N]={0,250,250,250,250,250,0},x;
    unsigned long long n,s=0;
    scanf("%d%llu",&x,&n);
    for(unsigned long long i=1;i<=n;i++){
        x=x%7;
        s+=a[x];
        x++;
    }
    printf("%llu",s);
}
原文地址:https://www.cnblogs.com/litifeng/p/10389459.html