公式

B - Nutrients 解説 by en_translator


This problem can be solved by appropriate operations on two-dimensional arrays.

Find the total amount of each nutrition and check if all of them exceeds the goal. Beware of indices handling.

Sample code (C)

#include <stdio.h>
int main(){
	int n, m;
	scanf("%d%d", &n, &m);

	int a[100];
	for(int i=0; i<m; i++){
		scanf("%d", &a[i]);
	}

	int x[100][100];
	for(int i=0; i<n; i++){
		for(int j=0; j<m; j++){
			scanf("%d", &x[i][j]);
		}
	}

	for(int j=0; j<m; j++){
		// 栄養素 j について判定する
		int sum = 0;
		for(int i=0; i<n; i++){
			sum += x[i][j];
		}
		if(sum < a[j]){
			puts("No");
			return 0;
		}
	}

	puts("Yes");
}

Sample code (Python)

N, M = map(int,input().split())
A = list(map(int,input().split()))
X = [list(map(int,input().split())) for _ in range(N)]

for j in range(M):
  s = 0
  for i in range(N):
    s += X[i][j]
  if s < A[j]:
    print("No")
    exit()

print("Yes")

投稿日時:
最終更新: