C和c++先学哪个:C++不属于哪个数字(find what doesn't belong)

我是编程的初学者,但是在编写自己的程序时,我遇到了一个似乎无法解决的障碍。

无论如何,给定一组数字像这些在一个数组:

4
14
24
27
34

你可以看到除了一个数字之外的所有数字在一个地方都有一个 4。我将如何编写一个函数来返回一个地方不同的数字,在这种情况下为 27?每次程序运行时,数字都会不同,但是由于这种情况,其中 4 个总是在一个地方有相同的数字。他们不一定是数字顺序。

我似乎无法找到一种数学上的方法,也无法通过搜索找到任何东西。

3

Jerry Coffin 的解决方案是不必要的O(log N);它可以通过使用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());
}

这个问题可以通过最多(N+1)/2比较来解决:

#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)];
}
3

这是做这项工作的一种方法。绝对不是最有效的,但无论如何都很好。只要只有一个与其他输入不同(显然是单位数字),这将适用于任何数量的输入。

#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());
}

编辑:我决定添加另一个虽然这仍然比 @ Rici 的(非常好)解决方案进行更多的比较,但它至少是线性的(并且不会重新排列原始数据):

#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];
}
2

使用%运算符编写一个程序来获取单位位置值

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];
}

这将适用于 5 的计数,如果只有一个数字具有不同的单位位置值

2

给你...: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;
}

请注意,实际工作:

   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;

只有 4 行!:p

check it out on liveworkspace

运行时间是 max(1,[异常的位置 #]),它是 O(n),其中 n 是 a 的大小。

本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处

(325)
G76内螺纹编程实例:Chrome自动播放政策-Chrome76
上一篇
Cube社作品:冒险作品解释(adventureworks data model)
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(64条)