https://blog.csdn.net/guotianqing/article/details/79828100 static的一些介绍
1. 使用static声明变量 :避免栈中数组越界导致不可预知错误
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int main()
{
static int queue[100];
static int a[10];
static int b[10];
memset(queue,0,sizeof(queue));
int n,m;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&m);
for(int i=0;i<m;i++)
scanf("%d",&b[i]);
// int a[10]={2,4,1,2,5,6};
// int b[10]={3,1,3,5,6,4};
int a_left=0,a_right=n;
int b_left=0,b_right=m;
int temp_left=0,temp_right=0;
while(a_left!=a_right && b_left!=b_right)
{
cout<<"*** "<<temp_left<<" *** "<<temp_right<<endl;
cout<<"queue: ";
for(int i=temp_left;i<temp_right;i++)
cout<<queue[i]<<" ";
cout<<endl;
for(int i=a_left;i<a_right;i++)
cout<<a[i]<<" ";
cout<<endl;
for(int i=b_left;i<b_right;i++)
cout<<b[i]<<" ";
cout<<endl;
queue[temp_right++]=a[a_left++];
for(int i=b_left;i<b_right;i++)
cout<<b[i]<<" ";
cout<<endl;
int temp=temp_left;
while(temp<temp_right-1)
{
if(queue[temp_right-1]==queue[temp++] && temp_right!=1)
{
// for(int i=temp-1;i<=temp_right-1;i++)
for(int i=temp_right-1;i>=temp-1;i--)
{
a[a_right++]=queue[i];
}
temp_right=temp-1;
break;
}
}
for(int i=b_left;i<b_right;i++)
cout<<b[i]<<" ";
cout<<endl;
queue[temp_right++]=b[b_left++];
temp=temp_left;
while(temp<temp_right-1)
{
if(b[b_left-1]==queue[temp++]&& temp_right!=1)
{
// for(int i=temp-1;i<=temp_right-1;i++)
for(int i=temp_right-1;i>=temp-1;i--)
{
b[b_right++]=queue[i];
}
temp_right=temp-1;
break;
}
}
for(int i=b_left;i<b_right;i++)
cout<<b[i]<<" ";
cout<<endl;
}
cout<<"queue: ";
for(int i=temp_left;i<temp_right;i++)
cout<<queue[i]<<" ";
cout<<endl;
for(int i=a_left;i<a_right;i++)
cout<<a[i]<<" ";
cout<<endl;
for(int i=b_left;i<b_right;i++)
cout<<b[i]<<" ";
cout<<endl;
cout<<a_left<<" "<<a_right<<endl;
cout<<endl;
if(a_left==a_right)
{
printf("b win\n");
}
else
printf("a win\n");
}
6
2 4 1 2 5 6
6
3 1 3 5 6 4
*** 0 *** 0
queue:
2 4 1 2 5 6
3 1 3 5 6 4
3 1 3 5 6 4
3 1 3 5 6 4
1 3 5 6 4
*** 0 *** 2
queue: 2 3
4 1 2 5 6
1 3 5 6 4
1 3 5 6 4
1 3 5 6 4
3 5 6 4
*** 0 *** 4
queue: 2 3 4 1
1 2 5 6
3 5 6 4
3 5 6 4
3 5 6 4
5 6 4 3 4 3
*** 0 *** 1
queue: 2
2 5 6 1 1
5 6 4 3 4 3
5 6 4 3 4 3
5 6 4 3 4 3
6 4 3 4 3
*** 0 *** 1
queue: 5
5 6 1 1 2 2
6 4 3 4 3
6 4 3 4 3
6 4 3 4 3
4 3 4 3
*** 0 *** 1
queue: 6
6 1 1 2 2 5 5
4 3 4 3
4 3 4 3
4 3 4 3
3 4 3
*** 0 *** 1
queue: 4
1 1 2 2 5 5 6 6
3 4 3
3 4 3
3 4 3
4 3
*** 0 *** 3
queue: 4 1 3
1 2 2 5 5 6 6
4 3
4 3
4 3
3 4 4
*** 0 *** 0
queue:
2 2 5 5 6 6 1 3 1
3 4 4
3 4 4
3 4 4
4 4
*** 0 *** 2
queue: 2 3
2 5 5 6 6 1 3 1
4 4
4 4
4 4
4
*** 0 *** 1
queue: 4
5 5 6 6 1 3 1 2 3 2
4
4
4
4 5 4
*** 0 *** 0
queue:
5 6 6 1 3 1 2 3 2
4 5 4
4 5 4
4 5 4
5 4
*** 0 *** 2
queue: 5 4
6 6 1 3 1 2 3 2
5 4
5 4
5 4
4 5 6 4 5
*** 0 *** 0
queue:
6 1 3 1 2 3 2
4 5 6 4 5
4 5 6 4 5
4 5 6 4 5
5 6 4 5
*** 0 *** 2
queue: 6 4
1 3 1 2 3 2
5 6 4 5
5 6 4 5
5 6 4 5
6 4 5
*** 0 *** 4
queue: 6 4 1 5
3 1 2 3 2
6 4 5
6 4 5
6 4 5
4 5 6 3 5 1 4 6
*** 0 *** 0
queue:
1 2 3 2
4 5 6 3 5 1 4 6
4 5 6 3 5 1 4 6
4 5 6 3 5 1 4 6
5 6 3 5 1 4 6
*** 0 *** 2
queue: 1 4
2 3 2
5 6 3 5 1 4 6
5 6 3 5 1 4 6
5 6 3 5 1 4 6
6 3 5 1 4 6
*** 0 *** 4
queue: 1 4 2 5
3 2
6 3 5 1 4 6
6 3 5 1 4 6
6 3 5 1 4 6
3 5 1 4 6
*** 0 *** 6
queue: 1 4 2 5 3 6
2
3 5 1 4 6
3 5 1 4 6
3 5 1 4 6
5 1 4 6
*** 0 *** 3
queue: 1 4 3
2 6 3 5 2
5 1 4 6
5 1 4 6
5 1 4 6
1 4 6
*** 0 *** 5
queue: 1 4 3 2 5
6 3 5 2
1 4 6
1 4 6
1 4 6
4 6 1 6 5 2 3 4 1
*** 0 *** 0
queue:
3 5 2
4 6 1 6 5 2 3 4 1
4 6 1 6 5 2 3 4 1
4 6 1 6 5 2 3 4 1
6 1 6 5 2 3 4 1
*** 0 *** 2
queue: 3 4
5 2
6 1 6 5 2 3 4 1
6 1 6 5 2 3 4 1
6 1 6 5 2 3 4 1
1 6 5 2 3 4 1
*** 0 *** 4
queue: 3 4 5 6
2
1 6 5 2 3 4 1
1 6 5 2 3 4 1
1 6 5 2 3 4 1
6 5 2 3 4 1
queue: 3 4 5 6 2 1
6 5 2 3 4 1
25 25
b win
Process returned 0 (0x0) execution time : 10.840 s
Press any key to continue.
2. 未使用static数组越界导致数据被篡改
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int main()
{
int queue[100];
int a[10];
int b[10];
memset(queue,0,sizeof(queue));
int n,m;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&m);
for(int i=0;i<m;i++)
scanf("%d",&b[i]);
// int a[10]={2,4,1,2,5,6};
// int b[10]={3,1,3,5,6,4};
int a_left=0,a_right=n;
int b_left=0,b_right=m;
int temp_left=0,temp_right=0;
while(a_left!=a_right && b_left!=b_right)
{
cout<<"*** "<<temp_left<<" *** "<<temp_right<<endl;
cout<<"queue: ";
for(int i=temp_left;i<temp_right;i++)
cout<<queue[i]<<" ";
cout<<endl;
for(int i=a_left;i<a_right;i++)
cout<<a[i]<<" ";
cout<<endl;
for(int i=b_left;i<b_right;i++)
cout<<b[i]<<" ";
cout<<endl;
queue[temp_right++]=a[a_left++];
for(int i=b_left;i<b_right;i++)
cout<<b[i]<<" ";
cout<<endl;
int temp=temp_left;
while(temp<temp_right-1)
{
if(queue[temp_right-1]==queue[temp++] && temp_right!=1)
{
// for(int i=temp-1;i<=temp_right-1;i++)
for(int i=temp_right-1;i>=temp-1;i--)
{
a[a_right++]=queue[i];
}
temp_right=temp-1;
break;
}
}
for(int i=b_left;i<b_right;i++)
cout<<b[i]<<" ";
cout<<endl;
queue[temp_right++]=b[b_left++];
temp=temp_left;
while(temp<temp_right-1)
{
if(b[b_left-1]==queue[temp++]&& temp_right!=1)
{
// for(int i=temp-1;i<=temp_right-1;i++)
for(int i=temp_right-1;i>=temp-1;i--)
{
b[b_right++]=queue[i];
}
temp_right=temp-1;
break;
}
}
for(int i=b_left;i<b_right;i++)
cout<<b[i]<<" ";
cout<<endl;
}
cout<<"queue: ";
for(int i=temp_left;i<temp_right;i++)
cout<<queue[i]<<" ";
cout<<endl;
for(int i=a_left;i<a_right;i++)
cout<<a[i]<<" ";
cout<<endl;
for(int i=b_left;i<b_right;i++)
cout<<b[i]<<" ";
cout<<endl;
cout<<a_left<<" "<<a_right<<endl;
cout<<endl;
if(a_left==a_right)
{
printf("b win\n");
}
else
printf("a win\n");
}
6
2 4 1 2 5 6
6
3 1 3 5 6 4
*** 0 *** 0
queue:
2 4 1 2 5 6
3 1 3 5 6 4
3 1 3 5 6 4
3 1 3 5 6 4
1 3 5 6 4
*** 0 ***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 6 6 2 6
6 3 6 6 2 6
6 3 6 6 2 6
3 6 6 2 6
*** 0 *** 4
queue: 1 1 3 6
2 1 1
3 6 6 2 6
3 6 6 2 6
3 6 6 2 6
6 6 2 6 3 2 6 3
*** 0 *** 2
queue: 1 1
1 1
6 6 2 6 3 2 6 3
6 6 2 6 3 2 6 3
6 6 2 6 3 2 6 3
6 2 6 3 2 6 3
*** 0 *** 1
queue: 6
1 1 1 1
6 2 6 3 2 6 3
6 2 6 3 2 6 3
6 2 6 3 2 6 3
2 6 3 2 6 3 6 1 6
*** 0 *** 0
queue:
6 1 1
2 6 3 2 6 3 6 1 6
2 6 3 2 6 3 6 1 6
2 6 3 2 6 3 6 1 6
6 3 2 6 3 6 1 6
*** 0 *** 2
queue: 6 2
1 1
6 3 2 6 3 6 1 6
6 3 2 6 3 6 1 6
6 3 2 6 3 6 1 6
3 2 6 3 6 1 6 6 1 2 6
*** 0 *** 0
queue:
1
3 2 6 3 6 1 6 6 1 2 6
3 2 6 3 6 1 6 6 1 2 6
3 2 6 3 6 1 6 6 1 2 6
2 6 3 6 1 6 6 1 2 6
queue: 1 3
2 6 3 6 1 6 6 1 2 6
25 25
b win
Process returned 0 (0x0) execution time : 9.059 s
Press any key to continue.
3. 改变解决方案的思路,避免数组越界等混杂情况的发生,可以不使用static
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
struct queue
{
int data[1000];
int head;
int tail;
};
struct stack
{
int data[10];
int top;
};
int main()
{
struct queue q1,q2;
struct stack s;
int book[10];
q1.head=1,q1.tail=1;
q2.head=1,q2.tail=1;
s.top=0;
memset(book,0,sizeof(book));
for(int i=1; i<=6; i++)
{
scanf("%d",&q1.data[q1.tail++]);
}
for(int i=1; i<=6; i++)
{
scanf("%d",&q2.data[q2.tail++]);
}
int t;
while(q1.head<q1.tail && q2.head<q2.tail)
{
for(int i=1;i<=s.top;i++)
printf("%d ",s.data[i]);
printf("\n");
for(int i=q1.head;i<q1.tail;i++)
printf("%d ",q1.data[i]);
printf("\n");
for(int i=q2.head;i<q2.tail;i++)
printf("%d ",q2.data[i]);
printf("\n\n");
t=q1.data[q1.head];
if(book[t]==0)
{
q1.head++;
s.data[++s.top]=t;
book[t]=1;
}
else
{
q1.head++;
q1.data[q1.tail++]=t;
while(s.data[s.top]!=t)
{
book[s.data[s.top]]=0;
q1.data[q1.tail++]=s.data[s.top--];
}
book[s.data[s.top]]=0;
q1.data[q1.tail++]=s.data[s.top--];
}
t=q2.data[q2.head];
if(book[t]==0)
{
q2.head++;
s.data[++s.top]=t;
book[t]=1;
}
else
{
q2.head++;
q2.data[q2.tail++]=t;
while(s.data[s.top]!=t)
{
book[s.data[s.top]]=0;
q2.data[q2.tail++]=s.data[s.top--];
}
book[s.data[s.top]]=0;
q2.data[q2.tail++]=s.data[s.top--];
}
}
cout<<q1.head<<" "<<q1.tail<<endl;
cout<<q2.head<<" "<<q2.tail<<endl;
if(q2.head==q2.tail)
{
printf("小哼 win\n");
for(int i=q1.head; i<q1.tail; i++)
cout<<q1.data[i]<<" ";
cout<<endl;
if(s.top>0)
{
for(int i=1; i<=s.top; i++)
{
cout<<s.data[i]<<" ";
}
}
cout<<endl;
}
if(q1.head==q1.tail)
{
printf("小哈 win\n");
for(int i=q2.head; i<q2.tail; i++)
cout<<q2.data[i]<<" ";
cout<<endl;
if(s.top>0)
{
for(int i=1; i<=s.top; i++)
{
cout<<s.data[i]<<" ";
}
}
cout<<endl;
}
}
2 4 1 2 5 6
3 1 3 5 6 4
2 4 1 2 5 6
3 1 3 5 6 4
2 3
4 1 2 5 6
1 3 5 6 4
2 3 4 1
1 2 5 6
3 5 6 4
2
2 5 6 1 1
5 6 4 3 4 3
5
5 6 1 1 2 2
6 4 3 4 3
6
6 1 1 2 2 5 5
4 3 4 3
4
1 1 2 2 5 5 6 6
3 4 3
4 1 3
1 2 2 5 5 6 6
4 3
2 2 5 5 6 6 1 3 1
3 4 4
2 3
2 5 5 6 6 1 3 1
4 4
4
5 5 6 6 1 3 1 2 3 2
4
5 6 6 1 3 1 2 3 2
4 5 4
5 4
6 6 1 3 1 2 3 2
5 4
6 1 3 1 2 3 2
4 5 6 4 5
6 4
1 3 1 2 3 2
5 6 4 5
6 4 1 5
3 1 2 3 2
6 4 5
1 2 3 2
4 5 6 3 5 1 4 6
1 4
2 3 2
5 6 3 5 1 4 6
1 4 2 5
3 2
6 3 5 1 4 6
1 4 2 5 3 6
2
3 5 1 4 6
1 4 3
2 6 3 5 2
5 1 4 6
1 4 3 2 5
6 3 5 2
1 4 6
3 5 2
4 6 1 6 5 2 3 4 1
3 4
5 2
6 1 6 5 2 3 4 1
3 4 5 6
2
1 6 5 2 3 4 1
26 26
26 32
小哈 win
6 5 2 3 4 1
3 4 5 6 2 1
Process returned 0 (0x0) execution time : 6.241 s
Press any key to continue.