免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2106 | 回复: 2
打印 上一主题 下一主题

[C++] 菜鸟求教一个递归问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-07-21 09:58 |只看该作者 |倒序浏览
#include<iostream>
#include<cstdio>
using namespace std;
#define Max 200001
struct data
{
    int l,r,max;
}node[3*Max];
int w;
int max(int a,int b)
{
    return a>b? a:b;
}
void BuildTree(int left,int right,int u)
{
    node.l=left;
    node.r=right;
    if(left==right)
    {
        node.max=w;
    }
    else
    {
        BuildTree(left,(left+right)>>1,2*u);
        BuildTree(((left+right)>>1)+1,right,2*u+1);
        node.max=w;
    }
}
int Update(int val,int u)
{
        int s;
        if(node.max>=val)
        {
                if(node.l==node.r)
                {
                        node.max-=val;
                        s=node.l;
                        cout<<s<<endl<<endl;;
                        return s;//为什么return的s值和上面的cout出s的值不一样呢?
                }
                else if(node[2*u].max>=val)
                {
                        Update(val,2*u);
                        node.max=max(node[2*u].max,node[2*u+1].max);
                }
                else
                {
                        Update(val,2*u+1);
                        node.max=max(node[2*u].max,node[2*u+1].max);
                }
        }
        else
        {
                //printf("-1\n");
                s=-1;
                //return -1;
        }
}
int main()
{
        int h,n,i,wi,ss;
        while(~scanf("%d%d%d",&h,&w,&n))
        {
                if(h>200000)
                        BuildTree(1,200001,1);
                else
                        BuildTree(1,h,1);
                for(i=0;i<n;i++)
                {
                        scanf("%d",&wi);
                        ss=Update(wi,1);
                        cout<<ss<<endl;
                }
        }
    return 0;
}

论坛徽章:
0
2 [报告]
发表于 2011-07-21 10:29 |只看该作者

  1.                       cout<<s<<endl<<endl;;
  2.                         return s;//为什么return的s值和上面的cout出s的值不一样呢?
复制代码
你怎么判断出不一样,加断点调试?

论坛徽章:
0
3 [报告]
发表于 2011-07-21 10:33 |只看该作者
回复 2# chinazhj


    在主函数中输出的s值和在这个函数中输出的s值不等。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP