英文:
Writing a program that, if given an array of characters, will output array that has the indices in order from largest to smallest (java)
问题
public static void main(String[]args){
        char[] a ={'f','a','z','v','t','u','i','j','o','c'};
        int[] b = new int[a.length];
      
        // Sort a
        for(int i = 0; i <= a.length - 2; i++){
            
            int maxindex = i;
            
            for(int j = maxindex + 1; j < a.length; j++){
                if(a[j] > a[maxindex]){
                    maxindex = j;
                }
            }
            // swap
            char temp = a[i];
            a[i] = a[maxindex];
            a[maxindex] = temp;
            
            b[i] = maxindex; // Store the index in the corresponding position of array b
        }
        for(int i = 0; i < a.length; i++){
            System.out.print(a[i] + " ");
        }
    }
英文:
So I'm working through a problem that if I have an array of characters it wants me to arrange them in order from largest to smallest but by the index not the actual letter. so ex, a[] = { 'a', 'z', 'p'} the output array would be like `b[] = {1 , 2, 0}
I understand how to sort the array of characters into descending order but now I'm confused on how to relate the two arrays together. I'm thinking I need to copy the original array into another array and then compare them someway. But im confused on the steps i need to take to get there. My sorting code is below just for reference. if anyone can help me out that would be great. Thanks
public static void main(String[]args){
       char[] a ={'f','a','z','v','t','u','i','j','o','c'};
       int[] b ;
     
     
     
       // Sort a
       for(int i= 0; i <= a.length-2;i++){
           
           int maxindex = i;
           
           for(int j = maxindex+1; j<a.length; j++){
               if(a[j]>a[maxindex]){
                   maxindex = j;
               }
           }
           // swap
           char temp = a[i];
           a[i] = a[maxindex];
           a[maxindex] = temp;
       }
       for(int i = 0; i < a.length; i++){
           System.out.print(a[i] + " ");
       }
   }
答案1
得分: 0
// 你好!比我想象的要棘手一些。下面是我所做的:
// 1. 从原数组中制作一份副本。
// 2. 对原数组进行排序。
// 3. 比较两个数组,以获取具有索引的数组。
// 就像你想的那样。我认为你可以使用其他类型的数据结构来简化代码,但如果这是为了练习Java,那也没问题。
char[] sorted = {'z', 'f', 'z', 'v', 't', 'u', 'i', 'j', 'o', 'k', 'b'};
// 让我们制作一个副本
char[] copy = sorted.clone();
// 使用与'sorted'数组相同大小的整数数组初始化'b'
int[] b = new int[sorted.length];
// 对原始数组进行排序(这是你的代码)
for (int i = 0; i <= sorted.length - 2; i++) {
    
    int maxindex = i;
    
    for (int j = maxindex + 1; j < sorted.length; j++) {
        if (sorted[j] > sorted[maxindex]) {
            maxindex = j;
        }
    }
    // 交换
    char temp = sorted[i];
    sorted[i] = sorted[maxindex];
    sorted[maxindex] = temp;
}
// 看看我们得到了什么
for (int i = 0; i < sorted.length; i++) {
    System.out.print(sorted[i] + " ");
}
// 现在比较原始和排序后的数组,然后获取索引数组
for (int i = 0; i < sorted.length; i++) {
    for (int k = 0; k < copy.length; k++) {
        if ((copy[k] == sorted[i] && i == 0) || (copy[k] == sorted[i] && k != b[i - 1])) {
            b[i] = k;
            break;
        }
    }
}
System.out.print("\n");
// 看看索引数组
for (int i = 0; i < b.length; i++) {
    System.out.print(b[i] + " ");
}
英文:
<!-- language: lang-java -->
Hi! It was a bit more challenging than I thought. Here is what I did:
- Make a copy from the original array.
 - Sort the original array.
 - Compare the two arrays so you can get the array with indexes
 
It's just like you thought. I think you could simplify the code using other types of structures, but it's fine if it is to practice java
char[] sorted = {'z','f','z','v','t','u','i','j','o','k','b'}; 
// Let's copy it
char[] copy = sorted.clone();
// initialize b as an array of integer with the same size as 'sorted' array
int[] b= new int[sorted.length];
// Sort your original array (this is your code)
for(int i= 0; i <= sorted.length-2;i++){
int maxindex = i;
for(int j = maxindex+1; j<sorted.length; j++){
if(sorted[j]>sorted[maxindex]){
maxindex = j;
}
}
// swap
char temp = sorted[i];
sorted[i] = sorted[maxindex];
sorted[maxindex] = temp;
}
// let's see what we got
for(int i = 0; i < sorted.length; i++){
System.out.print(sorted[i] + " ");
}
// now let's compare the original vs the sorted and get the indexes array
for(int i = 0; i < sorted.length; i++){
for(int k = 0; k < copy.length; k++){
if ((copy[k] == sorted[i] && i == 0)  || (copy[k] == sorted[i] && k != b[i - 1]) ) {
b[i] = k; 
break;
}				  
}
}
System.out.print("\n");
// let's see the indexes array
for(int i = 0; i < b.length; i++){
System.out.print(b[i] + " ");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论