计算机挑战赛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);
    }
}
相关推荐
凌肖战几秒前
力扣网编程55题:跳跃游戏之逆向思维
算法·leetcode
念九_ysl14 分钟前
Java 使用 OpenHTMLToPDF + Batik 将含 SVG 遮罩的 HTML 转为 PDF 的完整实践
java·开发语言·pdf
yaoxin52112323 分钟前
124. Java 泛型 - 有界类型参数
java·开发语言
Spirit_NKlaus26 分钟前
解决HttpServletRequest无法获取@RequestBody修饰的参数
java·spring boot·spring
不死的精灵32 分钟前
【Java21】在spring boot中使用ScopedValue
java·spring boot·后端
88号技师1 小时前
2025年6月一区-田忌赛马优化算法Tianji’s horse racing optimization-附Matlab免费代码
开发语言·算法·matlab·优化算法
勤奋的知更鸟1 小时前
Java 编程之模板方法模式
java·开发语言·模板方法模式
逸风尊者1 小时前
开发易掌握的知识:GeoHash查找附近空闲车辆
java·后端
ゞ 正在缓冲99%…1 小时前
leetcode918.环形子数组的最大和
数据结构·算法·leetcode·动态规划
碎叶城李白2 小时前
若依学习笔记1-validated
java·笔记·学习·validated