ccf/csp第一題java的實現
阿新 • • 發佈:2018-12-13
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]); } } } }