CCF CSP 卖菜 Java python csp201809_1 100分

CCF CSP 卖菜 Java python csp201809_1 100分

问题输入格式
       在一条街上有n个卖菜的商店,按1至n的顺序排成一排,这些商店都卖一种蔬菜。
       第一天,每个商店都自己定了一个价格。店主们希望自己的菜价和其他商店的一致,第二天,每一家商店都会根据他自己和相邻商店的价格调整自己的价格。具体的,每家商店都会将第二天的菜价设置为自己和相邻商店第一天菜价的平均值(用去尾法取整)。
       注意,编号为1的商店只有一个相邻的商店2,编号为n的商店只有一个相邻的商店n-1,其他编号为i的商店有两个相邻的商店i-1和i+1。
       给定第一天各个商店的菜价,请计算第二天每个商店的菜价。
输入格式
  输入的第一行包含一个整数n,表示商店的数量。
  第二行包含n个整数,依次表示每个商店第一天的菜价。
输出格式
  输出一行,包含n个正整数,依次表示每个商店第二天的菜价。
样例输入
8
4 1 3 1 6 5 17 9
样例输出
2 2 1 3 4 9 10 13
数据规模和约定
  对于所有评测用例,2 ≤ n ≤ 1000,第一天每个商店的菜价为不超过10000的正整数。
问题分析
       这道题就是一个简单的循环,只需要注意边界条件就可以了。
Java代码如下:

import java.util.Scanner;

public class csp201809_1 {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int number = input.nextInt();
        int[] array = new int[number];
        for(int i = 0;i < array.length;i++){
            array[i] = input.nextInt();
        }
        int[] res = new int[number];
        for(int i = 0;i < array.length;i++){
            if(i == 0){
                res[i] = (array[i] + array[i + 1]) / 2;
            }
            else if(i == number - 1){
                res[i] = (array[i] + array[i - 1]) / 2;
            }
            else{
                res[i] = (array[i - 1] + array[i] + array[i + 1]) / 3;
            }
        }
        for(int i = 0;i < res.length;i++){
            System.out.print(res[i] + " ");
        }
    }
}

Python3代码如下:

number = input()
list1 = input().split(" ")
array = []
num = (int)(number)
for i in range(num):
    array.append((int)(list1[i]))
res = []
temp = 0
for i in range(num):
    if i == 0:
        temp = (int)((array[i] + array[i + 1]) / 2)
        res.append(temp)
    elif i == num - 1:
        temp = (int)((array[i] + array[i - 1]) / 2)
        res.append(temp)
    else:
        temp = (int)((array[i - 1] + array[i] + array[i + 1]) / 3)
        res.append(temp)
print(" ".join(str(i) for i in res))


ok!大功告成了,如果你有更好的方法,可以在评论区交流啊!

发布了19 篇原创文章 · 获赞 13 · 访问量 4085

猜你喜欢

转载自blog.csdn.net/qq_38929464/article/details/87997496
今日推荐