C++ C++;哪个数字不';你不属于我吗?
我是一个编程初学者,但在编写自己的程序时,我遇到了一个似乎无法绕过的障碍 无论如何,在数组中给定一组类似的数字:C++ C++;哪个数字不';你不属于我吗?,c++,C++,我是一个编程初学者,但在编写自己的程序时,我遇到了一个似乎无法绕过的障碍 无论如何,在数组中给定一组类似的数字: 4 14 24 27 34 你可以看到,除了一个数字外,所有的数字都在一的位置上有一个4。我如何编写一个函数来返回一个不同的数字,在这里是27?每次程序运行时,数字都会不同,但由于这种情况,其中4个数字的“1”处总是有相同的数字。它们不一定按数字顺序排列 我似乎找不到一种数学方法,也无法通过搜索找到任何东西。有什么想法吗?使用%运算符编写一个程序,以获取单位位置值 void che
4
14
24
27
34
你可以看到,除了一个数字外,所有的数字都在一的位置上有一个4。我如何编写一个函数来返回一个不同的数字,在这里是27?每次程序运行时,数字都会不同,但由于这种情况,其中4个数字的“1”处总是有相同的数字。它们不一定按数字顺序排列
我似乎找不到一种数学方法,也无法通过搜索找到任何东西。有什么想法吗?使用
%
运算符编写一个程序,以获取单位位置值
void check ()
{
int i, changeIndex =0;
for ( i = 0; i < 5; i++)
{
for (int k = 0; k < 5; k++)
{
if (a[i]%10 == a[k]%10)
{
changeIndex++;
}
}
if (changeIndex != 4)
{
break;
}
changeIndex = 0;
}
cout<<a[i];
}
无效检查()
{
int i,changeIndex=0;
对于(i=0;i<5;i++)
{
对于(int k=0;k<5;k++)
{
如果(a[i]%10==a[k]%10)
{
changeIndex++;
}
}
if(changeIndex!=4)
{
打破
}
changeIndex=0;
}
给你…:p
适用于任意数量的输入…甚至可以检测它们是否都相同
#include <iostream>
int main() {
int a[] = {4,14,24,34,27,94};
// assume a has more than 2 elements, otherwise, it makes no sense
unsigned ri = 0;
if (a[1]%10 == a[0]%10) {
for (ri = 2; (ri < sizeof(a)/sizeof(a[0])) && (a[ri]%10 == a[0]%10); ri++);
} else if (a[2]%10 == a[0]%10)
ri = 1;
if (ri < sizeof(a)/sizeof(a[0]))
std::cout << "weird number is a["<< ri <<"] = "<<a[ri] << std::endl;
else
std::cout<<"they're all the same" << std::endl;
return 0;
}
#包括
int main(){
int a[]={4,14,24,34,27,94};
//假设a有2个以上的元素,否则就没有意义了
无符号ri=0;
如果(a[1]%10==a[0]%10){
对于(ri=2;(ri std::cout这里有一种方法可以完成这项工作。肯定不是最有效的方法,但无论如何都很好。这一方法可以用于任何数量的输入,只要只有一个输入与其他输入不同(显然是单位数字)
#包括
#包括
#包括
int main(){
向量n={4,14,27,24,34};
标准::排序(标准::开始(n),标准::结束(n),
[](inta,intb){返回a%10std::coutJerry Coffin的解决方案是不必要的O(logn)
;它可以通过使用std::partition
而不是std::sort
来改进:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> n = {4, 14, 27, 24, 34};
int first = n[0]%10;
std::partition(std::next(std::begin(n)), std::end(n),
[&](int a) { return first == a%10;});
std::cout << ((first != n[1]%10) ? n.front() : n.back());
}
但我也看到所有的数字都是2位,除了1位。这算数吗?奇数当然是1位,然后有一位数字…数学上呢?模数呢?24%10=4
,和27%10=7
。你需要找出哪个数字不同。使用模运算符%
查找最低有效位。除27之外,所有数字都是偶数。这只会给出最低有效位。他想:我该如何编写一个函数来返回与1不同的数字对不起,我没有看到任何其他简单的解决方案:-(给你,老兄::p试图减少#行数…10行!:pI将确定检查。很抱歉,我现在无法访问该链接。它从这里被阻止。我稍后将在@home:-)检查它)我将粘贴它…现在它完全神秘了。@Toms,我认为可能会将所有内容放入1 for循环中…:pah离开几个小时,你们想出了各种聪明的技巧:p注意,事实上,可以证明,你们做的比(N+1)/2比较更好。
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> n = {4, 14, 27, 24, 34};
std::sort(std::begin(n), std::end(n),
[](int a, int b) { return a%10 < b%10;});
std::cout << ((n[0]%10 < n[1]%10) ? n.front() : n.back());
}
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> n = {4, 14, 27, 24, 34};
auto pos = std::adjacent_find(std::begin(n), std::end(n),
[](int a, int b) { return a%10 != b%10; });
if (pos != std::begin(n))
std::cout << pos[1];
else
std::cout << n[n[1]%10 != n[2]%10];
}
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> n = {4, 14, 27, 24, 34};
int first = n[0]%10;
std::partition(std::next(std::begin(n)), std::end(n),
[&](int a) { return first == a%10;});
std::cout << ((first != n[1]%10) ? n.front() : n.back());
}
#include <iostream>
#include <vector>
int odd_man_out(const std::vector<int> n) {
size_t i;
for (i = 0; i + 2 < n.size(); i += 2) {
if (n[i]%10 != n[i+1]%10)
return n[i]%10 != n[i+2]%10 ? i : i + 1;
}
if (i + 2 == n.size() && n[i]%10 == n[i-1]%10)
return i + 1;
else
return i;
}
int main() {
std::vector<int> n = {4, 14, 27, 24, 34};
std::cout << n[odd_man_out(n)];
}