每日打卡心情好

· · 题解

【注意点】

【解题思路】

  1. 先把连续的1和0统计出来。
  2. 最后再进行计算。

【带你写代码】

  1. 搭框架
#include<vector>
#include<iostream>
using namespace std;
int points = 0,consecutive_days = 0;
vector<int> a;
//主代码
int main() {
    int n,x;
    cin >> n;
    for(int i = 0; i<n; i++) {//计算连续天数
        cin >> x;

    }
    while(!a.empty()){
        //计算分数
    }
    cout << points;
    return 0;
}
  1. 计算连续天数
#include<vector>
#include<iostream>
using namespace std;
int points = 0,consecutive_days = 0;
vector<int> a;
//主代码
int main() {
    int n,x;
    cin >> n;
    for(int i = 0; i<n; i++) {//计算连续天数
        cin >> x;
        if(a.empty()) {//连续的0用负数表示,连续的1用正数表示
            if(x==0) a.push_back(-1);
            if(x==1) a.push_back(1);
        } else {
            if(x==0) {
                if(a.back()<0) {
                    int q = a.back();
                    a.pop_back();
                    a.push_back(q-1);
                } else {
                    a.push_back(-1);
                }
            } else {
                if(a.back()>0) {
                    int q = a.back();
                    a.pop_back();
                    a.push_back(q+1);
                } else {
                    a.push_back(1);
                }
            }
        }
    }
    while(!a.empty()){
        //计算分数
    }
    cout << points;
    return 0;
}
  1. 计算分数
#include<vector>
#include<iostream>
using namespace std;
int points = 0,consecutive_days = 0;
vector<int> a;
//主代码
int main() {
    int n,x;
    cin >> n;
    for(int i = 0; i<n; i++) {//计算连续天数
        cin >> x;
        if(a.empty()) {//连续的0用负数表示,连续的1用正数表示
            if(x==0) a.push_back(-1);
            if(x==1) a.push_back(1);
        } else {
            if(x==0) {
                if(a.back()<0) {
                    int q = a.back();
                    a.pop_back();
                    a.push_back(q-1);
                } else {
                    a.push_back(-1);
                }
            } else {
                if(a.back()>0) {
                    int q = a.back();
                    a.pop_back();
                    a.push_back(q+1);
                } else {
                    a.push_back(1);
                }
            }
        }
    }
    while(!a.empty()){
        if(a.front()>=0) consecutive_days+=a.front();//计算分数
        change(consecutive_days);
        if(points<=0) points = 0;
        if(consecutive_days<=0) consecutive_days = 0;
        a.erase(a.begin());
    }
    cout << points;
    return 0;
}
  1. 主程序
#include<vector>
#include<iostream>
using namespace std;
int points = 0,consecutive_days = 0;
vector<int> a;
int earnv(int x){//主程序
    if(x>=365) return 6;//(一年四季都坚持下来了!真不容易!)
    if(x>=120) return 5;//(坚持四个月了!)
    if(x>=30) return 4;//(月圆月缺,习惯养成)
    if(x>=7) return 3;//(曜日轮回)
    if(x>=3) return 2;//(坚持 3 天了,加油!)
    if(x>=1) return 1;//(千里之行,始于足下)
}
int earn(int x){
    int ret = 0;
    for(int i = x-a.front()+1;i<=x;i++){
        ret+=earnv(i);
    }
    return ret;
}
int pow(int n,int m){
    if(n==0) return 0;
    if(m==0) return 1;
    return n*pow(n,m-1);
}
int cost(int x){
    return pow(2,(x*(-1))-1)*(-1);
}
void change(int x){
    if(a.front()>0) points+=earn(x);
    else consecutive_days+=cost(a.front());
}
int main() {
    int n,x;
    cin >> n;
    for(int i = 0; i<n; i++) {//计算连续天数
        cin >> x;
        if(a.empty()) {//连续的0用负数表示,连续的1用正数表示
            if(x==0) a.push_back(-1);
            if(x==1) a.push_back(1);
        } else {
            if(x==0) {
                if(a.back()<0) {
                    int q = a.back();
                    a.pop_back();
                    a.push_back(q-1);
                } else {
                    a.push_back(-1);
                }
            } else {
                if(a.back()>0) {
                    int q = a.back();
                    a.pop_back();
                    a.push_back(q+1);
                } else {
                    a.push_back(1);
                }
            }
        }
    }
    while(!a.empty()){
        if(a.front()>=0) consecutive_days+=a.front();//计算分数
        change(consecutive_days);
        if(points<=0) points = 0;
        if(consecutive_days<=0) consecutive_days = 0;
        a.erase(a.begin());
    }
    cout << points;
    return 0;
}

