参考代码:
import java.util.Scanner; public class 删除0元素 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub //2240: [蓝桥杯][算法训练]删除数组零元素 Scanner scanner=new Scanner(System.in); int n=scanner.nextInt(); int [] a=new int [n]; int count=0; for (int i = 0; i < a.length; i++) { a[i]=scanner.nextInt(); } for (int i = 0; i < a.length; i++) { if (a[i]!=0) { count++; } } System.out.println(count); for (int i = 0; i < a.length; i++) { if (a[i]!=0) { System.out.print(a[i]+" "); } } } }
0.0分
2 人评分