如何找到给定数字的最大数字

时间:2017-11-08 14:42:53

标签: c

我的问题是我将给定的八进制数转换为二进制,二进制转换为十六进制。并显示十六进制,我使用%lx。但我想找到这个十六进制中最大的一个。有没有办法可以存储%lx值,然后运行最大的函数?

long long convertOctalToBinary(int octalNumber);
int convertBinarytoHex(long long binary);
int largest(long int hex);

int main()
{
int octalNumber;
long long number;
long int result;
int temp;
int large;


printf("Enter an octal number: ");
scanf("%d", &octalNumber);
number = convertOctalToBinary(octalNumber);
printf("number : %d\n", number);
result = convertBinarytoHex(number);
printf("result : %lX\n", result);
large = largest(temp);
printf("large : %lX\n", large);
}



int convertBinarytoHex(long long number)
{

long int hexvalue = 0;
int i = 1;
int r; //declare variables

while (number != 0) //if binary number is not equal to 0 then following instructions will execute
{
    r = number % 10; //by performing modulo we get remainder
    hexvalue = hexvalue + r * i; //for first digit i =1 as binavalue*2^0,binvalue*2^1
    i = i * 2; //i value multiplied with 2 for next iteration,for next iterations it becmoes 2^1,2^2,2^3
    number = number / 10; //now get reamaing binary value
}
return hexvalue;
}

int largest(long int hex)
{
 int p,i=0,digit,large=0;//variables

 p=hex;//storing original value entered by user to variable p

 while(hex>0)
 {
    digit=hex%10;//finding digit

    if(digit>large)//checking condition for large
    large=digit;

 hex = hex / 10;//dividing number by 10
 }

 return large;
 }

2 个答案:

答案 0 :(得分:1)

你可能想要这个:

#include <stdio.h>

char largestdigit(const char *buffer)
{
  char largest = 0;       // largest digit (initially 0)

  char c;
  while (c = *buffer++)   // loop until we reach end of string
  {
    if (c > largest)
      largest = c;
  }

  return largest;
}

int main()
{
  int number;
  printf("Enter an octal number: ");
  scanf("%o", &number);                  // read number entered in octal

  char hexbuffer[50];
  sprintf(hexbuffer, "%x", number);      // output number in hexadecimal to hexbuffer

  char large = largestdigit(hexbuffer);  // find largest digit in hexbuffer
  printf("largest digit of hexadecimal representation (%x) of octal number %o : %c\n", number, number, largestdigit(hexbuffer));
}

示例运行:

Enter an octal number: 543
largest digit of hexadecimal representation (163) of octal number 543 : 6

答案 1 :(得分:1)

我会说你的方法完全错误。

首先,在数字类型中存储表示没有意义,除非您确实需要高性能。它可能会导致错误。 int存储号码,而不是它的代表。但是,您可以告诉scanf将输入视为八进制数。只需使用scanf("%o", &var)

如果问题是找到最大的数字,那么将它转换为数字数组(即字符串表示)要好得多。下面是一些将int转换为任意基础中的字符串的代码。

#include <stdio.h>

/* Will work for base 2-36. Well, it works for higher bases too, but 
   it can be iffy to interpret the result. */
void intToStr(int number, int base,  char * dest)
{
    int index = 0;
    while (number != 0) {
        int rem=number%base;
        char digit = rem>9 ? rem+'a'-10 : rem+'0';
        dest[index++]=digit;
        number/=base;
    }
    dest[index]='\0';
    for(int i=0; i<index-1; i++) {
        char tmp = dest[i];
        dest[i]=dest[index-i-1];
        dest[index-i-1]=tmp;
    }
}

char highestDigit(const char *str)
{
    char c=str[0];
    int index=0;
    while(str[index]) {
        c=c<str[index] ? str[index] : c;
        index++;
    }
    return c;
}

int main()
{
    char str[100];
    int n;
    printf("Enter a octal number: ");
    scanf("%o", &n);

    printf("\nBase Value Highest digit\n");        
    for(int i=2; i<17; i++) {
        intToStr(n, i, str);
        printf("%d: %s %c\n", i, str, highestDigit(str));
    }
}

一些输出:

$ ./a.out 
Enter an octal number: 100

Base Value Highest digit
2: 1000000 1
3: 2011 2
4: 1000 1
5: 224 4
6: 144 4
7: 121 2
8: 100 1
9: 71 7
10: 64 6
11: 59 9
12: 54 5
13: 4c c
14: 48 8
15: 44 4
16: 40 4