CCF-201809-1-卖菜

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/AivenZhong/article/details/83341623

思路:
这次ccf的前面两题好水啊,思路简单说下:第一题卖菜,题目意思是让每个商店价格根据第一天来调整,第二天的价格是第一天自己和相邻商店的价格平均值,所以就直接遍历第一天的价格表,算出第二天的价格表

Java代码:

import java.util.Scanner;

public class 卖菜 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int[] li = new int[n];
        int[] newLi = new int[n];
        for (int i = 0; i < li.length; i++)
            li[i] = input.nextInt();

        for (int i = 0; i < li.length; i++) {
            if (i == 0){
                newLi[i] = (li[i] + li[i + 1]) / 2;
            } else if (i == li.length - 1) {
                newLi[i] = (li[i] + li[i - 1]) / 2;
            } else {
                newLi[i] = (li[i - 1] + li[i] + li[i + 1]) / 3;
            }
        }

        for (int i : newLi)
            System.out.print(i + " ");
    }
}

python代码:

n = int(input())
prices = [int(val) for val in input().split()]
new_prices = [0 for i in range(len(prices))]

for i in range(len(prices)):
    if i == 0:
        new_prices[0] = int((prices[0] + prices[1]) / 2)
    elif i == len(prices) - 1:
        new_prices[i] = int((prices[i] + prices[i - 1]) / 2)
    else:
        new_prices[i] = int((prices[i - 1] + prices[i] + prices[i + 1]) / 3)

for i in new_prices:
    print(i, end=' ')

python真的简洁,但是java是我的大老婆,还是java用的熟练啊,下一次还是用java打的。

猜你喜欢

转载自blog.csdn.net/AivenZhong/article/details/83341623