7-7 输出数组元素 (15 分)
本题要求编写程序,对顺序读入的n个整数,顺次计算后项减前项之差,并按每行三个元素的格式输出结果。
输入格式:
输入的第一行给出正整数n(1<n≤10)。随后一行给出n个整数,其间以空格分隔。
输出格式:
顺次计算后项减前项之差,并按每行三个元素的格式输出结果。数字间空一格,行末不得有多余空格。
输入样例:
10
5 1 7 14 6 36 4 28 50 100
输出样例:
-4 6 7
-8 30 -32
24 22 50
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<a.length;i++){
a[i]=sc.nextInt();
}
for (int i = 1; i < n; i++) {
if(i%3==0 || i==n-1){
System.out.print(a[i]-a[i-1]);}
else{System.out.print(a[i]-a[i-1]+" ");}
if(i%3==0 && i!=n-1){
System.out.println();//找出除最后一行的数,根据题目要求要加空格
}
}
}
}