2.2 基本算法之递归和自调用函数 8758 2的幂次方表示 python
生活随笔
收集整理的這篇文章主要介紹了
2.2 基本算法之递归和自调用函数 8758 2的幂次方表示 python
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
http://noi.openjudge.cn/ch0202/8758/
""" 2.2 基本算法之遞歸和自調用函數 8758 2的冪次方表示 python http://noi.openjudge.cn/ch0202/8758/""" def calculate(n,step):if n==0:returncalculate(n//2,step+1)if n%2==1:if n//2!=0:print("+",end='')if step==1:print("2",end='')else:print("2(",end='')if step==0:print("0",end='')else:calculate(step,0)print(")",end='')n=int(input()) calculate(n,0)C++代碼:
/* 2.2 基本算法之遞歸和自調用函數 8758 2的冪次方表示 http://noi.openjudge.cn/ch0202/8758/*/ #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring>#define N 1000010using namespace std; void calculate(int n,int step) {if(n==0)return;calculate(n/2,step+1);if(n%2){if(n/2)cout<<"+";if(step==1)cout<<"2";else{cout<<"2(";if(step==0)cout<<"0";elsecalculate(step,0);cout<<")";}} } int main() {int n;cin>>n;calculate(n,0);cout<<endl;return 0; }總結
以上是生活随笔為你收集整理的2.2 基本算法之递归和自调用函数 8758 2的幂次方表示 python的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小学奥数 7654 等差数列末项计算 p
- 下一篇: 1.9 编程基础之顺序查找 02 输出最