首页 > 编程语言 >C# 获取数组排序后的下标

C# 获取数组排序后的下标

时间:2023-06-14 13:55:20浏览次数:37  
标签:src 下标 index C# System int Console 排序 idxArr

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp9
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] src = new int[] { 2, 1, 0, 4, 1, 3, 3, -1, -1 };
            List<int> idxArr = new List<int>();

            for (int i = 0; i < src.Length; i++)
            {
                int index = 0;
                int val = src[index];
                while (idxArr.Contains(index))
                {
                    index++;
                    val = src[index];
                }

                for (int j = 0; j < src.Length; j++)
                {
                    if (idxArr.Contains(j))
                    {
                        continue;
                    }

                    if (src[j] < val)
                    {
                        index = j;
                        val = src[j];
                    }
                }

                idxArr.Add(index);
            }

            foreach (int idx in idxArr)
            {
                Console.Write(src[idx] + " ");
            }
            Console.WriteLine();
            foreach(int idx in idxArr)
            {
                Console.Write(idx + " ");
            }
            Console.WriteLine();

            Console.ReadKey();
        }
    }
}

输出:

-1 -1 0 1 1 2 3 3 4
7 8 2 1 4 0 5 6 3

标签:src,下标,index,C#,System,int,Console,排序,idxArr
From: https://www.cnblogs.com/huvjie/p/17479986.html

相关文章

  • 曲线艺术编程 coding curves 第十章 螺旋曲线(SPIRALS)
    原作:KeithPetershttps://www.bit-101.com/blog/2022/11/coding-curves/译者:池中物王二狗(sheldon)源码:github:https://github.com/willian12345/coding-curves第十章螺旋(SPIRALS)曲线艺术编程系列第10章来聊聊螺旋线。螺旋非常像圆,它是一组点到定圆中心点的距离......
  • repo clean
    repo没有clean这个命令,如果需要清洁路径,删除没有追踪的文件,需要执行repoforall-c"gitclean-df"将repo路径里的所有文件完全恢复到最初状态,需要执行repoforall-c"gitreset--hardHEAD"repoforall-c"gitclean-df" ......
  • C++ 中的运算符重载
     您可以重定义或重载大部分C++内置的运算符。这样,您就能使用自定义类型的运算符。重载的运算符是带有特殊名称的函数,函数名是由关键字operator和其后要重载的运算符符号构成的。与其他函数一样,重载运算符有一个返回类型和一个参数列表。https://www.tzffs.com/lnzt15/......
  • C++ 多态
     多态按字面的意思就是多种形态。当类之间存在层次结构,并且类之间是通过继承关联时,就会用到多态。C++多态意味着调用成员函数时,会根据调用函数的对象的类型来执行不同的函数。下面的实例中,基类Shape被派生为两个类https://www.tzffs.com/mnst14/......
  • CSS动画和过渡的区别
    CSS动画和过渡都是用来实现网页元素的动态效果,但它们在实现方式和应用场景上有一些区别。CSS过渡(CSSTransitions)是在元素状态改变时实现平滑过渡效果的一种机制。过渡通过指定元素的属性变化,使元素从一个状态平滑地过渡到另一个状态。过渡通常用于响应用户操作、交互效果或状态......
  • container.io 相关命令
    ctr命令[containerd本身的CLI]1.查看命名空间列表ctrnsls2.导入镜像[-n用于指定命令空间,不指定的话默认default,则在k8s的crictl中看不到导入的镜像]ctr-n=k8s.ioimageimportcoreqi.tar3.查看镜像列表ctrimagesls4.查看容器列表ctrcls5.导出镜像ctrima......
  • AtCoder Beginner Contest 215 H Cabbage Master
    洛谷传送门AtCoder传送门考虑第一问。发现这个东西长得很像二分图匹配,考虑建图,第\(i\)个盒子建\(b_i\)个左部点,第\(i\)个球建\(a_i\)个右部点,每个盒子的每个点往可以放的球的每个点连边。显然要求能被满足等价于,这个二分图存在一个左部点的完全匹配。因为一个盒子的......
  • vue watch deep 用法
    简单案例<template><div><h1>watchdeep</h1><p>obj:{{obj}}</p><p>调用watch次数:{{times}}</p><button@click="chgObj">改变对象</button></div></t......
  • Acwing 4440 照相
    Acwing4440照相原题指路因为序列长为偶数,考虑将牛进行两两分组为什么要将其进行两两分组:因为题目按偶数前缀进行反转,每一组中的牛总是相邻的,不会被拆散。两两分组后会有四种情况:GGHHGHHG我们再观察可得:每次反转,就是将每组内的两头牛进行互换如:而GGHH反转并......
  • VSCode - Replace Tab with Spaces
    PressF1.Input'indentationtospaces'.SetaKeyBindingtothecommand'ConvertIndentationtoSpaces':Exitandre-openVSCodetomakethesettingtakeeffect.......