【问题标题】:c rotation through for loop and indices at the endc 通过 for 循环和索引在末尾旋转
【发布时间】:2021-03-04 08:33:52
【问题描述】:

所以我是一个初学者,并试图解决一个关于在 C 中通过 d 元素旋转数组的问题。我的代码适用于一般索引,但是每当从最后一个元素到第一个元素的转换时,我都会得到一个输出而不是显示数字 5(例如:),而是显示一个大数字 1969844117。 所以我使用的代码是:

#include<stdio.h>

int main()
{
    int n,d;
    int i,j,tmp;
    int a[20];
    int b[20];

    printf("Input Size Of array:");
    scanf("%d",&n);

    printf("Input the rotation factor d:");
    scanf("%d",&d);

    for(i=0;i<n;i++){
        printf("Enter a[%d]:",i);
        scanf("%d",&a[i]);
    }

    for(i=0,j=i+d;i<n,j<i+d;i++,j++){
        tmp=a[i];
        b[j]=tmp;
    }
    for(i=0;i<n;i++){
        printf("%d\n",b[i]);
    }

    return 0;
}   

那么有什么我没有得到或者我缺少什么逻辑。循环结束时索引的变化对循环中的字符有什么影响?

【问题讨论】:

  • 在你的 for 循环中,i&lt;n,j&lt;i+d 应该是 i&lt;n &amp;&amp; j&lt;i+d
  • 请给出一个明确的例子,说明您为nd 和数组传递了什么。然后给出你得到的输出和你期望的输出。比如,给定数组[1,2,3,4]2 的旋转,我期望[3,4,1,2],但得到[a,b,c,d]
  • 条件i&lt;n,j&lt;i+d(在第二个for循环中)不像你想象的那样工作。

标签: arrays c sorting


【解决方案1】:

for loop 中,您尝试分配b[j] where j can be greater than n,这违背了目的和起始元素b[j],其中j &lt; d 仍未分配,因此您得到随机数作为输出。因此下面的for loop 是错误的。

for(i=0,j=i+d;i<n,j<i+d;i++,j++){
            tmp=a[i];
            b[j]=tmp;
        }

通过使用嵌套循环,您可以避免在单个 for 循环中使用两个不同的变量(这本身就是一个糟糕的编程示例)。 这是正确的代码:

    j=0;
    for(i=0;i<n;i++){
            j=i+d;
        tmp=a[i];
        b[j%n]=tmp;
    }

这里,当j &gt;= n再使用j%n时,j将被分配数组j = 0,1,2,3 and so on的起始索引

【讨论】:

    【解决方案2】:

    坦率地说,您的循环 for 循环没有多大意义,变量 tmp 的使用毫无意义(对此感到抱歉)。

    根据您想要的旋转方向,我建议:

    for(i = 0; i < n; i++) {
        b[i] = a[(i + d) % n];
    }
    

    for(i = 0; i < n; i++) {
        b[i] = a[(i + n - d) % n];
        // or: b[(i + d) % n] = a[i];
    }
    

    【讨论】:

      【解决方案3】:

      由于您不需要进一步维护数字的存储,一旦打印了与位移对应的元素(将在末尾输出),我们将打印第一个读取的元素并保存在数组中。

      #include <stdio.h>
      #include <stdlib.h>
      
      int main()
      {
          int rotation;
      
          int N;
      
          /* this is not necessary, we can be reading as far as we
           * continue having input.  We print the saved entries
           * after input has been exausted */
          fprintf(stderr, "Number of entries: ");
          scanf("%d", &N);
          fprintf(stderr, "Rotation shift: ");
          scanf("%d", &rotation);
      
          int *array = malloc(rotation * sizeof *array);
          
          /* all these elements go at the end */
          int i;
          for (i = 0; i < rotation; i++) {
              if (scanf("%d", &array[i]) != 1)
                  break;
          }
      
          /* print the elements past the header that we'll put at the end */
          if (i >= rotation) {
              int value;
              N -= i;
              while (N-- > 0 && scanf("%d", &value) == 1) {
                  printf("%d\n", value);
              }
          } else {
              rotation = i;
          }
      
          /* print the heading ones at end. */
          for (i = 0; i < rotation; i++) {
              printf("%d\n", array[i]);
          }
      
          free(array);
      
          return 0;
      }
      

      而且您不需要为所有输入消耗一个完整的数组。如果您想要负位移(尾部的元素在开头),您只需将 N 添加到该数字,您将获得正确的输出(-d 的旋转与 @987654324 的旋转相同@,所以在数字上加上N或减去N就足够了,直到它在0N-1之间,都包括在内)

      【讨论】:

        猜你喜欢
        • 2021-07-25
        • 1970-01-01
        • 1970-01-01
        • 2017-06-17
        • 2019-02-20
        • 1970-01-01
        • 2022-01-22
        • 2020-11-20
        • 2013-10-12
        相关资源
        最近更新 更多