洛谷p101

这是一道#数学#分治的题目,小白的我第一次接触,开始想用STL容器设计,但是最后不尽人意,后来仔细看才发现,这是一道考察迭代算法的题目,于是,写法就很简单了:

#include <iostream>
#include <cmath>
using namespace std;
int print(int n) {
    while (n) {
        int i = 0;
        while (pow(2, i) <= n)i++;
        int mark = i - 1;
        n -= pow(2, mark);
        if (mark / 2) {
            cout << 2 << "(";
            print(mark);
            cout << ")" << "+";
            continue;
        }
        if (mark == 1) {
            cout << 2 << "+";continue;
        }
        cout << 2 << "(" << mark << ")" << "+";
    }
    cout << '\b';
    return 1;
}
int main() {
    int number = 0;
    cin >> number;
    print(number);
    cout << ' ';
    return 0;
}

点击,运行!零AC,e。。。然后再看题目一眼:

 

好吧,我是呆瓜。然后又做了修改,以下是最后代码:

 

#include <iostream>
#include <cmath>
using namespace std;
void fun(int n){
    if(n)cout<<"+";
}
int print(int n){
    while(n){
        int i=0;
        while(pow(2,i)<=n)i++;
        int mark=i-1;
        n-=pow(2,mark);
        if(mark/2){cout<<2<<"(";
			 print(mark);
			 cout<<")";
             fun(n);
			 continue;
        }
        if(mark==1){
		cout<<2;fun(n);continue; }
        cout<<2<<"("<< mark <<")";
        fun(n);
    }
    return 1;
}
int main(){
    int number=0;
    cin>>number;
    print(number);
    return 0;
}

结束。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值