Fork me on GitHub

题目1095:2的幂次方(递归)

题目链接:http://ac.jobdu.com/problem.php?pid=1095

详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus

参考代码:

//
//  1095 2的幂次方.cpp
//  Jobdu
//
//  Created by PengFei_Zheng on 05/05/2017.
//  Copyright © 2017 PengFei_Zheng. All rights reserved.
//
 
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstring>
#include <cmath>
#include <climits>
 
using namespace std;
 
 
void print(int n){
    if(n==1) return;
    if(n==0){
        printf("0");
        return;
    }
    bool first = true;
    for(int i = 31 ; i >= 0 ; i--){
        if(((n>>i)&1)==1){//find the hightest pos
            if(first){
                first = false;
            }
            else {
                printf("+");
            }
            printf("2");
            if(i!=1){
                printf("(");
            }
            print(i);//not until i==1 or i==0
            if(i!=1){
                printf(")");
            }
        }
    }
}
int n;
int main(){
    while(scanf("%d",&n)!=EOF){
        print(n);
        printf("\n");
    }
    return 0;
}
/**************************************************************
    Problem: 1095
    User: zpfbuaa
    Language: C++
    Result: Accepted
    Time:10 ms
    Memory:1520 kb
****************************************************************/

 

posted @ 2017-05-05 14:33  伊甸一点  阅读(253)  评论(0编辑  收藏  举报