题目描述:
这道题的标签是数学类题目,所以不是很难,需要理清题目意思以及代码逻辑就会很简单。无非就是除和取余运算的融合。不想细讲,直接看代码,然后你自己手撕代码。
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> NUM;
int n;
int num = 0;
int i = 0;
cin >> n;
while (n)
{
num = 0;
while (n != 1)
{
num += n / 3;
n = n % 3 + n / 3;
if (n == 2)
{
num += 1;
i++;
break;
}
}
NUM.push_back(num);
cin >> n;
}
vector<int>::iterator it;
for (it = NUM.begin(); it != NUM.end(); it++)
{
cout << *it << endl;
}
};