錯的題目前面數字都對,直到最後面才出錯,可以請各位大神幫我看嗎
#include <iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int c;
cin>>c;
int square[n][n];
for( int i = 0 ; i < n ; i++ )
{
for( int j = 0 ; j < n ; j++ )
{
cin>>square[i][j];
}
}
int x = (n-1)/2;
int y = (n-1)/2;
cout<<square[x][y];
int t;
switch(c)
{
case 0://left
t = -1;
for( int i = 1 ; i <= (n-2) ; i++ )
{
for( int j = 0 ; j < i ; j++ )
{
y += t;
cout<<square[x][y];
}
for( int j = 0 ; j < i ; j++ )
{
x += t;
cout<<square[x][y];
}
t *= -1;
}
for( int i = 0 ; i < n-1 ; i++ )
{
y += 1;
cout<<square[x][y];
}
for( int i = 0 ; i < n-1 ; i++ )
{
x += 1;
cout<<square[x][y];
}
for( int i = 0 ; i < n-1 ; i++ )
{
y -= 1;
cout<<square[x][y];
}
break;
case 1://up
t = -1;
for( int i = 1 ; i <= (n-2) ; i++ )
{
for( int j = 0 ; j < i ; j++ )
{
x += t;
cout<<square[x][y];
}
for( int j = 0 ; j < i ; j++ )
{
y -= t;
cout<<square[x][y];
}
t *= -1;
}
for( int i = 0 ; i < n-1 ; i++ )
{
x += 1;
cout<<square[x][y];
}
for( int i = 0 ; i < n-1 ; i++ )
{
y -= 1;
cout<<square[x][y];
}
for( int i = 0 ; i < n-1 ; i++ )
{
x -= 1;
cout<<square[x][y];
}
break;
case 2://right
t = 1;
for( int i = 1 ; i <= (n-2) ; i++ )
{
for( int j = 0 ; j < i ; j++ )
{
y += t;
cout<<square[x][y];
}
for( int j = 0 ; j < i ; j++ )
{
x += t;
cout<<square[x][y];
}
t *= -1;
}
for( int i = 0 ; i < n-1 ; i++ )
{
y -= 1;
cout<<square[x][y];
}
for( int i = 0 ; i < n-1 ; i++ )
{
x += 1;
cout<<square[x][y];
}
for( int i = 0 ; i < n-1 ; i++ )
{
y += 1;
cout<<square[x][y];
}
break;
case 3://down
t = 1;
for( int i = 1 ; i <= (n-2) ; i++ )
{
for( int j = 0 ; j < i ; j++ )
{
x += t;
cout<<square[x][y];
}
for( int j = 0 ; j < i ; j++ )
{
y -= t;
cout<<square[x][y];
}
t *= -1;
}
for( int i = 0 ; i < n-1 ; i++ )
{
x -= 1;
cout<<square[x][y];
}
for( int i = 0 ; i < n-1 ; i++ )
{
y += 1;
cout<<square[x][y];
}
for( int i = 0 ; i < n-1 ; i++ )
{
x += 1;
cout<<square[x][y];
}
break;
}
return 0;
}
case 2://right
for( int i = 0 ; i < n-1 ; i++ )
{
x += 1;
cout<<square[x][y];}
case 2 裡面,x += 1 改成 x -= 1