蓝桥杯--算法训练 数据交换(java)

在这里插入图片描述
这是一道很无语的题。。。。这个swap 函数压根用不到,因为java是值传递,经过swap函数操作之后的 x 和 y 并没有变化。

import java.util.Scanner;

public class Main {
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int x = sc.nextInt();
		int y = sc.nextInt();
        swap(x, y);
        System.out.println(y + " " + x);
		sc.close();
	}
	public static void swap(int x, int y) {
		int tmp = x;
		x = y;
		y = tmp;
	}
}

正如下面的提交了一次仍然是100分,一个空的swap函数。。。

import java.util.Scanner;

public class Main {
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int x = sc.nextInt();
		int y = sc.nextInt();
        System.out.println(y + " " + x);
		sc.close();
	}
	public static void swap(int x, int y) {
		
	}
}

发布了47 篇原创文章 · 获赞 4 · 访问量 2921

猜你喜欢

转载自blog.csdn.net/QinLaoDeMaChu/article/details/104092423
今日推荐