计算机挑战赛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);
    }
}
相关推荐
com未来3 分钟前
使用 NSSM 安装 Tomcat 11.0.6 为 Windows 服务
java·windows·tomcat
TDengine (老段)8 分钟前
基于 TSBS 标准数据集下 TimescaleDB、InfluxDB 与 TDengine 性能对比测试报告
java·大数据·开发语言·数据库·时序数据库·tdengine·iotdb
养军博客10 分钟前
spring boot3.0自定义校验注解:文章状态校验示例
java·前端·spring boot
lgily-122511 分钟前
常用的设计模式详解
java·后端·python·设计模式
IT成长史16 分钟前
deepseek梳理java高级开发工程师微服务面试题
java·微服务
茶本无香17 分钟前
Feign+Resilience4j实现微服务熔断机制:原理与实战
java·微服务·feignclient·熔断·resilience4j
遇见火星17 分钟前
Ansible模块——从控制节点向目标主机复制文件!
java·服务器·ansible
小码ssim20 分钟前
通过POI实现对word基于书签的内容替换、删除、插入
java·word
真的想上岸啊26 分钟前
c语言第一个小游戏:贪吃蛇小游戏05
c语言·算法·链表
香饽饽~、34 分钟前
函数式方法的实现(JDK8+)
java·服务器