【问题标题】:C - reverse a numberC - 反转一个数字
【发布时间】:2013-05-08 17:58:37
【问题描述】:

我在 linux 上用 C 编码,我需要反转一个数字。 (例如:12345 将变成 54321),我打算使用 itoa 将其转换为字符串,然后将其反转,因为字符串操作可能更容易,但事实证明 itoa 是非标准的并且不包括在内在 gcc 中。有没有办法对十进制数进行二进制旋转样式的事情,如果没有,我应该采取什么方法?

【问题讨论】:

  • 您可以使用sprintf() 来替代itoa()sprintf(output, "%d", input)
  • 没有“包含在 gcc 中”,因为它只是一个编译器。如果你想使用itoa(),我很确定它可以在任何平台上编译。
  • 也许不是(完全)完全相同的副本,但我对上一个问题的回答肯定适用于此。 stackoverflow.com/a/10819608/179910
  • itoa 是标准的 C 语言,它在 中声明
  • @piokuc:我有理由确定没有任何 C 标准允许 stdlib.h 声明 itoa

标签: c algorithm reverse


【解决方案1】:
int n;
scanf("%d",&n);
int rev=0,rem;
while(n>0)
{
    rem=n%10; //take out the remainder .. so it becomes 5 for 12345
    rev=rev*10+rem; //multiply the current number by 10 and add this remainder.
    n=n/10; //divide the number. So it becomes 1234.
}
printf("%d",rev);

【讨论】:

    【解决方案2】:
    #include<stdio.h>
    main()
    {
                     int rev=0,n;
                     scanf("%d",&n);
                     while(n)
                     {
                             rev=10*rev+n%10;
                             n/=10;
                     }
                     printf("result=%d",rev);
    }
    

    【讨论】:

      【解决方案3】:

      不用字符串。

      fkt()
         {
          int i = 12345;
      
          int n = 0;
          int x;
          char nr[10];
          char *p = &nr[0];
      
          while(i != 0)
          {
              x = i % 10;
              i = i/10;
              n = n * 10 + x;
              *p = x+'0';
              p++;
          }
      
          *p = 0;
      
          printf("%d   %s\n", n, nr);
          return 0;
      }
      

      【讨论】:

        【解决方案4】:

        如果你真的想使用字符串,你可以使用 sprintf 来完成 itoa 的工作。

        int k = 12345;
        char str[40];
        sprintf(str,"%d",k);
        

        然后反转字符串并使用 atoi 或 sscanf 将其转换回 int。

        【讨论】:

        • 一般来说,你使用 snprintf 而不是 sprintf 以避免缓冲区溢出。
        • @ThomasFenzl 通常是的,但在这个特定的用例中,字符串的长度受整数 N 的大小限制为 log_10(2^N) + 1
        • 虽然这是真的,但我希望没有人使用过 sprintf ;-) 或者至少首先向新编码人员解释潜在问题​​。
        【解决方案5】:

        你可以使用堆栈来做到这一点,

        struct node
        {
          char character;
          struct node *next;
        };
        
        struct node *list_head,*neos;
        
        main()
        {
          list_head=NULL;
          char str[14];
          int number,i;
          scanf("%d",&number);     
        
          sprintf(str,"%d",number);  //here i convert number to string
         for(i=0;i<strlen(str);i++)  //until the end of the string
         {
           add_to_stack(str[i]);   //i take every character and put it in the stack
         }
         print_the_number();
        

        }

        注意这里,在堆叠项目 最后添加的, 它先取出来, 这就是它起作用的原因..

        void add_to_stack(char charac)
        {
          neos=(struct node*)malloc(sizeof(struct node));
          neos->character=charac;
          neos->next=list_head;
          list_head=neos;
        }
        
        void print_the_number()
        {
           struct node *ptr;
           ptr=list_head;
           while(ptr!=NULL)
           {
             printf("%c",ptr->character);
             ptr=ptr->next;
           }  
        }
        

        【讨论】:

          【解决方案6】:

          iota() 不是标准的 C 函数,但 snprintf() 也可以达到目的。

          /* assume decimal conversion */
          const char * my_itoa (int input, char *buffer, size_t buffersz) {
              if (snprintf(buffer, sz, "%d", input) < sz) return buffer;
              return 0;
          }
          

          由于输入不能为负,可以使用无符号类型:

          unsigned long long irev (unsigned input) {
              unsigned long long output = 0;
              while (input) {
                  output = 10 * output + input % 10;
                  input /= 10;
              }
              return output;
          }
          

          反转输入可能会导致值不再适合输入类型,因此返回结果会尝试使用更宽的类型。如果unsignedunsigned long long 具有相同的宽度,这可能仍然会失败。对于这种情况,使用字符串来表示反转值可能是最简单的。或者,如果唯一的目标是打印数字,您可以使用循环以相反的顺序打印数字。

          void print_irev (unsigned input) {
              if (input) {
                  do {
                      putchar('0' + input % 10);
                      input /= 10;
                  } while (input);
              } else {
                  putchar('0');
              }
              putchar('\n');
          }
          

          【讨论】:

          • 输入可以是负数吗?
          【解决方案7】:
          #include<iostream>
          using namespace std;
          int main()    
          { 
          
          int dig,n,rev=0;`
          cout<<"enter number";
          cin>>n;
          while(n!=0)
          {   
          dig=n%10;
          rev=rev*10+dig;
          n=n/10; }
          
          if(n==0){
          cout<<"palindrome of zeros "; 
          }
           if(rev==1)
          {
          cout<<"reverse of 10 is 01";
          }
          //since exception occurs when user inputs 10 or 0s
          else
          {
           cout<<"reverse of the number is ";
           cout<<rev;
          
          }
          
          getch();
          }
          

          【讨论】:

          • 由于逻辑原因,我们无法反转 0 或 10,所以我们需要使用 if else 来处理此类异常。
          • 这个问题是关于 C 而不是 C++。
          • 还有其他更好的解决方案可以在数字后反转尾随零吗?例如,'5000' 的反向按上述逻辑返回 '5',尽管反向输出为 '0005'
          • 我会说尾随零问题的“答案”是(a)将数字转换为字符串并反转字符串,或者(b)不用担心,因为无论如何,“反转数字”是一个相当人为的、虚构的问题。
          【解决方案8】:

          有两种方法

          方法一:

          int n;
          cin>>n;
          int rev=0,rem;
          while(n>0)
          {
              rem=n%10;
              rev=rev*10+rem; 
              n=n/10; 
          }
          cout<<rev;
          

          方法二:

          cin>>n; // size of array
              
              int a[n+1]={0};
              
              for(i=1;i<=n;i++)
              cin>>a[i];
              
              for(i=n;i>0;i--)
              cout<<a[i];
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2019-02-14
            • 1970-01-01
            • 1970-01-01
            • 2018-03-24
            • 1970-01-01
            • 2011-01-22
            • 1970-01-01
            相关资源
            最近更新 更多