NC229586. Abnormal unity
描述
A fence with three stakes, a hero with three help!
There is an army. Each soldier has a positive integer basic force value V. When the square army with the side length of N is fighting, it is extremely united. The method of unity of each soldier is as follows:
1. It is assumed that each soldier has adjacent soldiers in eight directions (front, rear, left, right, left front, right front, left rear and right rear), and connects front and rear, left and right, left front and right rear and right front and left rear.That is, the front row in the front row points to the last row, the rear row in the last row points to the front row, connected left and right, and so on. Similarly, the leftmost front and then the left front point to the rightmost rear, the rightmost rear and then the right rear point to the leftmost front, the connection between the right front and the left rear, and so on.
2. If the basic force value V of a soldier is an odd number, the soldier will unite the soldiers in the front, rear, left and right directions around him. That is, the soldier's comprehensive force value is the sum of the soldiers in the front, back, left and right directions around him and his own basic force value;
3. If the basic force value V of a soldier is even, the soldier will unite the soldiers in the front left, front right, rear left and rear right directions around him. That is, the soldier's comprehensive force value is the sum of the soldiers in the front left, front right, rear left and rear right directions around him and his own basic force value.
For such a united army, do you know the sum of the comprehensive force value of the soldiers of the whole army?
输入描述
The input hasmultiple (no more than 100) test data.
The first line ofeach test data is a positive integer N (1<=N<=100), which represents theside length of the square Army (the number of people on each side).
Next, there are Nlines, N positive integers for each line, representing the basic force value Vof soldiers at the corresponding position (1<=V<=100).
The end of inputwill be represented by a line of test data with N equal to 0, which should notbe processed.
输出描述
Each test data outputs a line, which is a positive integer, that is, the sum of the comprehensive force values of the soldiers of the whole army.
示例1
输入:
4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 0
输出:
80 184
C++(clang++ 11.0.1) 解法, 执行用时: 23ms, 内存消耗: 488K, 提交时间: 2023-04-09 10:45:01
#include<iostream> using namespace std; const int N = 110; int g[N][N]; int main(){ while(1){ int n; cin>>n; if(n==0)return 0; int ans=0; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ cin>>g[i][j]; } } for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(g[i][j]%2==1){ ans=ans+g[i][j]+g[(i-1+n)%n][j]+g[(i + 1)%n][j]+g[i][(j-1+n)%n]+g[i][(j+1)%n]; } else{ ans=ans+g[i][j]+g[(i-1+n)%n][(j-1+n)%n]+g[(i-1+n)%n][(j+1)%n]+g[(i+1)%n][(j-1+n)%n]+g[(i+1)%n][(j+1)%n]; } } } cout<<ans<<endl; } return 0; }
Python3 解法, 执行用时: 156ms, 内存消耗: 4728K, 提交时间: 2022-10-22 14:47:08
while 1: N=int(input()) if N: arr=[] sum=0 for i in range(N): l=list(map(int,input().split())) arr.append(l) for m in range(N): for k in range(N): if(arr[m][k]%2==1): sum+=arr[m][k]+arr[(m+1)%N][k]+arr[(m-1)%N][k]+arr[m][(k+1)%N]+arr[m][(k-1)%N] else: sum+=arr[m][k]+arr[(m+1)%N][(k+1)%N]+arr[(m+1)%N][(k-1)%N]+arr[(m-1)%N][(k+1)%N]+arr[(m-1)%N][(k-1)%N] print(sum) else: break