原题题目

在这里插入图片描述


代码实现(首刷自解)

class Solution {
public:
    int totalMoney(int n) {
        int count = 0,ret = 0,start = 1;
        int pre = 21;
        while(count < n)
        {
            int temp = (n-count <= 7 ? n-count : 7);
            count += temp;
            if(temp == 7)
            {
                ret += (pre+7);
                pre += 7;
            }
            else    ret += (start + (start+temp-1))*temp/2;
            ++start;
        }
        return ret;
    }
};

更多推荐