简体   繁体   English

如何反转一个三位数

[英]How to reverse a 3 digit number

Code:代码:

#include <stdio.h>

int main()
{
    int n, num1,num2,num3, reverse;    

    printf("Enter the number to reverse:\n");
    scanf("%d", &n);

    num1 = n / 100;
    num2 = (n % 100) / 10;
    num3 = (n % 10) / 100;

    reverse = num3*100+ num2*10;

    printf(" The reverse is %d", reverse);

    system("pause");
    return 0;
}

I wanted to reverse a number without using any loops, I included the steps so far, but Im not sure if it is right or not:我想在不使用任何循环的情况下反转一个数字,到目前为止我已经包含了这些步骤,但我不确定它是否正确:

  1. Declare the int into num1, num2 and num3 along witht the output, reverse将 int 声明为 num1、num2 和 num3 以及输出,反向
  2. Ask the user to enter a number to reverse要求用户输入一个数字来反转
  3. Store the number using scanf使用 scanf 存储数字
  4. Get the last digit of the number , num1= n/100获取数字的最后一位,num1= n/100
  5. Get the middle digit from the user, num2=(num%100)/10从用户处获取中间数字,num2=(num%100)/10
  6. To get the last digit from the user, num3=(num%10)/100要从用户那里获取最后一位数字,num3=(num%10)/100

Are these steps right?这些步骤对吗? I tried working with the program but no luck whatsoever, until I get the steps correct.我尝试使用该程序但没有任何运气,直到我得到正确的步骤。

Example:示例:

Enter a number: 263
Output: 362

The number is always 3 digits, no change whatsoever号码始终是 3 位数字,没有任何变化

First of all , your num3 is wrong , that is modified below and secondly , reverse is calculated wrong首先,你的 num3 是错误的,即在下面修改,其次,reverse 计算错误

#include <stdio.h>

int main()

{


int n, num1,num2,num3, reverse ;


    printf("Enter the number to reverse:\n");
    scanf("%d", &n);

    num1 = n / 100;
    num2 = (n % 100) / 10;
    num3 = n%10 ;

    // num1 , num2 , num3 are digits only  , to make a number use the below step
    reverse = 100*num3 + 10*num2 + num1;

        printf(" The reverse is %d", reverse);



    system("pause");
    return 0;
}

Using a diverse approach使用多样化的方法

int main()
{
    char string[4];
    int reverse = 0;

    printf("Enter the number to reverse:\n");
    scanf_s("%d", &reverse);

    if (reverse > 999)
        return 0;

    sprintf_s(string, "%d", reverse);

    char c1 = string[0];
    string[0] = string[2];
    string[2] = c1;

    reverse = atoi(string);

    printf(" The reverse is %d", reverse);
    return 0;
}

or if you don't want to use atoi或者如果你不想使用 atoi

int main()
{
    char string[4];
    int reverse = 0;

    printf("Enter the number to reverse:\n");
    scanf_s("%d", &reverse);

    if (reverse > 999)
        return 0;

    sprintf_s(string, "%d", reverse);

    printf(" The reverse is %c%c%c", string[2], string[1], string[0]);

    return 0;
}

if the middle digit is calculated like如果中间数字是这样计算的

num2=(num%100)/10;

then the less significant digit is calculated like然后计算较低的有效数字,如

num3 = (num % 10)/1;

or just或者只是

num3 = num % 10;

This statement这份声明

reverse = num3+ num2+ num1;

does not give what you are expecting.没有给出你所期望的。 You should write instead你应该写

reverse = 100 * num3 + 10 * num2 + num1;

In fact you need not to calculate the digits.事实上,您不需要计算数字。 Just enter a three-digit number as three separate digits.:)只需输入一个三位数作为三个单独的数字。:)

For example例如

#include <stdio.h>

int main( void )
{
    unsigned int n1, n2, n3;

    printf("Enter a three-digit number: ");
    scanf("%1u%1u%1u", &n1, &n2, &n3);

    printf("Reversed number is %u%u%u\n", n3, n2, n1);

    return 0;
}

The program output might look like程序输出可能看起来像

Enter a three-digit number: 263
Reversed number is 362

If you do not want to output leading zeroes then you can include if statements in your program.如果您不想输出前导零,则可以在程序中包含 if 语句。 That is instead of this statement那不是这个声明

printf("Reversed number is %u%u%u\n", n3, n2, n1);

you can write你可以写

printf("Reversed number is ");
if (n3) printf("%u", n3);
if (n3 || n2) printf("%u", n2);
printf("%u\n", n1);

In this case if to enter for example在这种情况下,如果要输入例如

100

then the output will be那么输出将是

1

instead of而不是

001

Or you can use the expression 100 * n1 + 10 * n2 + n1 .或者您可以使用表达式100 * n1 + 10 * n2 + n1 For example例如

printf("Reversed number is %u\n", 100 * n3 + 10 * n2 + n1);

here is a better version i think 我认为这是一个更好的版本

   #include<stdio.h>
   int main()
   { 
   int num,n1,n2;
   printf("enter the number : ");
   scanf("%d",&num);
   n1=num%10;
   num=num/10;
   n2=num%10;
   printf("%d",n1*100+n2*10+num/10);
   return 0;
   }

i think this code can not get any cleaner, although you can use recursion via making a new function and reverse n number of digits. 我认为这段代码无法使代码更简洁,尽管您可以通过创建新函数并反转n个数字来使用递归。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM