C语言 简化版九宫格
答案:2 悬赏:50
解决时间 2021-03-02 18:37
- 提问者网友:听门外雪花风
- 2021-03-02 15:23
C语言 简化版九宫格
最佳答案
- 二级知识专家网友:我住北渡口
- 2021-03-02 15:34
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
1
3
4
5
6
7
8
9
1
2
4
5
6
7
8
9
1
2
3
5
6
7
8
9
1
2
3
4
6
7
8
9
1
2
3
4
5
7
8
9
1
2
3
4
5
6
8
9
1
2
3
4
5
6
7
9
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
1
3
4
5
6
7
8
9
1
2
4
5
6
7
8
9
1
2
3
5
6
7
8
9
1
2
3
4
6
7
8
9
1
2
3
4
5
7
8
9
1
2
3
4
5
6
8
9
1
2
3
4
5
6
7
9
1
2
3
4
5
6
7
8
全部回答
- 1楼网友:拾荒鲤
- 2021-03-02 16:52
#include <stdio.h>
#define N 9
int check_row( int a[N][N], int r )
{
int i,j;
for( i=0; i<N; i++ )
for( j=i+1; j<N; j++ )
if ( a[r][i] == a[r][j] )
return 0;
return 1;
}
int check_column( int a[N][N], int c )
{
int i,j;
for( i=0; i<N; i++ )
for( j=i+1; j<N; j++ )
if ( a[i][c] == a[j][c] )
return 0;
return 1;
}
void get_total( int a[N][N], int *total1, int *total2 )
{
int i;
for( i=0; i<N; i++ )
{
*total1 += a[i][i];
*total2 += a[i][N-i-1];
}
}
int main()
{
int a[N][N], i, j, yes=1;
for( i=0; i<N; i++ )
for( j=0; j<N; j++ )
scanf( "%d", &a[i][j] );
for( i=0; i<N; i++ )
{
if ( !check_row( a, i ) || !check_column( a, i ) )
{
yes = 0;
break;
}
}
printf( "%s ", yes ? "YES" : "NO" );
int total1 = 0, total2 = 0;
get_total( a, &total1, &total2 );
printf( "%d %d ", total1>total2?total1:total2, total1>total2?total2:total1 );
}
1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9 1
3 4 5 6 7 8 9 1 2
4 5 6 7 8 9 1 2 3
5 6 7 8 9 1 2 3 4
6 7 8 9 1 2 3 4 5
7 8 9 1 2 3 4 5 6
8 9 1 2 3 4 5 6 7
9 1 2 3 4 5 6 7 8
#define N 9
int check_row( int a[N][N], int r )
{
int i,j;
for( i=0; i<N; i++ )
for( j=i+1; j<N; j++ )
if ( a[r][i] == a[r][j] )
return 0;
return 1;
}
int check_column( int a[N][N], int c )
{
int i,j;
for( i=0; i<N; i++ )
for( j=i+1; j<N; j++ )
if ( a[i][c] == a[j][c] )
return 0;
return 1;
}
void get_total( int a[N][N], int *total1, int *total2 )
{
int i;
for( i=0; i<N; i++ )
{
*total1 += a[i][i];
*total2 += a[i][N-i-1];
}
}
int main()
{
int a[N][N], i, j, yes=1;
for( i=0; i<N; i++ )
for( j=0; j<N; j++ )
scanf( "%d", &a[i][j] );
for( i=0; i<N; i++ )
{
if ( !check_row( a, i ) || !check_column( a, i ) )
{
yes = 0;
break;
}
}
printf( "%s ", yes ? "YES" : "NO" );
int total1 = 0, total2 = 0;
get_total( a, &total1, &total2 );
printf( "%d %d ", total1>total2?total1:total2, total1>total2?total2:total1 );
}
1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9 1
3 4 5 6 7 8 9 1 2
4 5 6 7 8 9 1 2 3
5 6 7 8 9 1 2 3 4
6 7 8 9 1 2 3 4 5
7 8 9 1 2 3 4 5 6
8 9 1 2 3 4 5 6 7
9 1 2 3 4 5 6 7 8
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯
• 手机登qq时,显示手机磁盘不足,清理后重新登 |
• 刺客的套装怎么选啊? |