下面的程序采用回溯法,即对每一个系数的可能情况(0, 1, ..., abs(r)-1)进行枚举,如果计算结果与n相同,则得到结果。
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std; int n, r, x[20] = {0}, bits; char bit[]="01234567ABCDEFGHIJKLMNOPQRSTUVWXYZ"; int pow2(int n, int e){ int ans = 1, tmp; if(n==0) return 1; else if(n==1) return e; if(n%2){ tmp = pow2(n/2, e); return e*tmp*tmp; } else { tmp = pow2(n/2, e); return tmp*tmp; } } int max_len(){ int i = 0; while(1){ if(pow2(i, abs(r))> abs(n)) return i+1; i++; } } void dfs(int k, int m){ int i, j; if(m==n){ for(i=bits; i>=0; i--) if(x[i]!=0) break; cout<
<<'='; for(j=i; j>=0; j--) cout<
>n>>r; bits = max_len(); dfs(0, 0); return 0; }
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- niushuan.com 版权所有 赣ICP备2024042780号-2
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务