【问题标题】:sort and show the number of digits排序并显示位数
【发布时间】:2018-11-27 20:48:40
【问题描述】:

我希望我的程序可以对输入的整数进行排序并计算输入的任何整数的个数,但我不知道应该在哪里写 c 的 cout 例子 a[9]={2,3,2,6,6,3,5,2,2} 2的数量是4 3的数量是2 6的数量是2 . . 请修复此代码

int main()
{
cout << "please enter the number of digites :" << endl;
int n;
cin>>n;
int a[n];
cout<<"enter numbers :"<<endl;
for(int i=0;i<n;i++)
    cin>>a[i];


     int i,j;

for(i=0;i<n-1;i++)
{
    for(j=0;j<n-i-1;j++)
        if(a[j]>a[j+1])
        {
            int temp;
            temp=a[j+1];
            a[j+1]=a[j];
            a[j]=temp;
        }
}

int c;
for(int m=0;m<n;m++)
{
    if(a[m]==a[m+1])
        c++;
    else
        c=0;
}



return 0;
}

【问题讨论】:

  • C++ 中不允许使用 VLA,请改用 std::vector
  • 您必须对输入进行排序吗? this technique 无需对源容器进行排序即可工作,只需 2 行代码。
  • @user463035818 和 std::map 使用 std::vectorstd::sort 完全没有必要
  • @Slava 当然 ;)
  • 如果你想排序,std::sort 有什么问题?

标签: c++ sorting logic cout


【解决方案1】:

通读我的解决方案,我已经评论了我更改的部分。我整理了一下。

回答您的问题:您应该在将 count 变量重置为 1 之前打印输出(数组中整数的频率)。这将起作用,因为我们已经对数组进行了排序,并且不会必须向前看是否会出现更多当前数字。

[编辑] 我还在你的代码上方添加了这个:

#include <iostream>
#include <vector>
using namspace std;

完整解决方案

#include <iostream>
#include <vector>

using namespace std;

int main() {
    // Get input
    int n;
    cout << "Please enter the number of digits: ";
    cin>>n;

    vector<int> a;
    cout << "Enter " << n << " numbers: " << endl;
    for(int i=0;i<n;i++) {
        int temp;
        cin >> temp;
        a.push_back(temp);
    }

    // Sort input
    int i,j;
    for (i = 0; i < a.size(); i++) {
        for(j = 0; j < a.size()-i-1; j++) {
            if(a[j] > a[j+1]) {
                int temp;
                temp=a[j+1];
                a[j+1]=a[j];
                a[j]=temp;
            }
        }
    }

    // If an element is in an array
    // we can not have 0 occurrences
    // of that element, hence count
    // must start at 1
    int count = 1;

    // Int to count
    int current = a[0];

    // Ouput if we have reset the count,
    // or if it is the last iteration
    bool output;

    // Loop through array
    for (int i = 1; i < a.size(); i++) {
        output = false; // Reset output if we have printed
        if (a[i] == current) {
            // If current int and the element next to it are the same,
            // increase the count
            count++;
        } else {
            // If current and next are different,
            // we need to show the frequency,
            // and then reset count to 1
            cout << current << " occurs " << count << " times" << endl;
            count = 1;
            current = a[i];
        }
    }

    // Output one last time, for last int in sorted set
    cout << current << " occurs " << count << " times" << endl;

    return 0;
}

如果这没有帮助,请阅读此页面,它是 C 中的解决方案,但可以轻松适应 C++。 https://codeforwin.org/2015/07/c-program-to-find-frequency-of-each-element-in-array.html 这将帮助您理解和编写任务。他们将带您逐步了解算法。

【讨论】:

  • 现在非常好的答案,我只需将int a[n]; 修复为std::vector,因为它取决于特定的编译器扩展,并且每个标准 c++ 都不允许(或者只是提及,如果你不想更改超过必要的 OP 代码)
  • ...你有一个越界错误;)a[i+1]i==n-1
  • @user463035818 修复了越界错误和解决错误时的输出
【解决方案2】:

这是std::map 的典型用例。 std::map&lt;char,int&gt; 可让您轻松计算字符的频率(将用户输入视为字符而不是将其转换为数字更容易)。

这基本上就是你所需要的:

#include <iostream>
#include <iterator>
#include <map>

int main(){
  std::istream_iterator<char> it( std::cin );
  std::istream_iterator<char> end_of_input;
  std::map<char,int> data;
  while (it != end_of_input ) data[*(it++)]++;
  for (const auto& e : data) std::cout << e.first << " " << e.second << "\n";
}

这可能一次很多,所以让我们一个接一个。

std::istream_iterator&lt;char&gt; 允许您从流中提取字符,就像您在迭代容器一样。所以while 迭代std::cin 直到它到达输入的末尾。然后*(it++) 递增迭代器并返回从流中提取的字符。 data[x]++ 访问映射中键 x 的值并递增其值。如果映射中还没有键的值,则默认初始化为0

对于输入:11223 它打印

1 2
2 2
3 1

你的代码有一些问题,不知道我能不能把它们都找出来......

您在此处使用 VLA(可变长度数组):int a[n];。这是一个编译器扩展,而不是标准的 c++。

您越界访问数组。当i == 0 然后j 上升到j&lt;n-i-1 == n-1 然后你访问a[j+1] == a[n],但数组的最后一个有效索引是n-1。另一个循环中的同样问题 (a[m+1])。

假设您的排序工作正常,最后一个循环几乎可以为您提供元素的数量,但不完全是,要修复它,您可以将其更改为...

int current = a[0];
int counter = 1;
for(int m=1;m<n;m++) {
   if(a[m] == current) {
       counter++;
   } else {
       std::cout << current << " appears " << counter << " times" << endl;
       counter=1;   // note: minimum freq is 1 not 0
       current = a[m];
   }
}   

【讨论】:

    猜你喜欢
    • 2020-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-21
    • 1970-01-01
    • 2016-09-25
    • 1970-01-01
    相关资源
    最近更新 更多