C++博客 :: 首页 :: 新随笔 ::  ::  :: 管理

pku1068

Posted on 2010-08-20 16:27 Kevin_Zhang 阅读(219) 评论(0)  编辑 收藏 引用 所属分类: 模拟
http://acm.pku.edu.cn/JudgeOnline/problem?id=1068
分析:
  

(1)    The first element of W-sequence must be 1.

(2)    The matched left parenthesis is the closest unmatched left parenthesis.

(3)    The left must have left parenthesis and the first right parenthiesis matches with the left parenthesis nearest to it.

(4)    If , ,else w[i]=i+1;  the subscript must be from  0.

收获:局部变量和全局变量谨慎使用。这个代码在用全局变量bool flag时在外部进行初始化,在循环体内部未初始化,导致前面的循环影响后面的结果而出错。因此对全局变量在何处进行初始化必须十分细心。
代码:
#include"iostream"
#include
"stdio.h"
using namespace std;

int p[21],w[21],t,n,k;
bool flag;

int main()
{
    scanf(
"%d",&t);
    
for(int i=0;i<t;i++)
    
{
        scanf(
"%d",&n);
        
for(int j=0;j<n;j++)
            scanf(
"%d",&p[j]);
        w[
0]=1;
        
for(int j=1;j<n;j++)
        
{
            flag
=false;
            
for(k=j-1;k>=0;k--)
            
{
                
if(p[j]-p[k]>=j-k)
                
{  
                    w[j]
=j-k;
                    flag
=true;
                    
break;
                }

                
            }

            
if(flag==true)
            
{
                
continue;
            }

            
else
                w[j]
=j+1;

        }

        
for(int i=0;i<n;i++)
            printf(
"%d ",w[i]);
        printf(
"\n");
    }

    
return 0;

}

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理