Posted on 2010-08-20 16:27
Kevin_Zhang 閱讀(238)
評論(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.
收獲:局部變量和全局變量謹(jǐn)慎使用。這個代碼在用全局變量bool flag時在外部進(jìn)行初始化,在循環(huán)體內(nèi)部未初始化,導(dǎo)致前面的循環(huán)影響后面的結(jié)果而出錯。因此對全局變量在何處進(jìn)行初始化必須十分細(xì)心。
代碼:
#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;

}