我的任务是让用户键入数组中有多少个元素,然后输入要放入数组中的整数。然后我必须对数组进行排序并找到最大的数字并打印出数组的元素,但如果有重复则只打印一次该数字。我还必须打印出阵列中每个元素出现的次数。例如,如果用户输入有5个元素然后输入2,1,2,-3,2那么它应该打印-3表示1计数,1表示1计数,2表示3计数。到目前为止我有它所以它将打印出元素并删除重复但我不能让它打印出每个元素的正确出现次数。到目前为止,这是我的代码。
void findRepeats(int numbers[], int num)
{
int instances = 0;
cout << "Number" << " " << "Occurrences" << endl;
for (int i = 0; i < num; i++)
{
bool matching = false;
instances = 1;
for (int j = 0; (j < i); j++)
{
if (numbers[i] == numbers[j])
{
instances++;
matching = true;
}
}
if (!matching)
cout << numbers[i] << " " << instances << endl;
}
}
现在它说所有号码只出现一次
答案 0 :(得分:1)
您可以采取的一种方法是首先对数字进行排序,然后再确定有多少重复项。这样,更容易避免多次打印相同数字的结果,并且您也不必为每个数字遍历整个数组。
void findRepeats(int numbers[], int num);
int main(){
int array[] = {2, 1, 2, -3, 2};
findRepeats(array,5);
}
void findRepeats(int numbers[], int num) {
//sort the array first
std::sort(numbers, numbers + num);
int last = numbers[0];
int count = 0;
cout << "Number of Occurrences\n";
for (int i = 0; i < num; i++) {
if (last == numbers[i]) {
++count;
} else {
cout << last << " " << count << '\n';
count = 1;
}
last = numbers[i];
}
if (count > 0) {
cout << last << " " << count << '\n';
}
}
<强>打印:强>
Number of Occurrences
-3 1
1 1
2 3
答案 1 :(得分:1)
我会使用map
或unordered_map
,以及......,将整数映射到它的出现次数。它使事情变得非常简单,因为它基本上会照顾你的副本。
#include <iostream>
#include <unordered_map>
using namespace std;
void reportCounts(const int numbers[], const size_t size){
unordered_map<int, unsigned int> counts;
//unfortunately range-for here would a little PIA to apply
//or at least I don't know convenient way
for(size_t i = 0; i < size; ++i) {
counts [ numbers[i] ]++; //increase `count` of i-th number
}
//print results
for(auto count : counts ){
cout << count.first << ' ' << count.second << endl;
}
}
int main(){
int array[] = {2, 1, 2, -3, 2};
reportCounts(array,5);
}
因为这是一项任务,我将把c ++ shenaningans告诉你和http://cppreference.com。关键字包括map
,map::iterator
以及关联容器,其中map
为。{/ p>
我确实理解它可能比某些算法的简单实现更难理解,但这可能接近现代c ++中的最佳解决方案,并且努力理解它的工作方式和原因应证明是有益的。人们应该注意到必须编写的代码少得多,并且不必发明任何算法。减少实施时间,减少出错的地方,减少测试。
答案 2 :(得分:0)
搜索您的阵列。对于每个整数,要么记录它,要么增加它的计数。重复过程直到完成,然后打印出来。
如何?你说?一种方法是使用并行数组来存储找到的唯一整数,另一种方法是存储整数计数。然后打印唯一的整数及其计数。
简单搜索算法的代码示例:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void print(vector<int> valueArray,vector<int> countArray){
for(unsigned int i = 0; i < valueArray.size(); ++i){
cout<<"Found value "<<valueArray[i]<<": "<<countArray[i]<<" times."<<endl;
}
}
void findRepeats(vector<int> testArray,vector<int> &valueArray,vector<int> &countArray){
for(unsigned int i = 0; i < testArray.size(); ++i){
if(valueArray.size() == 0){
valueArray.push_back(testArray[i]);
countArray.push_back(1);
}else{
bool newEntry = true;
for(unsigned int j = 0; j < valueArray.size(); ++j){
if(testArray[i] == valueArray [j]){
countArray[j]++;
newEntry = false;
break;//After find, break out of j-for-loop to save time.
}
}
if(newEntry){
valueArray.push_back(testArray[i]);
countArray.push_back(1);
}
}
}
}
int main(){
vector<int> testArray; //To store all integers entered.
vector<int> valueArray; //To store non-copied integers, dynamically, else handle it yourself.
vector<int> countArray; //To count increments of numbers found, dynamically, else handle it yourself.
testArray = {0,2,5,4,1,3,6,2,5,9,8,7,4,1,2,6,5,4,8,3,2,1,5,8,6,9,8,7,4,4,5,6,8,2,1,3,0,0,1,2,0,2,5,8};//Dummy data.
findRepeats(testArray,valueArray,countArray);//Function to find statistics on testArray.
cout<<"\nPrinting found characters, and number of times found: "<<endl;
print(valueArray,countArray);
return 0;
}
输出类似于:
Printing found characters, and number of times found:
Found value 0: 4 times.
Found value 2: 7 times.
Found value 5: 6 times.
Found value 4: 5 times.
Found value 1: 5 times.
Found value 3: 3 times.
Found value 6: 4 times.
Found value 9: 2 times.
Found value 8: 6 times.
Found value 7: 2 times.
在上面,我使用向量来简化,但是如果必须使用c样式数组,一种方法是创建所有三个相同大小的向量,并保留一个整数计数器用于在valueArray和countArray;他们应该分享,因为他们的关系是1比1。你还需要将它传递给findRepeats函数。
拥有相同大小的数组将确保您的值和计数适合您的数组;如果输入的每个数字都是唯一的,就会发生这种情况。