kotlin键值对数组_Kotlin程序以降序对数组进行排序

news/2024/5/19 23:51:37 标签: python, 数据结构, 快速排序, h.264, hashmap

kotlin键值对数组

Given an array, we have to sort its elements in descending order.

给定一个数组,我们必须按降序对其元素进行排序。

Example:

例:

    Input:
    arr = [10, 20, 5, 2, 30]

    Output:
    sorted array (Descending Order): [30, 20, 10, 5, 2]

在Kotlin中按降序对数组进行排序的程序 (Program to sort an array in descending order in Kotlin)

package com.includehelp.basic

import java.util.*

//Main Function entry Point of Program
fun main(args: Array<String>) {
    //Input Stream
    val s = Scanner(System.`in`)

    //Input Array Size
    print("Enter number of elements in the array: ")
    val size = s.nextInt()

    //Create Integer array of Given size
    val intArray = IntArray(size)

    //Input array elements
    println("Enter Arrays Elements:")
    for (i in intArray.indices) {
        print("intArray[$i] : ")
        intArray[i] = s.nextInt()
    }

    //to Perform Descending Order Sorting on integer Array
    var temp:Int
    for (i in intArray.indices) {
        for (j in i + 1 until intArray.size) {
            if (intArray[i] < intArray[j]) {
                temp = intArray[i]
                intArray[i] = intArray[j]
                intArray[j] = temp
            }
        }
    }

    //Alternatively we can also use sortDescending() method 
    //of Arrays Class in kotlin to sort in Descending Order
    //intArray.sortDescending()

    print("Descending Order: ")
    for (item in intArray) {
        print("$item ")
    }
}

Output

输出量

Run 1:
-----
Enter number of elements in the array: 8
Enter Arrays Elements:
intArray[0] : 34
intArray[1] : 6
intArray[2] : -98
intArray[3] : 0
intArray[4] : 12
intArray[5] : 7
intArray[6] : 7
intArray[7] : 45
Descending Order: 45 34 12 7 7 6 0 -98
----------------
Enter number of elements in the array: 7
Enter Arrays Elements:
intArray[0] : 12
intArray[1] : 34
intArray[2] : 56
intArray[3] : 78
intArray[4] : 90
intArray[5] : -65
intArray[6] : 032
Descending Order: 90 78 56 34 32 12 -65


翻译自: https://www.includehelp.com/kotlin/sort-an-array-in-descending-order.aspx

kotlin键值对数组


http://www.niftyadmin.cn/n/1255267.html

相关文章

PE——节表结构

联合体 节表数据结构 ① Name&#xff1a;8个字节&#xff0c;内容可自定义&#xff0c;需要注意的是该名称并不遵守必须以"\0"结尾的规律&#xff0c;如果不是以"\0"结尾&#xff0c;系统会截取8个字节的长度进行处理.我们在读取的时候需要小心&#xff…

Hbase-2.0.0_01_安装部署

该文章是基于 Hadoop2.7.6_01_部署 进行的 1. 主机规划 主机名称 IP信息 内网IP 操作系统 安装软件 备注&#xff1a;运行程序 mini01 10.0.0.11 172.16.1.11 CentOS 7.4 jdk、hadoop、zookeeper、hbase QuorumPeerMain、NameNode、HMaster mini02 10.0.0.12 172…

线性代数 行矩阵的值_矩阵的最小值| 使用Python的线性代数

线性代数 行矩阵的值Prerequisites: 先决条件&#xff1a; Defining a Matrix 定义矩阵 Here, we are implementing a Python program to find the minimum value from a matrix or vector using an inbuilt function in the numpy library. 在这里&#xff0c;我们正在实现一…

0030-购买衣服

题目 购买衣服难度级别&#xff1a;A&#xff1b; 运行时间限制&#xff1a;1000ms&#xff1b; 运行空间限制&#xff1a;51200KB&#xff1b; 代码长度限制&#xff1a;2000000B 试题描述衣服每件 95 元&#xff0c;若消费满300元&#xff0c;可打八五折。输入购买衣服的件数…

t != (t = tail)_一品解读 超德系动力油耗却比日系还低? 全新Jeep指南者1.3T成紧凑SUV最强者!...

——逆流而上&#xff0c;逆势增长&#xff0c;从去年以来中国SUV车市已经由增量市场转为存量市场&#xff0c;车企间的竞争将进一步加剧&#xff0c;这对每个车企来说都是严峻的考验。但就在遭遇寒潮的车市中&#xff0c;广汽菲克Jeep仍然凭借旗下产品的强大竞争力&#xff0c…

微控制器,微处理器_微控制器与微处理器

微控制器,微处理器The terms microcontroller and microprocessor are commonly misinterpreted to be the same thing even though they have wide areas of difference. The devices have real-time application in many fields. It is true that they share quite a number …

Find a way HDU - 2612

Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki. Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city.…

追风华为商城抢购手机软件_华为Mate30系列5G版,中州通讯手机连锁官网价现货开售!...

5G时代已来HUAWEI Mate 30系列5G版中州通讯手机连锁官网价现货开售就在今天&#xff0c;HUAWEI Mate 30系列5G版正式开售。作为全球首款第二代5G手机&#xff0c;TA有哪些硬核实力&#xff1f;接下来&#xff0c;就为你一一道来。01搭载全球首款旗舰5G SoC重构&#xff0c;芯片…