#include <stdio.h>
int main() {
int a;
int i,j;
while (scanf("%d", &a) != EOF) { // 注意 while 处理多个 case
// 64 位输出请用 printf("%lld") to
int arr[a][a];
for(i = 0; i < a; i++)
{
for(j = 0; j < a;j++)
{
scanf("%d",&arr[i][j]);
}
}
int count = 0;
for(i = 0; i < a; i++)
{
for(j = 0; j < a;j++)
{
if(arr[i][j] == 0)
{
count++;
}
}
}
int c = 0;
i = 1;
while(i < a)
{
c = c + i;
i ++;
}
if(count == c)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
return 0;
}
}
有一个简单的思路
就是数出0的个数,count++;
但是找出要找出a与0的个数之间的关系
仔细观察不难发现是等差数列求和
但是我研究出来一个更好用的公式
int c = 0;
i = 1;
while(i < a)
{
c = c + i;
i ++;
}
运用循环累计计算出c的值
即count的判断条件

京公网安备 11010502036488号