zoukankan      html  css  js  c++  java
  • Java实现 蓝桥杯VIP 算法提高 交换Easy

    算法提高 交换Easy
    时间限制:1.0s 内存限制:512.0MB
    问题描述
      给定N个整数组成的序列,每次交换当前第x个与第y个整数,要求输出最终的序列。
    输入格式
      第一行为序列的大小N(1<=N<=1000)和操作个数M(1<=M<=1000)。
      第二行包含N个数字,表示初始序列。
      接下来M行,每行两个整数x,y (1<=x,y<=N),表示要交换的两个整数。在一次交换中,如果x和y相等,则不会改变序列的内容。
    输出格式
      输出N行,为交换后的序列中的数。
    样例输入
    5 2
    1 2 3 4 5
    1 2
    3 4
    样例输出
    2
    1
    4
    3
    5

    import java.util.Scanner;
    
    
    public class 交换Easy {
    	static int a[]=new int[1001];
    	static int b[]=new int[2];
        public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            int n=sc.nextInt();
            int m=sc.nextInt();
            for(int i=0;i<n;i++)
            	a[i]=sc.nextInt(); 
            for(int j=0;j<m;j++){
            	b[0]=sc.nextInt();
            	b[1]=sc.nextInt();
            	int x=b[0],y=b[1];
            	if(x!=y){
                	int t=a[x-1];
                	a[x-1]=a[y-1];
                	a[y-1]=t;
                }
            	else
            		a[x-1]=a[y-1];
            }    	        	
            for(int i=0;i<n;i++)
            	System.out.println(a[i]);             
        }
    
    }
    
    
  • 相关阅读:
    JVM类加载(3)—初始化
    JVM类加载(1)—加载
    SQL笔记
    html5离线Web应用
    推荐3个很好的html5 网址
    HTML 5 File API应用实例
    异常与错误的区别
    html5Local Storage(本地存储)
    HTML5 js api 新的选择器
    5个HTML5 API
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13078814.html
Copyright © 2011-2022 走看看