博客
关于我
杭电oj 2020 绝对值排序 (Java)(注意换行符在oj上的特别要求)
阅读量:377 次
发布时间:2019-03-05

本文共 923 字,大约阅读时间需要 3 分钟。

杭电oj 2020 绝对值排序

Problem Description

输入n(n<=100)个整数,按照绝对值从大到小排序后输出。题目保证对于每一个测试实例,所有的数的绝对值都不相等。

Input

输入数据有多组,每组占一行,每行的第一个数字为n,接着是n个整数,n=0表示输入数据的结束,不做处理。

Output

对于每个测试实例,输出排序后的结果,两个数之间用一个空格隔开。每个测试实例占一行。

Sample Input

3 3 -4 2
4 0 1 2 -3
0

Sample Output

-4 3 2

-3 2 1 0

Author

lcy

import java.util.Scanner;public class Main{   	public static void main(String[] args){   		Scanner input=new Scanner(System.in);		int i,n,b,k;		boolean run=true;		int[] arr=new int[100];		while(run){   			n=input.nextInt();			if(n==0) break;   //输入结束			for(i=0;i
i;k--){ //从数组最后一个元素开始访问比较绝对值大小 b=arr[i]; arr[i]=Math.abs(arr[i])>Math.abs(arr[k])? arr[i]:arr[k]; //确保arr[i]绝对值最大 arr[k]=arr[i]==arr[k]?b:arr[k]; //确保arr[i]与arr[k]双向交换 } } System.out.print(arr[0]); for(i=1;i

【注意】在杭电oj上,输出的时候,\n需用%n,print改为printf。

即:System.out.printf("%n");
或者:System.out.println("");
用杭电oj提交的时候System.out.print("\n");会出现PE错误。

转载地址:http://crpg.baihongyu.com/

你可能感兴趣的文章
nodejs学习笔记一——nodejs安装
查看>>
vue3+Element-plus icon图标无法显示的问题(已解决)
查看>>
NodeJS实现跨域的方法( 4种 )
查看>>
nodejs封装http请求
查看>>
nodejs常用组件
查看>>
nodejs开发公众号报错 40164,白名单配置找不到,竟然是这个原因
查看>>
Nodejs异步回调的处理方法总结
查看>>
NodeJS报错 Fatal error: ENOSPC: System limit for number of file watchers reached, watch ‘...path...‘
查看>>
nodejs支持ssi实现include shtml页面
查看>>
Nodejs教程09:实现一个带接口请求的简单服务器
查看>>
nodejs服务端实现post请求
查看>>
nodejs框架,原理,组件,核心,跟npm和vue的关系
查看>>
Nodejs概览: 思维导图、核心技术、应用场景
查看>>