【AC代码】(干净整洁无注释)

#include<vector>
#include<iostream>
using namespace std;
int points = 0,consecutive_days = 0;
vector<int> a;
int earnv(int x){
    if(x>=365) return 6;//(一年四季都坚持下来了!真不容易!)
    if(x>=120) return 5;//(坚持四个月了!)
    if(x>=30) return 4;//(月圆月缺,习惯养成)
    if(x>=7) return 3;//(曜日轮回)
    if(x>=3) return 2;//(坚持 3 天了,加油!)
    if(x>=1) return 1;//(千里之行,始于足下)
}
int earn(int x){
    int ret = 0;
    for(int i = x-a.front()+1;i<=x;i++){
        ret+=earnv(i);
    }
    return ret;
}
int pow(int n,int m){
    if(n==0) return 0;
    if(m==0) return 1;
    return n*pow(n,m-1);
}
int cost(int x){
    return pow(2,(x*(-1))-1)*(-1);
}
void change(int x){
    if(a.front()>0) points+=earn(x);
    else consecutive_days+=cost(a.front());
}
int main() {
    int n,x;
    cin >> n;
    for(int i = 0; i<n; i++) {
        cin >> x;
        if(a.empty()) {
            if(x==0) a.push_back(-1);
            if(x==1) a.push_back(1);
        } else {
            if(x==0) {
                if(a.back()<0) {
                    int q = a.back();
                    a.pop_back();
                    a.push_back(q-1);
                } else {
                    a.push_back(-1);
                }
            } else {
                if(a.back()>0) {
                    int q = a.back();
                    a.pop_back();
                    a.push_back(q+1);
                } else {
                    a.push_back(1);
                }
            }
        }
    }
    while(!a.empty()){
        if(a.front()>=0) consecutive_days+=a.front();
        change(consecutive_days);
        if(points<=0) points = 0;
        if(consecutive_days<=0) consecutive_days = 0;
        a.erase(a.begin());
    }
    cout << points;
    return 0;
}

【AC代码】(有注释)

#include<vector>
#include<iostream>
using namespace std;
int points = 0,consecutive_days = 0;
vector<int> a;
int earnv(int x){//主程序
    if(x>=365) return 6;//(一年四季都坚持下来了!真不容易!)
    if(x>=120) return 5;//(坚持四个月了!)
    if(x>=30) return 4;//(月圆月缺,习惯养成)
    if(x>=7) return 3;//(曜日轮回)
    if(x>=3) return 2;//(坚持 3 天了,加油!)
    if(x>=1) return 1;//(千里之行,始于足下)
}
int earn(int x){
    int ret = 0;
    for(int i = x-a.front()+1;i<=x;i++){
        ret+=earnv(i);
    }
    return ret;
}
int pow(int n,int m){
    if(n==0) return 0;
    if(m==0) return 1;
    return n*pow(n,m-1);
}
int cost(int x){
    return pow(2,(x*(-1))-1)*(-1);
}
void change(int x){
    if(a.front()>0) points+=earn(x);
    else consecutive_days+=cost(a.front());
}
int main() {
    int n,x;
    cin >> n;
    for(int i = 0; i<n; i++) {//计算连续天数
        cin >> x;
        if(a.empty()) {//连续的0用负数表示,连续的1用正数表示
            if(x==0) a.push_back(-1);
            if(x==1) a.push_back(1);
        } else {
            if(x==0) {
                if(a.back()<0) {
                    int q = a.back();
                    a.pop_back();
                    a.push_back(q-1);
                } else {
                    a.push_back(-1);
                }
            } else {
                if(a.back()>0) {
                    int q = a.back();
                    a.pop_back();
                    a.push_back(q+1);
                } else {
                    a.push_back(1);
                }
            }
        }
    }
    while(!a.empty()){
        if(a.front()>=0) consecutive_days+=a.front();//计算分数
        change(consecutive_days);
        if(points<=0) points = 0;
        if(consecutive_days<=0) consecutive_days = 0;
        a.erase(a.begin());
    }
    cout << points;
    return 0;
}

小彩蛋(AC记录)