Advertisement
jenrus

Задача №70. Переставить соседние элементы

Oct 18th, 2018
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.56 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class hw2{
  4.     public static void main(String[] args){
  5.         Scanner scan = new Scanner(System.in);
  6.         int b = scan.nextInt();
  7.         int[] a = new int[b];
  8.         int k = 0;
  9.         for(int i=0; i<a.length; i++){
  10.             a[i]=scan.nextInt();
  11.         }
  12.         if(a.length%2==1){
  13.             for(int i=0; i<a.length-1; i+=2){
  14.                 int t = a[i];
  15.                 a[i] = a[i+1];
  16.                 a[i+1] = t;
  17.             }
  18.         }else{
  19.             for(int i=0; i<a.length; i+=2){
  20.                 int t = a[i];
  21.                 a[i] = a[i+1];
  22.                 a[i+1] = t;
  23.             }
  24.         }
  25.         for(int i=0; i<a.length; i++){
  26.             System.out.print(a[i]+" ");
  27.         }
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement