我在写洛谷题https://www.luogu.com.cn/problem/P1838
的时候遇到了问题,
#include<bitsdc++.h>
using namespace std;
const int N=110;
string x;
char g[10];
int flag;
char ch;
signed main()
{
cin>>x;
for(int i=0;i<x.length();i++)
{
if((i+1)%2!=0) g[x[i]-'0']='x';
if((i+1)%2==0) g[x[i]-'0']='o';
}
if(g[1]==g[2]&&g[2]==g[3]) ch=g[1],flag=1;
if(g[4]==g[5]&&g[5]==g[6]) ch=g[4],flag=1;
if(g[7]==g[8]&&g[8]==g[9]) ch=g[7],flag=1;
if(g[1]==g[4]&&g[4]==g[7]) ch=g[1],flag=1,cout<<ch<<" "<<"test0"<<endl;
if(g[2]==g[5]&&g[5]==g[8]) ch=g[2],flag=1;
if(g[3]==g[6]&&g[6]==g[9]) ch=g[3],flag=1;
if(g[1]==g[5]&&g[5]==g[9]) ch=g[1],flag=1;
if(g[3]==g[5]&&g[5]==g[7]) ch=g[3],flag=1;
cout<<ch<<" "<<"test1"<<endl;
if(flag==1)
{
if(ch=='x') cout<<"xiaoa wins.";
if(ch=='o') cout<<"uim wins.";
}else{
cout<<"drew.";
}
return 0;
}
为什么这个位置1能正确输出ch,位置2就不能输出ch了
加了goto后就能ac了,这是为什么啊。。
#include<bitsdc++.h>
using namespace std;
const int N=110;
string x;
char g[10];
int flag;
char ch;
signed main()
{
cin>>x;
for(int i=0;i<x.length();i++)
{
if((i+1)%2!=0) g[x[i]-'0']='x';
if((i+1)%2==0) g[x[i]-'0']='o';
}
if(g[1]==g[2]&&g[2]==g[3])
{
ch=g[1],flag=1;goto end;
}
if(g[4]==g[5]&&g[5]==g[6])
{
ch=g[4],flag=1;goto end;
}
if(g[7]==g[8]&&g[8]==g[9])
{
ch=g[7],flag=1;goto end;
}
if(g[1]==g[4]&&g[4]==g[7])
{
ch=g[1],flag=1;goto end;
}
if(g[2]==g[5]&&g[5]==g[8])
{
ch=g[2],flag=1;goto end;
}
if(g[3]==g[6]&&g[6]==g[9])
{
ch=g[3],flag=1;goto end;
}
if(g[1]==g[5]&&g[5]==g[9])
{
ch=g[1],flag=1;goto end;
}
if(g[3]==g[5]&&g[5]==g[7])
{
ch=g[3],flag=1;goto end;
}
end:
if(flag==1)
{
if(ch=='x') cout<<"xiaoa wins.";
if(ch=='o') cout<<"uim wins.";
}else{
cout<<"drew.";
}
return 0;
}