-2

如何在 C 编程中从左到右反转整数数组?

#include <math.h>
#include <stdio.h>

int main() {
    int k[4] = {1,2,3,4};
    N=4;

    for(k=1; k<=N; k=k+1);
        printf("%d",flip(k));
}

示例:k = [1,2,3,4] 翻转应该给 k=[4,3,2,1] k 是一个数字数组

4

7 回答 7

2

声明Nint(虽然k不需要)。循环打印余数 10 并N = N/ 10每次递减,尝试以下代码:

for(; N; N = N/10) 
       printf(" %d", N % 10);

循环运行直到 N != 0。

它不反转数字,而只是打印反转。

于 2013-09-02T08:22:12.163 回答
2

你为什么不只是谷歌这样简单的问题......!

#include <stdio.h>

int main()
{
   int n, reverse = 0;

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

   while (n != 0)
   {
      reverse = reverse * 10;
      reverse = reverse + n % 10;
      n = n / 10;
   }

   printf("Reverse of entered number is = %d\n", reverse);     
   return 0;
}

输出:

root@jeegar:~# gcc test.c
root@jeegar:~# ./a.out 
Enter a number to reverse
1234
Reverse of entered number is = 4321
于 2013-09-02T08:25:56.943 回答
2

你问的代码:

#include <stdio.h>

int main() {
    int k = 0;
    int n = 1234;

    while (n != 0)
    {
        k *= 10;
        k += n % 10;
        n /= 10;
    }

    printf("%d", k);

    return 0;
}

从这里很容易将其更改为函数。请注意,对于“接近” int 最大值的数字,它将无法正常工作。

ideone的代码:http: //ideone.com/IIK0cc

于 2013-09-02T08:26:29.340 回答
2

编辑. 刚刚看到您的修订版说问题是反转一个int数组,而不仅仅是一个数字。嗯,呵呵。这要容易得多。由于这是 C,你应该使用指针。其他任何东西都不是 C. :-)

#include <stdio.h>

void reverse (int ary[], int size);

int main (void) {
    int ary[] = { 1, 2, 3, 4 };
    int size = sizeof (ary) / sizeof (ary[0]);

    reverse (ary, size);

    for (int i = 0; i < size; i++)
        printf ("%d ", ary[i]);

    printf ("\n");
    return 0;
}


void reverse (int ary[], int size) {
    int* startPtr;
    int* endPtr;
    int temp;

    startPtr = ary;
    endPtr = ary + size - 1;

    while (endPtr > startPtr) {
        temp = *startPtr;
        *startPtr = *endPtr;
        *endPtr = temp;
        startPtr++;
        endPtr--;
    }
    return;
}

输出:

4 3 2 1
于 2013-09-02T08:58:25.657 回答
1

首先,你的问题含糊不清,自相矛盾。例如,是k一个整数,正如您在问题中包含的代码所暗示的那样,还是k一个整数数组,正如您谈论“向量”所暗示的那样?此外,您想反转整数还是反转数组?这是两个非常不同的问题。为了以后参考,如果你想得到一个好的答案,你应该问一个好问题;并且展示你的一些作品也不会受到伤害。

无论如何,撇开诽谤不谈,这里是反转数组的代码。没有错误检查,所以如果你用错误的方式调用它......好吧,你自己:

void flip(int *array,  // the array to reverse
          int count)   // the number of elements in the array
{
    int i = 0, tmp;

    while(i != count / 2)
    {
        tmp = array[i];    
        array[i] = array[count - i - 1];
        array[count - i - 1] = tmp;

        i++;
    }
}

有关如何使用此功能的示例,请查看我安装的这个示例程序

于 2013-09-02T09:00:51.153 回答
0

试试这个:

int flip(int k) { 
  int new = 0;

  while (k > 0) { 
    // Take the last digit and add it to the front of new   
    new = (new * 10) + k % 10;

    // Divide by ten to drop the last digit of k
    k = k / 10;
  }

  return new;
}
于 2013-09-02T08:30:59.187 回答
0

看起来您正在尝试反转数组,而不是数字

在这种情况下,假设source是您当前的数组,SIZEsource分配给它的大小的常量,并且destination是您的目标数组,您可以使用:

for (is = SIZE - 1, id = 0; is >= 0; is--, id++) {
    destination[id] = source[is];
}

工作演示

如果要替换sources 内容...

如果要替换source而不是反向复制,请执行此操作,然后分配destinationsource

for (counter = 0; counter < SIZE; counter++) {
    destination[counter] = source[counter];
}
于 2013-09-02T08:56:16.850 回答