C++ 如何在一组数字中找到一个不同的值
这是一个大数据,包含1亿个整数,但其中有一个与其他相同整数不同的值,例如:1,1,1,1,1,1,1,42,1,1,1,1,1,1。。但是,我不知道下面的代码发生了什么C++ 如何在一组数字中找到一个不同的值,c++,algorithm,C++,Algorithm,这是一个大数据,包含1亿个整数,但其中有一个与其他相同整数不同的值,例如:1,1,1,1,1,1,1,42,1,1,1,1,1,1。。但是,我不知道下面的代码发生了什么 int main() { vector <int> data; cout << "Enter same numbers " << " and a different one( negative to be end) :" << endl; int valu
int main() {
vector <int> data;
cout << "Enter same numbers " << " and a different one( negative to be end) :" << endl;
int value;
while (cin >> value && value > 0) {
data.push_back(value);
}
int unique_value;
int size = data.size();
if (data[0] != data[size - 1]) {
if (data[0] != data[2]) {
unique_value = data[0];
} else {
unique_value = data[size - 1];
}
cout << "found the unique number: " << unique_value << endl;
exit(0);
}
int low = 1;
int high = size - 2;
while (high > low) {
if (data[high] != data[low]) {
//其中必有一个是不同的,只要和data[0]就能得到结果
if (data[high] != data[0]) {
unique_value = data[high];
} else {
unique_value = data[low];
}
break;
}
}
if (high == low) {
unique_value = data[high];
}
cout << "found the unique number: " << unique_value << endl;
return 0;
}
intmain(){
矢量数据;
cout(0){
数据。推回(值);
}
int唯一值;
int size=data.size();
如果(数据[0]!=数据[size-1]){
如果(数据[0]!=数据[2]){
唯一_值=数据[0];
}否则{
唯一_值=数据[大小-1];
}
cout对前三个元素进行排序,然后取中间的一个。这是您的非唯一编号。仔细查看列表,然后查找与之不同的编号:
int data[] = {7,7,7,7,7,7,42,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7};
size_t N = sizeof(data)/sizeof(data[0]);
sort(data, data+3);
int non_unique = data[1];
for (int i = 0 ; i != N ; i++) {
if (data[i] != non_unique) {
cout << data[i] << endl;
break;
}
}
int data[]={7,7,7,7,7,42,7,7,7,7,7,7,7,7,7,7,7,7};
size_t N=sizeof(数据)/sizeof(数据[0]);
排序(数据,数据+3);
int non_unique=数据[1];
对于(int i=0;i!=N;i++){
if(数据[i]!=非唯一){
cout假设只允许输入两个可能的数字(“一个与其他整数不同的值”),则不需要全部存储。在这种情况下,您将有类似1,1,1,1,1,1,1,1,42,1,1,1,1,1,1,1
的输入
如果是这种情况,可以使用(伪代码):
firstNum=0;firstCount=0
secondNum=0;secondCount=0
尽管如此:
num=getNumber()
如果num<0:
休息一会儿
如果firstCount==0:#第一个唯一编号
firstNum=num
firstCount=1
下一段时间
如果num==firstNum:#第一个
第一名++
下一段时间
如果secondCount==0:#第二个唯一编号
secondNum=num
secondCount=1
下一段时间
if num secondNum:#第三个唯一编号(不允许)
打印“只允许两个数字”
停跑
secondNum++#是第二个唯一的数字
如果secondNum==0:#存在<2个唯一的数字
打印“没有足够的不同数字”
停跑
如果firstCount>1和secondCount>1:#没有唯一的数字
打印“无唯一编号”
停跑
如果firstCount==1:#选择计数为1的数字
打印“唯一编号为”firstNum
其他的
打印“唯一编号为”secondNum
相反,如果可能存在大量不同的数字,则需要一个解决方案,将每个数字与其他数字进行对比检查
这可以通过多种方式实现(可能还有其他方式,这些只是最先想到的几种方式):
- 慢O(n^2)检查每个数字与每个其他(后续)数字
- 排序稍微快一点(可能是O(logn),然后通过排序列表检查相邻的数字
- 如果输入范围有限,可以对每个可能的数字使用计数数组,并查找最终计数为1的数字(确保没有其他数字也这样做)
根据您的问题文本,我认为情况并非如此,但我认为我最好也涵盖这一方面。第一件事是您的代码有什么问题。您有一个而循环,由两个变量高和低控制,这两个变量在循环内不更新,因此它将永远旋转
至于算法,您不需要存储数字来查找不同的数字,而是可以读取前两个数字,如果它们不同,则读取第三个数字,然后得出答案。如果它们相同,则保留其中一个数字,然后继续读取数字,直到找到一个不同的数字:
// omitting checks for i/o errors, empty list and single number list:
// and assuming that there is one that is different
int main() {
int first;
int current;
std::cin >> first >> current;
if (first != current) {
int third;
std::cin >> third;
if (first==third) {
std::cout << current << "\n";
} else {
std::cout << first << "\n";
}
return 0;
}
while (std::cin >> current && current == first) ;
std::cout << current << "\n";
}
//忽略对i/o错误、空列表和单号列表的检查:
//假设有一个是不同的
int main(){
int优先;
电流;
标准::cin>>第一个>>当前;
如果(第一个!=当前){
第三名;
第三;
如果(第一个==第三个){
std::cout每个人都告诉过你他们会怎么做,但没有回答你的问题:你的代码有什么问题
问题是您的代码从未终止,因为您从未更改循环中的high
和low
值。您从数组的两端开始工作,并将两个值与数组中的第一个元素进行比较。现在,这使得第一个if块冗余,实际上有点奇怪,因为它检查第三个数组元素ent(无任何边界检查)
所以…把这部分拿出来:
//if (data[0] != data[size - 1]) {
// if (data[0] != data[2]) {
// unique_value = data[0];
// } else {
// unique_value = data[size - 1];
// }
// cout << "found the unique number: " << unique_value << endl;
// exit(0);
//}
这应该行得通,但很奇怪
现在,请注意,由于缓存局部性的原因,以这种方式搜索数组是个坏主意。出于优化原因,您希望将搜索限制在内存的同一部分,而对于这种算法,没有理由不检查数组中的三个相邻值
事实上,您只需要检查前三个元素,然后您就可以确定非唯一值,或者同时确定这两个元素。如果前三个元素都相同,您只需对数组的其余部分进行线性搜索,直到找到不同的值……已经指出,您甚至不必读取值你可以在飞行中执行此操作
size_t size = data.size();
if( size < 3 ) {
cerr << "Not enough data\n";
exit(0);
}
int unique_val = 0;
if( data[1] == data[0] && data[2] == data[0] ) {
int common_val = data[0];
for( int i = 3; i < size; i ++ ) {
if( data[i] == common_val ) continue;
unique_val = data[i];
break;
}
}
else if( data[1] != data[0] ) {
if( data[2] == data[1] )
unique_val = data[0];
else
unique_val = data[1];
}
else {
unique_val = data[2];
}
if( unique_val == 0 ) {
cout << "No unique value found\n";
} else {
cout << "Unique value is " << unique_val << "\n";
}
size\u t size=data.size();
如果(尺寸<3){
cerr顺便说一句,你不需要存储所有的数字——只需要前一个和当前的数字,在读入时检查它们。你知道这些整数的上界吗?这些整数排序了吗?你的代码完全错了:二进制搜索无法工作,即使你正确地实现了它(你没有做到).我很好奇什么是幕后应用程序scenario@zinking对我来说,这像是一个面试问题。
int low = 1;
int high = size - 1;
while (high >= low) {
if( data[high] != data[0] )
{
if (data[low] == data[high]) {
unique_value = data[high];
} else {
unique_value = data[0];
}
break;
}
else if( data[low] != data[0] )
{
unique_value = data[low];
break;
}
low++;
high--;
}
// Take out the part that compares high==low. It was wrong, and has been made
// redundant by looping while high >= low (instead of high > low).
size_t size = data.size();
if( size < 3 ) {
cerr << "Not enough data\n";
exit(0);
}
int unique_val = 0;
if( data[1] == data[0] && data[2] == data[0] ) {
int common_val = data[0];
for( int i = 3; i < size; i ++ ) {
if( data[i] == common_val ) continue;
unique_val = data[i];
break;
}
}
else if( data[1] != data[0] ) {
if( data[2] == data[1] )
unique_val = data[0];
else
unique_val = data[1];
}
else {
unique_val = data[2];
}
if( unique_val == 0 ) {
cout << "No unique value found\n";
} else {
cout << "Unique value is " << unique_val << "\n";
}