当前位置: 首页 > article >正文

计算机挑战赛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++:

#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:

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:

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);
    }
}


http://www.kler.cn/news/356564.html

相关文章:

  • 多ip访问多网站
  • VScode打开代码显示乱码
  • Git小知识:合理的分支命名约定
  • LLMS-大语言模型和ai的关系?
  • 常用AI工具
  • C++ Socket通讯连接Server,并可设置连接超时时间
  • Android从上帝视角来看PackageManagerService
  • 洛谷 P4995:跳跳! ← 贪心算法
  • 编程大师都选择的Mustache 一个高效的Java库
  • linux中安装和使用dos2unix
  • (30)数字信号处理中的时域分析:均值、方差、与功率
  • 力扣 中等 82.删除排序链表中的重复元素 II
  • 【C++】类的默认成员函数:深入剖析与应用(上)
  • 电子电气架构---智能计算架构和SOA应用
  • Java動態轉發代理IP詳解
  • 个人用计算理论导引笔记(待补充)
  • 优选算法第一讲:双指针模块
  • C++(模板进阶)
  • Android 自定义TextView实现文字描边效果
  • 【vue】⾃定义指令+插槽+商品列表案例