题目描述:
- 思路: 倒叙查找,如N=10,则最后一个扭蛋肯定是偶数,即
- 4<–(3)–10
- 1<–(3)–4
- 0<–(2)–1
- 所以最后输出应该是(2)(3)(3)即233
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int N;
// int x=0; //刚开始没有扭蛋,x初值为0,故N的最小值为1
vector<int> array; //定义未知长度数组
cout<<"Please enter the value of the positive integer N:"<<endl;
cin>>N;
for(int i=0;N>=1;i++)
{
if(N==1){
array.push_back(2);
break;
}
else if(N>0 && N%2==0) // N为偶数,说明最后是由3扭到的
{
array.push_back(3);
N=(N-2)/2;
}
else if(N>0 && N%2==1)// N为奇数
{
array.push_back(2);
N=(N-1)/2;
}
}
for(int j=0; j<array.size(); j++)
{
cout<<array[(array.size()-1-j)]<<""; // 倒叙输出array的值
}
cout<<endl;
return 0;
}