ccf/csp第一题java的实现

import java.util.*;//包和类的导入,导入标准输入输出库
public class example3
{
	public static void main(String[] args)
	{
		Scanner sc=new Scanner(System.in);
		String s;//定义String型s
		
		s=sc.nextLine();//将s作为输入,运行时都会停留直到输入enter键
		int n=Integer.parseInt(s);//强制类型转换,将String型的转换为int型,不直接使用int是因为int会产生数据的输入错误
		
		String[] s_1=sc.nextLine().split(" ");//通过此方式产生一个名为S_1,只有通过split(" ")的方法才能输入空格的数组
		int[] store=new int[n];
		for(int i=0;i<n;i++)
		{
			store[i]=Integer.parseInt(s_1[i]);//强制类型转换,由于空格已经没有了,就可以一一对应了,上面时去除空格的方法
		}
		int[] newstore=new int[n];//定义一个int的新数组
		for(int i=0;i<n;i++)
		{
			if(i==0)
			{
				newstore[i]=(store[i]+store[i+1])/2;
			}
			else if(i==(n-1))
			{
				newstore[i]=(store[i]+store[i-1])/2;
			}
			else
			{
				newstore[i]=(store[i]+store[i-1]+store[i+1])/3;
			}
		}
		for(int i=0;i<n;i++)
		{
			if(i<n-1)
			{
				System.out.print(newstore[i]+" ");//保证输出格式的正确化
			}
			else
			{
				System.out.print(newstore[i]);
			}
		}
	}
	
	
}

猜你喜欢

转载自blog.csdn.net/qq_40741513/article/details/84000641