1. 程式人生 > >CCF-201809-1-賣菜

CCF-201809-1-賣菜

思路:
這次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打的。