Wunder Fund Round 2016 (Div. 1 + Div. 2 combined)--A. Slime Combining

A. Slime Combining
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Your friend recently gave you some slimes for your birthday. You have n slimes all initially with value 1.

You are going to play a game with these slimes. Initially, you put a single slime by itself in a row. Then, you will add the other n - 1 slimes one by one. When you add a slime, you place it at the right of all already placed slimes. Then, while the last two slimes in the row have the same value v, you combine them together to create a slime with value v + 1.

You would like to see what the final state of the row is after you've added all n slimes. Please print the values of the slimes in the row from left to right.

Input

The first line of the input will contain a single integer, n (1 ≤ n ≤ 100 000).

Output

Output a single line with k integers, where k is the number of slimes in the row after you've finished the procedure described in the problem statement. The i-th of these numbers should be the value of the i-th slime from the left.

Sample test(s)
input
1
output
1
input
2
output
2
input
3
output
2 1
input
8
output
4

Note

大体题意:

给你n个泥巴,每个泥巴初始值为1,当最右边两个泥巴值一样的话,就可以合在一起,并且值加1,问最后排完之后,从左到右依次输出有的泥巴,并且输出他们的值!


方法1:(数学规律)

先自己随便写几个n,慢慢就会看出:泥巴和n有关系,正好对应n的二进制为1的下标,比如说11吧 结果应该是4 2 1,他的二进制是1011,   1,2,4位置上正好是1.

因此:代码如下:

#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<sstream>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;
const int maxn = 1000 + 10;
const int maxt = 100 + 10;
const double eps = 1e-8;
int main()
{
    ios::sync_with_stdio(false);
    int n;
    while(cin >> n){
        for (int i = 31; i > -1; --i)
            if (n & (1 << i))cout << i + 1 << " ";
    cout << endl;
    }
    return 0;
}

方法2:(栈模拟)

很明显,这个游戏规则类似于栈:不断往里放,只判断栈顶,和第二个元素。

放一个元素就check一次,因为放的话必然有两个或以上的元素,一个元素只能出现在循环入栈的时候,所以在check里面可以先放,放完了发现size  > 1就在check不断递归!不过有个缺点,没法遍历栈,我这里用了vector,还以为会超时,,结果过了!


#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<sstream>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;
const int maxn = 1000 + 10;
const int maxt = 100 + 10;
const double eps = 1e-8;
stack<int>s;
void check(){
    int top1=s.top();s.pop();
    int top2=s.top();s.pop();
    if(top1==top2){
        s.push(top1+1);
        if (s.size() > 1)check();
    }
    else {s.push(top2);s.push(top1);}
}
int main()
{
    ios::sync_with_stdio(false);
    int n;
    while(cin >> n){

        s.push(1);
        for (int i = 0; i < n-1;++i){
            s.push(1);
            check();
        }
        vector<int>v;
        while(!s.empty()){v.push_back(s.top());s.pop();}
        for (int i = v.size()-1; i > -1; --i)cout << v[i] << " ";
        cout<<endl;
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值