题解59

· · 个人记录

金币

题意分析

一个骑士领金币,有一些阶段,每个阶段连续 i 天每天发 i 枚金币。给定总天数 k,按该规则累加,求出前 k 天骑士总共得到的金币数量。

正确思路

循环枚举阶段数,不断累加该阶段总天数,未超出 k 就累加金币,超出则处理剩余天数,统计全部金币并输出结果。

正确代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,ans=0;
signed main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin>>n;
    for(int i=1;;i++){
        for(int j=1;j<=i;j++){
            ans+=i;//此次
            n--;//总天数减1
            if(n==0){
                cout<<ans;//过了n天的总金币数
                return 0;
            }
        }
    }
    return 0;
}

小木棍

题意分析

给定一个自然数 n,使这 n 根木棍能摆出最小得数(不能有前导 0)。

正确思路

数字分别对应:

0 1 2 3 4 5 6 7 8 9
6 2 5 5 4 5 6 3 7 6

最好是位数最少,用的小木棍越多,位数就会少,其中‘8’要用7根小木棍,所以只要剩下的小木棍数大于等于7,就可以拼成8,而其余的小木棍就可以拼成当下最少的数,但是剩余3或4的情况不一样,因为如果n原来本身就小于7,就不能叫做剩余,要做特判。

正确代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
signed main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        cin>>n;
        if(n<2){
            cout<<-1<<'\n';
            continue;
        }
        if(n%7==0){//各种情况
            for(int i=1;i<=n/7;i++){
                cout<<8;
            }
        }else if(n%7==1){
            cout<<10;
            for(int i=1;i<n/7;i++){
                cout<<8;
            }
        }else if(n%7==2){
            cout<<1;
            for(int i=1;i<=n/7;i++){
                cout<<8;
            }
        }else if(n%7==3){
            if(n/7>=2){
                cout<<200;
                for(int i=1;i<n/7-1;i++){
                    cout<<8;
                }
            }
            else if(n==3){//小于7就只能输出一位数
                cout<<7;
            }else{
                cout<<22;
                for(int i=1;i<n/7;i++){
                    cout<<8;
                }
            }
        }else if(n%7==4){
            if(n==4){
                cout<<4;
            }else{
                cout<<20;
                for(int i=1;i<n/7;i++){
                    cout<<8;
                }
            }
        }else if(n%7==5){
            cout<<2;
            for(int i=1;i<=n/7;i++){
                cout<<8;
            }
        }else if(n%7==6){
            cout<<6;
            for(int i=1;i<=n/7;i++){
                cout<<8;
            }
        }
        cout<<'\n';
    }
    return 0;
}

小苹果

题意分析

一共有n个红润多汁儿的小苹果,有一位名叫小爆的入,他有个癖好->每天从最左边开始,隔两个苹果拿一个,问拿完苹果要几天,第n个苹果第几天拿走的。

正确思路

首先我们可以找其中的规律,每次都是拿走3的倍数+1,知道规律后,就可以便利直到全部拿走。

正确代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,ans=-1,cs=1;
signed main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n;
    while(n>0){
        if(n%3==1 && ans==-1){
            ans=cs;//编号为n的苹果被吃了
        }
        n-=(n+2)/3;//苹果被小屁孩拿走了
        cs++;
    }
    cout<<cs-1<<' '<<ans;
    return 0;
}