Writing a program that, if given an array of characters, will output array that has the indices in order from largest to smallest (java)

huangapple go评论101阅读模式
英文:

Writing a program that, if given an array of characters, will output array that has the indices in order from largest to smallest (java)

问题

  1. public static void main(String[]args){
  2. char[] a ={'f','a','z','v','t','u','i','j','o','c'};
  3. int[] b = new int[a.length];
  4. // Sort a
  5. for(int i = 0; i <= a.length - 2; i++){
  6. int maxindex = i;
  7. for(int j = maxindex + 1; j < a.length; j++){
  8. if(a[j] > a[maxindex]){
  9. maxindex = j;
  10. }
  11. }
  12. // swap
  13. char temp = a[i];
  14. a[i] = a[maxindex];
  15. a[maxindex] = temp;
  16. b[i] = maxindex; // Store the index in the corresponding position of array b
  17. }
  18. for(int i = 0; i < a.length; i++){
  19. System.out.print(a[i] + " ");
  20. }
  21. }
英文:

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[] = { &#39;a&#39;, &#39;z&#39;, &#39;p&#39;} 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

  1. public static void main(String[]args){
  2. char[] a ={&#39;f&#39;,&#39;a&#39;,&#39;z&#39;,&#39;v&#39;,&#39;t&#39;,&#39;u&#39;,&#39;i&#39;,&#39;j&#39;,&#39;o&#39;,&#39;c&#39;};
  3. int[] b ;
  4. // Sort a
  5. for(int i= 0; i &lt;= a.length-2;i++){
  6. int maxindex = i;
  7. for(int j = maxindex+1; j&lt;a.length; j++){
  8. if(a[j]&gt;a[maxindex]){
  9. maxindex = j;
  10. }
  11. }
  12. // swap
  13. char temp = a[i];
  14. a[i] = a[maxindex];
  15. a[maxindex] = temp;
  16. }
  17. for(int i = 0; i &lt; a.length; i++){
  18. System.out.print(a[i] + &quot; &quot;);
  19. }
  20. }

答案1

得分: 0

  1. // 你好!比我想象的要棘手一些。下面是我所做的:
  2. // 1. 从原数组中制作一份副本。
  3. // 2. 对原数组进行排序。
  4. // 3. 比较两个数组,以获取具有索引的数组。
  5. // 就像你想的那样。我认为你可以使用其他类型的数据结构来简化代码,但如果这是为了练习Java,那也没问题。
  6. char[] sorted = {'z', 'f', 'z', 'v', 't', 'u', 'i', 'j', 'o', 'k', 'b'};
  7. // 让我们制作一个副本
  8. char[] copy = sorted.clone();
  9. // 使用与'sorted'数组相同大小的整数数组初始化'b'
  10. int[] b = new int[sorted.length];
  11. // 对原始数组进行排序(这是你的代码)
  12. for (int i = 0; i <= sorted.length - 2; i++) {
  13. int maxindex = i;
  14. for (int j = maxindex + 1; j < sorted.length; j++) {
  15. if (sorted[j] > sorted[maxindex]) {
  16. maxindex = j;
  17. }
  18. }
  19. // 交换
  20. char temp = sorted[i];
  21. sorted[i] = sorted[maxindex];
  22. sorted[maxindex] = temp;
  23. }
  24. // 看看我们得到了什么
  25. for (int i = 0; i < sorted.length; i++) {
  26. System.out.print(sorted[i] + " ");
  27. }
  28. // 现在比较原始和排序后的数组,然后获取索引数组
  29. for (int i = 0; i < sorted.length; i++) {
  30. for (int k = 0; k < copy.length; k++) {
  31. if ((copy[k] == sorted[i] && i == 0) || (copy[k] == sorted[i] && k != b[i - 1])) {
  32. b[i] = k;
  33. break;
  34. }
  35. }
  36. }
  37. System.out.print("\n");
  38. // 看看索引数组
  39. for (int i = 0; i < b.length; i++) {
  40. System.out.print(b[i] + " ");
  41. }
英文:

<!-- language: lang-java -->
Hi! It was a bit more challenging than I thought. Here is what I did:

  1. Make a copy from the original array.
  2. Sort the original array.
  3. 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

  1. char[] sorted = {&#39;z&#39;,&#39;f&#39;,&#39;z&#39;,&#39;v&#39;,&#39;t&#39;,&#39;u&#39;,&#39;i&#39;,&#39;j&#39;,&#39;o&#39;,&#39;k&#39;,&#39;b&#39;};
  2. // Let&#39;s copy it
  3. char[] copy = sorted.clone();
  4. // initialize b as an array of integer with the same size as &#39;sorted&#39; array
  5. int[] b= new int[sorted.length];
  6. // Sort your original array (this is your code)
  7. for(int i= 0; i &lt;= sorted.length-2;i++){
  8. int maxindex = i;
  9. for(int j = maxindex+1; j&lt;sorted.length; j++){
  10. if(sorted[j]&gt;sorted[maxindex]){
  11. maxindex = j;
  12. }
  13. }
  14. // swap
  15. char temp = sorted[i];
  16. sorted[i] = sorted[maxindex];
  17. sorted[maxindex] = temp;
  18. }
  19. // let&#39;s see what we got
  20. for(int i = 0; i &lt; sorted.length; i++){
  21. System.out.print(sorted[i] + &quot; &quot;);
  22. }
  23. // now let&#39;s compare the original vs the sorted and get the indexes array
  24. for(int i = 0; i &lt; sorted.length; i++){
  25. for(int k = 0; k &lt; copy.length; k++){
  26. if ((copy[k] == sorted[i] &amp;&amp; i == 0) || (copy[k] == sorted[i] &amp;&amp; k != b[i - 1]) ) {
  27. b[i] = k;
  28. break;
  29. }
  30. }
  31. }
  32. System.out.print(&quot;\n&quot;);
  33. // let&#39;s see the indexes array
  34. for(int i = 0; i &lt; b.length; i++){
  35. System.out.print(b[i] + &quot; &quot;);
  36. }

huangapple
  • 本文由 发表于 2020年10月5日 04:31:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/64199600.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定