排列元素0、1和2的数组,以使该数组将所有0放在第一位,然后将所有1和2放在最后

时间:2019-04-22 12:35:34

标签: c++

我需要编写一个C ++程序,提示用户在数组中输入10个整数。 输入数字为:0、1或2。 程序应提取在屏幕上输入的数组。排列元素0、1和2的数组,以便该数组将所有0放在第一位,然后将所有1放在首位。 最后所有2个。 屏幕上显示排列的数组。

现在已经为此奋斗了几个小时。我卡住了,不知道如何显示Output。 输入应为0 0 1 0 1 2 2 2 0 1 输出0000111222 怎么?

int main ()
{
  int t [N], i, ;
  cout << "Enter 10 arrays from 0-2" << endl;
  cout << "Original array:";

  for (i = 0; i <N; i ++)
  {
    cin >> t [i];
  }

  if (t [i]> = 0 && t [i] <= 2)
  {
    cout << "Rearranging elements of array:" << ? << endl;
  }
  cout << "End of the program!"

  return 0;
}

3 个答案:

答案 0 :(得分:2)

什么时候做

 if (t [i]> = 0 && t [i] <= 2)

i等于N,因此您可以访问数组之外​​的内容,并且当然不会对数组进行排序

您不会检查cin >> t[i]是否成功,因此如果用户输入的内容不是int,则不会设置当前的所有条目(如果您输入0,使用std::vector


第一种方法是不考虑范围0..2,将int t[n]替换为std::vector<int> t(N),然后使用sort(t.begin(), t.end())对数组进行排序

复杂度为O(N*log(N))(此处N为10)

例如:

#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

#define N 10

int main ()
{
  vector<int> t(N);
  size_t i; // size_t the right type for an index

  cout << "Enter 10 values in range 0..2" << endl;

  for (i = 0; i < N; ++i)
  {
    for (;;) {
      cout << "value #" << i << ':';

      if (!(cin >> t[i])) {
        cerr << "not a number" << endl;
        cin.clear(); // raz error

        string s;

        cin >> s; // skip bad input
      }
      else if ((t[i] < 0) || (t[i] > 2))
        cerr << "value out of range" << endl;
      else
        break;
    }
  }

  cout << "Original array:";
  for (i = 0; i < N; ++i) cout << ' ' << t[i]; // old way to do
  cout << endl;

  sort(t.begin(), t.end());

  cout << "Sorted array:";
  for (auto v : t) cout << ' ' << v; // new way to do
  cout << endl;

  cout << "End of the program!" << endl;

  return 0;
}

编译和执行:

pi@raspberrypi:/tmp $ g++ -pedantic -Wextra -Wall s.cc
pi@raspberrypi:/tmp $ ./a.out
Enter 10 values in range 0..2
value #0:aze
not a number
value #0:-2
value out of range
value #0:3
value out of range
value #0:2
value #1:0
value #2:1
value #3:2
value #4:0
value #5:2
value #6:1
value #7:0
value #8:0
value #9:1
Original array: 2 0 1 2 0 2 1 0 0 1
Sorted array: 0 0 0 0 1 1 1 2 2 2
End of the program!

考虑范围[min .. max]不太大的第二种方法是计算每个值的数量,然后填充数组以遵守这些计数

复杂度为O(2N)(此处N为10)

例如:

#include <vector>
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

#define MIN 0
#define MAX 2
#define N 10

int main ()
{
  vector<int> t(N);
  size_t i; // size_t the right type for an index

  cout << "Enter 10 values in range " << MIN << ".." << MAX << endl;

  for (i = 0; i < N; ++i)
  {

    for (;;) {
      cout << "value #" << i << ':';

      if (!(cin >> t[i])) {
        cerr << "not a number" << endl;
        cin.clear(); // raz error

        string s;

        cin >> s; // skip bad input
      }
      else if ((t[i] < MIN) || (t[i] > MAX))
        cerr << "value out of range" << endl;
      else
        break;
    }
  }

  cout << "Original array:";
  for (auto v : t) cout << ' ' << v;
  cout << endl;

  // count numbers
  vector<size_t> counts(MAX - MIN + 1);

  for (auto v : t) counts[v - MIN] += 1;

  // fill again
  i = 0;

  for (int r = MIN; r <= MAX; ++r) {
    size_t n = counts[r - MIN];

    while (n--) t[i++] = r;
  }

  cout << "Sorted array:";
  for (auto v : t) cout << ' ' << v;
  cout << endl;

  cout << "End of the program!" << endl;

  return 0;
}

编译和执行:

pi@raspberrypi:/tmp $ g++ -pedantic -Wextra -Wall s2.cc
pi@raspberrypi:/tmp $ ./a.out
Enter 10 values in range 0..2
value #0:a
not a number
value #0:3
value out of range
value #0:0
value #1:2
value #2:1
value #3:1
value #4:2
value #5:2
value #6:2
value #7:0
value #8:1
value #9:2
Original array: 0 2 1 1 2 2 2 0 1 2
Sorted array: 0 0 1 1 1 2 2 2 2 2
End of the program!

具体来说,如@PaulMcKenzie在评论中所述,对于0到2之间的值(实际上是3个可能的值),您可以使用Dutch national flag problem并看一下这个问题:R G B element array swap

复杂度为O(N)(此处N为10)

答案 1 :(得分:0)

作为初学者,您可能想尝试一下(这是我不使用其他库就可以想到的最简单的解决方案):

int main () {
const int size = 10;
int arr[size], temp = 0;
for (int i = 0; i < size; i++) {
    cin >> arr[i];
}
for (int j = 0; j < size - 1; j++) {
    for (int k = 0; k < size - j - 1; k++) {
        if(arr[k] > arr[k+1]) {
            temp = arr[k];
            arr[k] = arr[k+1];
            arr[k+1] = temp;
        }
        else
            continue;
    }
}

for (int i = 0; i < size; i++) {
    cout << arr[i] << " ";
}
return 0;
}

希望对您有帮助。

答案 2 :(得分:0)

第一个答案对我来说太困难了,我只是初学者,不知道我在做什么。 第二个答案是应该正确的方向,但我需要输入的数字不能大于2或小于数字0,这就是现在的问题:D 很抱歉,我无法理解该语法。

相关问题