alias_0715 于 2013.09.27 20:41 提问

“Yes”，如果打出顺序满足“栈”的特点；
“No”，如果打出顺序不满足“栈”的特点。

3
1 2 3 4 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1
3 1 2 4 5 6 7 8 9 10

Yes
Yes
No

include
using namespace std;

int main() {
int T;
int n[12];
cin >> T;
while (T--) {
int r = 0,max = 0;
for (int i = 0; i < 10; i++) {
cin >> n[i];
}
for (int i = 0; i < 8; i++) {
if(max < n[i]){
max = n[i];
}
if (r = 1) break;
if (n[i+1] < max){
if (n[i+2] > n[i+1] && n[i+2] < max) {
r = 1;
}
}

``````  }
if (r = 0) cout << "Yes\n";
else cout << "No\n";
``````

}
system("pause");
return 0;
}

2个回答

Jimrs1252   2013.10.22 23:27

int T;
int n[12];
cin >> T;
while (T--)
{
int r = 0,max = 0;
for (int i = 0; i < 10; i++)
{
cin >> n[i];
}
for (int i = 0; i < 8; i++)
{
if(max < n[i])
{
max = n[i];
}
if (r == 1) break;//这里错了。你写成了r=1
if (n[i+1] < max)
{
if (n[i+2] > n[i+1] && n[i+2] < max)
{
r = 1;
}
}
}
if (r == 0) //这里错了。你写成了r=0
cout << "Yes\n";
else
cout << "No\n";
}
system("pause");
return 0;

DoctorCZM   2014.01.10 18:47

if (r = 0) //这是赋值，而非条件判断，特别注意
cout << "Yes\n";
else cout << "No\n";

``````#include <cstdio>
``````

#include

using namespace std;

int isCorrectOrder(int * arr, int len)
{
int i, j;
int stack[10], cur, index;
for(i = 0, cur = 1, index = 0; i < len; ++ i)
{
if(arr[i] >= cur) //下一个要出栈的数没有在栈中出现过
{
for(j = cur; j < arr[i]; ++ j)
stack[index ++] = j;
cur = arr[i] + 1;
}
else
{
if(stack[index-1] == arr[i]) //下一个出栈的数在栈顶位置
-- index;
else //其他情况则不可能是给定已经顺序的出栈顺序
return -1;
}
}

``````return 0;
``````

}

int main()
{
int T, ret;
int n[12];
cin >> T;
while (T--)
{
for (int i = 0; i < 10; i++)
cin >> n[i];

``````    ret = isCorrectOrder(n, 10);

if (ret == 0) cout << "Yes\n";
else cout << "No\n";
}

system("pause");
return 0;
``````

}