注册 登录
编程论坛 数据结构与算法

一递归算法:求解释 ~详细点 谢谢

风雨123 发布于 2013-08-05 06:55, 1030 次点击
程序代码:
/*任何一个正整数都可以用2的幂次方表示。
例如;137= 2^7+2^3+2^0.  -->2(7)+2(3)+2(0) .
输入:正整数n  输出:符合n的 0,2表示
*/
#include <iostream>
using namespace std;
stry(int n,int r)
{
  if(n==1)
cout<<"2("<<r<<")";
else
{
  stry(n/2,r+1);
if(n%2==1)
cout<<"+2("<<r<<")";
}
}
int main()
{
  int n;
cin>>n;
if(n>=1)
  stry(n,0);
else
cout<<"error"<<endl;
}
7 回复
#2
邓士林2013-08-05 08:04
赞一个
#3
wp2319572013-08-05 08:11
回复 2楼 邓士林
楼主看不懂递归代码  让你逐句解释一下
#4
wp2319572013-08-05 09:25
程序代码:
#include <stdio.h>

int main()
{
    int dest[40]={0};
    int index=0;
    int j=0;
    for(int source1=2;source1<99;source1++)
    {
        int source=source1;
        for(int i=0;i<40;i++) dest[i]=0;
        printf("%d=",source);
        index=0;
        //辗转相除
        while(1)
        {
            dest[index]=source%2;
            source=source/2;
            index++;
            if (source==1) { dest[index]=1; break; }
        }
        //检索第一个1的位置
        for(j=0;j<index;j++)
        {
            if (dest[j]==1 ) break;
        }
        //以类似15=2^3+2^2+2^1+2^0的方式输出
        while(index>=0)
        {
            if(dest[index]!=0 && index>j )  printf("2^%d+",index);
            if(dest[index]!=0 && index==j)  printf("2^%d",index);
            index--;
        }
        printf("\n");
    }
    return 0;
}
不用递归也能实现   不过比递归啰嗦一些
#5
风雨1232013-08-05 17:00
这个其实就是将10进制转化为2进制,最近刚刚接触算法,对递归我有点难以理解。   谢谢。
#6
qzpmww2013-08-06 00:03
递归之我的个人理解:不知道楼主学编程的时间有多久,是否理解计算机执行程序时对子程序调用的原理。不过就我学c语言的理解来说,递归的原理是这样的:↓↓
①主程序main开始执行,到stry(n,0);
②调用子程序stry(int n,int r),执行处理stry(n,0)方法;
③因为n!=1,所以stry(n,0)方法还没执行完成,计算机就又开始调用子程序stry(int n,int r),执行处理stry(n/2,0);
④重复②,③,直到调用stry(int n,int r)时,n==1;
⑤因为n==1,stry(n,0)直接执行语句cout<<"2("<<r<<")",stry(1,0)结束;
⑥stry(1,0)结束,stry(2,0)可以执行stry(n/2,r+1)下一行的语句,stry(2,0)结束。
⑦重复执行⑥,直到stry(n,0)方法中stry(n/2,0)结束,stry(n,0)方法执行下一行语句,stry(n,0)方法结束,返回主程序。
⑧程序结束。

运行时程序的流程应该是这样的:
-----------------------------------------------------------------------|
| int n;                                                               |
| cin>>n;       ↓                                                     |
|      ----------------------------------------------------------------|
|     | stry(n,0)        ↓                                            |           ↑
|     |           |----------------------------------------------------|
|     |           |   stry(n/2,0)           ↓                         |           ↑
|     |           |          |-----------------------------------------|
|     |           |          | stry((n/2)/2,0)       ↓                |           ↑
|     |           |          |             ----------------------------|
|     |           |          |             |   ........    ↓          |           ↑
|     |           |          |             |          -----------------|
|     |           |          |             |          | stry(1,0)      |           ↑
|     |           |          |             |          |                |------处理完返回上一级
|     |           |          |             |          |                |
|----------------------------------------------------------------------|
     ↓
     ↓
程序结束
#7
molubingxue2013-08-22 13:03
#include<stdio.h>
main()
{
    int x,n;
    scanf("%d",&n);
    x=0;
    printf("%d=",n);
    while(n!=0)
    {
        if (n==1) break;
        if (n%2==1)
        {
            printf("2^%d+",x++);
        }
        else
        {
            x++;
        }
        n/=2;
    }
    if (n==1) printf("2^%d",x);
}

走起~
#8
cs648812792013-08-23 16:31
自己在脑袋里面用几个数字,试试,就差不多能明白吧.....
1