如何在C ++中提取数字的数字?

时间:2011-09-15 19:53:14

标签: c++

基本上我想制作一个小程序,当你输入一个数字(比如145)时,它会读取3位数并打印出最大的数字。

int a,b,c,max;

cout << "Enter a, b and c: ";
cin >> a >> b >> c;

max = a;
if (b>max)
    max = b;
if (c>max)
    max = c;
cout << "Max is " << max << "\n";

我想到使用这样的东西,但我不知道如何让计算机读取每个数字。 谢谢!

6 个答案:

答案 0 :(得分:5)

将第一行的int更改为char

#include <iostream>

int main() {
  char a, b, c, max;

  std::cout << "Enter a, b and c: ";
  std::cin >> a >> b >> c;

  max = a;
  if (b>max)
    max = b;
  if (c>max)
    max = c;
  std::cout << "Max is " << max << "\n";

}

这有效,但实际上不是解决这个问题的正确方法IMO for C ++。

这稍微好一些,但没有任何输入验证:

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

int main() {

  std::string s;

  std::cout << "Enter a number: ";
  std::cin >> s;

  char maxChar = *max_element(s.begin(), s.end());

  std::cout << "Max is " << maxChar << "\n";
}

答案 1 :(得分:1)

如果您已经掌握了数字,那么普通C在比keith.layne的答案更少的时间内完成C ++时,无需诉诸任何特定的C ++:

unsigned big_digit(unsigned value)
{
  unsigned biggest = 0;

  while (value) {
    unsigned digit = value % 10;
    if ( digit > biggest )
      biggest = digit;
    value /= 10;
  }

  return biggest;
}

希望这不是作业。

答案 2 :(得分:0)

您可以使用%(模数)进行此类操作。

我认为LINK会对你有正义感

答案 3 :(得分:0)

  

基本上我想制作一个小程序,当你输入一个数字(比如145)时,它会读取3位数并打印出最大的数字。

int a, b, c, max;

cout << "Enter a, b and c: ";
cin >> a >> b >> c;

max = a;
if (b>max)
    max = b;
if (c>max)
    max = c;
cout << "Max is " << max << "\n";

我想到使用这样的东西,但我不知道如何让计算机读取每个数字。谢谢!


虽然keith.layne使用字符串的答案如果你想要一个不使用字符串的答案,你可以使用整数除法和模数得到相同的结果:

  #include <iostream>
  using std::cin;
  using std::cout;
  using std::endl;

  int main()
  {
     int userInput, max;

     cout << "Input a number and I'll give the biggest digit: ";
     cin >> userInput;
     cout << "The max digit of " << userInput << " is ";

     max = userInput % 10; // sets one's digit to max
     userInput /= 10;  // reduces number by a digit
     while(userInput > 0)  // while number has remaining digits
     {
        if(userInput % 10 > max)// checks for a new max
        {
           max = userInput % 10;
        }
     userInput /= 10; // reduces number by a digit
     }
     cout << max << endl;

     return 0;
  }

答案 4 :(得分:0)

规范:必须是a4位数字,或 - 或+,修改此代码以获得所需的输出。干杯!

#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;

int main()
{
    int a, b, c, d, rem;
    int no;
    int max = 0;
    int min = 0;
    cin >> no;
    if (no < 0)
       no = abs(no);

    a = no/1000;
    rem = no%1000;

    b = rem/100;
    rem = rem%100;

    c = rem/10;
    rem = rem%10;
    //cout<<a;
    if(a > 0 && a <= 9)
    {
        if(a > max)
            max = a;
        else min = a;

        if(b > max)
            max = b;
        else min = b;

        if(c > max)
            max = c;
        else min = c;

        if(rem > max)
            max = rem;
        else min = rem;

        if(max > min)
            cout << max << endl;
        else
            cout << min << endl;
    }
    else
        cout<<"Invalid no"<<endl;
    return 0;
}

答案 5 :(得分:0)

在尝试解决代码战问题时,我想到了这一点。

int i = 0;
int num_ = num;    //we will need a dummy, num is the original
while(num_ != 0){   //count the number of digits
    num_ /= 10;
    i++; //yayyy
}

int *ptr = new int[i]; //dynamic array to store individual numbers
int pos = 0; 

while(1){ //copy digits to dynamic array
    if(num > 10){
         num_ = num%10;
        ptr[pos] = num_;
        num /= 10;
        pos++;
    }
    else{
        num_ = num%10;
        ptr[pos] = num_;
        break;
    } //array now contains our digits
}