0

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

无论如何,在数组中给定一组这样的数字:

4
14
24
27
34

您可以看到除了一个之外的所有数字在个位上都有一个 4。我将如何编写一个可以返回不同位置的数字的函数,在这种情况下为 27?每次运行程序时,数字都会有所不同,但由于场景的原因,其中 4 个将始终在一个位置具有相同的数字。它们不一定按数字顺序排列。

我似乎无法找到一种数学方法,也无法通过搜索找到任何东西。有任何想法吗?

4

4 回答 4

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];
}
于 2013-02-07T05:29:32.413 回答
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)];
}
于 2013-02-07T06:58:30.227 回答
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 的计数,如果只有一个数字具有不同的单位位值

于 2013-02-07T04:06:09.657 回答
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

在 liveworkspace 上查看

运行时间为 max(1,[location of the exception #]),即 O(n),其中 n 是 a 的大小。

于 2013-02-07T04:41:46.357 回答