【问题标题】:maximum 2 digits from a number一个数字中最多 2 位数字
【发布时间】:2014-06-09 08:44:29
【问题描述】:

这是我在“尝试”中从用户输入的数字中查找最大 2 个值的代码:

#include <iostream>
using namespace std;

void maximum2(int a, int& max1,int& max2 ){

int temp = 0;
max2= a%10;
max1 = ((a/10)%10);

if (max2>max1) {
  temp = max1;
  max1 = max2;
  max2 = temp;}
for(; a>0; a/=10){
    if (a%10>max1)max1= a%10;
    else if (a%10<max1 && a%10>max2) max2 = a%10;
}

}
void main(){

int max1, max2, num;
cin>>num;
  maximum2(num,max1,max2);
  cout<<"max 1 = "<<max1<<" max 2 = "<<max2<<endl<<endl;
}

它适用于大多数数字,例如例如34256 某些情况除外。例如,当我输入 54321 时,它给出的 max1 为 5,这是正确的,但它给出的 max2 为 1,这不是所需的值。你能帮我追踪我的错误吗?

【问题讨论】:

  • main 必须返回 int
  • 还是没有解决问题。
  • 请重新缩进您的代码。此外,使用更多空间将增加代码的可读性,而不是像那样难以阅读的一堆东西。并且注意,在同一个循环中不需要多次计算a % 10,这只是浪费运行时间
  • @user657267 在嵌入式系统中,main 不需要返回任何内容。旧的 C 编译器和 Visual C 都接受void main(),无需关心
  • @LưuVĩnhPhúc 编译器足够聪明,可以使用临时值而不是一遍又一遍地计算相同的操作,如果它认为合适的话。

标签: c++ loops for-loop integer max


【解决方案1】:

不是直接的解决方案,但也许是另一种看待事物的方式:

#include <algorithm>
#include <string>
#include <iostream>

int main()
{
  std::string num;  
  std::cin >> num;  

  std::sort(num.begin(), num.end(), std::greater<>{});
    // Or std::greater<char> if your compiler doesn't support the shorter syntax

  std::cout << num[0] << ' ' << num[1] << '\n';
}

如果用户输入 0-9 以外的字符,显然没有多大用处,尽管您始终可以使用 isdigit 解析字符串以删除任何其他字符。

【讨论】:

  • 我猜这个答案很有趣。我想我应该在 OP 中提到我对编程还比较陌生,所以我不了解“算法”或“刺痛”库
【解决方案2】:

将您的 for 循环更改为:

for(; a>0; a/=10){
    int r = a % 10;
    if(r > max2) max2 = r;
    if(max2 > max1) std::swap(max1, max2);
}

原因:在您的for 循环中,当您将当前感兴趣的数字与max2 进行比较时,您不会将max2max1 进行比较以检查max1 是否需要更新。

核心 C++ 版本:

for(; a>0; a/=10){
    int r = a % 10;
    if(r > max2) max2 = r;
    if(max2 > max1) {
      int tmp = max2;
      max2    = max1;
      max1    = tmp;
    }
}

【讨论】:

    【解决方案3】:

    for 循环内,您正在更改或者 max1 的值 max2 的值。但是为了让您的代码在所有情况下都能正常工作,for 循环中必须有一个点,您可以在其中更改 both max1 的值 max2 的值。

    话虽如此,这里有一个修复建议:

    void maximum2(int a,int& max1,int& max2)
    {
        max1 = 0;
        max2 = 0;
        for(; a>0; a/=10)
        {
            int temp = a%10;
            if (temp > max1)
            {
                 max2 = max1;
                 max1 = temp;
            }
            else if (temp > max2)
            {
                 max2 = temp;
            }
        }
    }
    

    【讨论】:

      【解决方案4】:

      你的代码的问题在于函数中的这些条件

      if (a%10>max1)max1= a%10;
      else if (a%10<max1 && a%10>max2) max2 = a%10;
      

      对于像 54321 这样的数字,else 语句将永远不会执行,因为除了前两位数字之外,任何下一个数字都大于 max1。但即使是前两位数,else 语句也永远不会执行。

      考虑到参数的值可以是负数。

      我会这样写函数

      #include <utility>
      
      //...
      
      std::pair<int, int> maximum2( int x, unsigned int base = 10 )
      {
         std::pair<int, int> max( 0, 0 );
      
         unsigned int z = std::abs( x );
      
         do
         {
            unsigned int digit = z % base;
      
            if ( max.first < digit )
            {
               max.second = max.first;
               max.first = digit;
            }
            else if ( max.second < digit )
            {
               max.second = digit;
            }
         } while ( z /= base );
      
         return max;
      }         
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-03-08
        • 1970-01-01
        • 2015-08-20
        • 1970-01-01
        • 2021-11-12
        • 2017-06-01
        • 2018-11-14
        • 2021-10-08
        相关资源
        最近更新 更多