KiKi想知道一个n阶方矩是否为上三角矩阵,请帮他编程判定。上三角矩阵即主对角线以下的元素都为0的矩阵,主对角线为从矩阵的左上角至右下角的连线。
KiKi想知道一个n阶方矩是否为上三角矩阵,请帮他编程判定。上三角矩阵即主对角线以下的元素都为0的矩阵,主对角线为从矩阵的左上角至右下角的连线。
第一行包含一个整数n,表示一个方阵包含n行n列,用空格分隔。 (2≤n≤10)
从2到n+1行,每行输入n个整数(范围-231~231-1),用空格分隔,共输入n*n个数。
一行,如果输入方阵是上三角矩阵输出"YES"并换行,否则输出"NO"并换行。
3 1 2 3 0 4 5 0 0 6
YES
4 1 2 3 4 5 6 7 8 9 0 11 12 13 0 0 16
NO
import java.util.Scanner; /** * Created by kingwan on 2020/10/9. * 说明:取巧 */ public class Main { static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { boolean flag = true;//是否为上三角矩阵的标志 int n = scanner.nextInt(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { int value = scanner.nextInt(); //只要j<i就是在左下角的数 if(j<i){ //大于0-->false if(value>0){ flag = false; } } } } System.out.println(flag?"YES":"NO"); } }
#include <iostream> #include <vector> using namespace std; int main() { vector<vector<int>> vec; int num; cin >> num; for (int i = 0 ; i < num; ++i) { vector<int> temp; int temp_num; for (int j = 0 ; j < num; ++j) { cin >> temp_num; temp.push_back(temp_num); } vec.push_back(temp); } int flag = 1; for (int i = 0; i < num; ++i) { if (flag == 0) break; for (int j = 0; j < i; ++j) { if (vec[i][j] != 0) { flag = 0; break; } } } cout << ((flag == 1) ? "YES" : "NO"); }
#include <stdio.h> int main(){ int n, arr[10][10]; int count, x = 0, flag = 1; scanf("%d", &n); for(int i = 0; i < n; i++, x++){ count = 0; for(int j = 0; j < n; j++){ scanf("%d", &arr[i][j]); if(arr[i][j] == 0) count++; } if(count != x) flag = 0; } printf("%s", flag? "YES" : "NO"); return 0; }
n = int(input()) array = [] for i in range(n): array.append([int(x) for x in input().split()]) flag =1 for x in range(1,n):#行 for y in range(x) :#列 if (array[x][y] != 0): flag =0 if flag ==1: print("YES") else: print("NO")
while(line = readline()){ var n = parseInt(line); var res ="" for(var i=0;i<n;i++){ var line = readline(); if(line.includes("0 ".repeat(i))){ res+="YES" }else{ res+="NO" } } if(res.includes("NO")){ console.log("NO"); }else{ console.log("YES"); } }
#include<stdio.h> int main() { int n = 0, i = 0, j = 0, arr[10][10]; scanf("%d", &n); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { scanf("%d", &arr[i][j]); if (j < i && arr[i][j] != 0) { printf("NO\n"); return 0; } } } printf("YES\n"); return 0; }
#include <stdio.h> int main() { int n; scanf("%d",&n); int i,j,array[n][n],flag=0; for(i=0;i<n;i++) { for(j=0;j<n;j++) { scanf("%d",&array[i][j]); } } for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(j<i&&array[i][j]!=0) { flag=1; } } } if(flag==1) { printf("NO\n"); } else{ printf("YES\n"); } return 0; }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[][] arr = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { arr[i][j] = in.nextInt(); } } int count = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { if(arr[i][j]==0){ count++; } } } int num=isT(n); if(num == count){ System.out.print("YES"); }else{ System.out.print("NO"); } } private static int isT(int n) { if(n==1){ return 0; }else if(n==2){ return 1; }else{ return isT(n-1) + (n-1); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[][] nums = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { nums[i][j] = in.nextInt(); } } int flag = 0; //判断对角线下面是否为0 for (int i = 1; i < n; i++) { for (int j = 0; j < i; j++) { if (nums[i][j] != 0) { flag = 1; } } } if (flag == 0) { System.out.println("YES"); } else { System.out.println("NO"); } } }
int main() { int n = 0; scanf("%d", &n); int arr[50][50] = { 0 }; int i = 0; int j = 0; int flag = 0; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { scanf("%d", &arr[i][j]); } } //判断上三角矩阵,如果数不为0,则flag==0,为0则为1 for (i = 1; i < n; i++) { for (j = 0; j < i; j++) { if (arr[i][j] != 0) { flag = 1; } } } if (flag == 0) { printf("YES"); } else { printf("NO"); } }
#include <stdio.h> int main() { int n = 0; scanf("%d", &n); int arr[10][10] = { 0 }; for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { scanf("%d", &arr[i][j]); } } int sum = 0; int a = n * (n - 1) / 2; int k = 0; int p = 1; while(a) { while(p < n) { sum += arr[p][k]; a--; p++; } k++; p = 1 + k; } if(sum == 0) { printf("YES"); } else { printf("NO"); } }
#include <stdio.h> int main() { int n=0; scanf("%d",&n); int arr[n][n]; int i=0; int j=0; for (i=0; i<n; i++) { for (j=0; j<n; j++) { scanf("%d",&arr[i][j]); } } int count=0; for (i=1; i<n; i++) { for (j=0; j<i; j++) { count+=arr[i][j]; } } if (count==0) { printf("YES"); } else { printf("NO"); } return 0; }