计算机挑战赛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);
    }
}
相关推荐
朝新_4 小时前
【多线程初阶】阻塞队列 & 生产者消费者模型
java·开发语言·javaee
立莹Sir4 小时前
Calendar类日期设置进位问题
java·开发语言
木子.李3475 小时前
排序算法总结(C++)
c++·算法·排序算法
季鸢5 小时前
Java设计模式之状态模式详解
java·设计模式·状态模式
闪电麦坤956 小时前
数据结构:递归的种类(Types of Recursion)
数据结构·算法
@yanyu6666 小时前
springboot实现查询学生
java·spring boot·后端
ascarl20106 小时前
准确--k8s cgroup问题排查
java·开发语言
magic 2456 小时前
Lombok 的 @Data 注解失效,未生成 getter/setter 方法引发的HTTP 406 错误
java
爱敲代码的憨仔6 小时前
分布式协同自动化办公系统-工作流引擎-流程设计
java·flowable·oa
Gyoku Mint7 小时前
机器学习×第二卷:概念下篇——她不再只是模仿,而是开始决定怎么靠近你
人工智能·python·算法·机器学习·pandas·ai编程·matplotlib