zoukankan      html  css  js  c++  java
  • 2的幂次方(power)

    2的幂次方(power)

    题目描述

    任何一个正整数都可以用2的幂次方表示。例如:
    137=27+23+20
    同时约定方次用括号来表示,即ab 可表示为a(b)。
    由此可知,137可表示为:
    2(7)+2(3)+2(0)
    进一步:7=22+2+20 (21用2表示)
    3=2+20
    所以最后137可表示为:
    2(2(2)+2+2(0))+2(2+2(0))+2(0)
    又如:
    1315=210 +28 +25 +2+1
    所以1315最后可表示为:
    2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)

    输入

    输入:正整数(n≤20000)

    输出

    输出:符合约定的n的0,2表示(在表示中不能有空格)

    样例输入

    137
    1315
    

    样例输出

    2(2(2)+2+2(0))+2(2+2(0))+2(0)
    2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)
    分析:递归
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #include <ext/rope>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define vi vector<int>
    #define pii pair<int,int>
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    const int maxn=1e5+10;
    const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
    using namespace std;
    using namespace __gnu_cxx;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    int n,m;
    void work(int now)
    {
        if(now==1){printf("2(0)");return;}
        else if(now==2){printf("2");return;}
        int p=1,q=0;
        while(p<=now)q++,p<<=1;
        q--,p>>=1;
        if(p==now)
        {
            printf("2(");
            work(q);
            printf(")");
        }
        else
        {
            if(p==2)printf("2+");
            else
            {
                printf("2(");
                work(q);
                printf(")+");
            }
            work(now-p);
        }
    }
    int main()
    {
        int i,j,k,t;
        while(~scanf("%d",&n))
        {
            work(n);
            printf("
    ");
        }
        //system ("pause");
        return 0;
    }
     
  • 相关阅读:
    如何配置寄存器
    逻辑分析仪的使用
    CAN中如何计算波特率并配置波特率
    led不同颜色的驱动电压和驱动电流
    ULINK2配置
    电机加减速转动
    2N7002
    未添加时钟文件产生报错
    烟雾传感器
    Strategy 设计模式 策略模式 超靠谱原代码讲解
  • 原文地址:https://www.cnblogs.com/dyzll/p/5662110.html
Copyright © 2011-2022 走看看