计算机挑战赛6

<p><font face="仿宋">定义了NxN的二维数组,数组元素(整数)通过键盘输入,请编写程序,求出数组周边元素的和值,并输出。(2&lt;=N&lt;=100)</font><br/></p><p><font face="仿宋"><br/></font></p><p><font face="仿宋">输入格式:</font></p><p>第1行:输入N。其中2&lt;=N&lt;=100<br/>第2到N+1行,每行输入N的元素,元素之间用两个空格分隔。</p><p><br/></p><p>输出格

代码:

C++:

cpp 复制代码
#include<iostream>
using namespace std;
const int N = 0x3f3f;
class Solution {
public:
	int SumNum(int arr[], int n) {
		int res = 0;
		for (int i = 1; i <= n * n; i++) {
			if (i < n) {
				res += arr[i];
			}
			else if (i % n == 0 && i < n * n) {
				res += arr[i];
				res += arr[i + 1];
			}
			else if (i > n * (n - 1) + 1) {
				res += arr[i];
			}

		}
		return res;
	}
};
int main() {
	int arr[N];
	int n;
	cin >> n;
	for (int i = 1; i <= n * n; i++) {
		int num;
		cin >> num;
		arr[i] = num;
	}
	Solution solution = Solution();
	int res = solution.SumNum(arr, n);
	cout << res << endl;
	return 0;
}

Python:

python 复制代码
class Solution:
    def SumNum(self, arr, n):
        res = 0
        for i in range(1, n * n + 1, 1):
            if i < n:
                res += arr[i]
            elif i % n == 0 and i <= n * (n - 1) + 1:
                res += arr[i]
                res += arr[i + 1]
            elif i > n * (n - 1) + 1:
                res += arr[i]
        return res


def main():
    solution = Solution()
    n = int(input())
    arr = [0] * 0x3f3f
    num = input().split()
    for i in range(0, n * n, 1):
        arr[i + 1] = int(num[i])
    res = solution.SumNum(arr, n)
    print(res)


if __name__=="__main__":
    main()

Java:

java 复制代码
package com.my.gududu;

import java.util.*;


class Solution {
    int SumNum(int arr[], int n) {
        int res = 0;
        for (int i = 1; i <= n * n; i++) {
            if (i < n) {
                res += arr[i];
            }
            else if (i % n == 0 && i <= n * (n - 1)) {
                res += arr[i];
                res += arr[i + 1];
            }
            else if (i > n * (n - 1) + 1) {
                res += arr[i];
            }
        }
        return res;
    }
}
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int arr[] = new int[0x3f3f];
        for (int i = 1; i <= n * n; i++) {
            int num = input.nextInt();
            arr[i] = num;
        }
        Solution solution = new Solution();
        int res = solution.SumNum(arr, n);
        System.out.println(res);
    }
}
相关推荐
不要再敲了几秒前
Spring Security 完整使用指南
java·后端·spring
还是鼠鼠30 分钟前
《黑马商城》微服务保护-详细介绍【简单易懂注释版】
java·spring boot·spring·spring cloud·sentinel·maven
西望云天1 小时前
The 2023 ICPC Asia Shenyang Regional Contest(2023沈阳区域赛CEJK)
数据结构·算法·icpc
她说..1 小时前
通过git拉取前端项目
java·前端·git·vscode·拉取代码
青衫码上行1 小时前
【从0开始学习Java | 第18篇】集合(下 - Map部分)
java·学习
我星期八休息1 小时前
C++异常处理全面解析:从基础到应用
java·开发语言·c++·人工智能·python·架构
zh_xuan1 小时前
LeeCode92. 反转链表II
数据结构·算法·链表·leecode
江湖有缘1 小时前
【Docker项目实战】使用Docker部署ShowDoc文档管理工具
java·docker·容器
2401_841495642 小时前
【数据结构】汉诺塔问题
java·数据结构·c++·python·算法·递归·
程序猿阿越2 小时前
Kafka源码(六)消费者消费
java·后端·源码阅读