无输出 - 程序不提供任何输出

如何解决无输出 - 程序不提供任何输出

我正在学习数据结构。我尝试编写一个函数 rotate(arr[],d,n) 将大小为 n 的 arr[] 旋转 d 个元素。

旋转是指移动数组中的元素。

该程序没有给出任何错误,而是挂起一点但没有运行。

代码如下:-

#include <stdio.h>

int rotate(int arr[],int d,int n,int dir)
{
    int temp,i;

    while (d)
    {
        if (dir)
        {

            // for left shift
            // First element will always get replaced in a rotation.
            temp = arr[0];
            for (i = 0; i < n - 1; i++)

                // for left shifting the second element next to its original position.
                arr[i] = arr[i + 1];

            // Putting the temp value in the last position.
            arr[n - 1] = temp;
        }

        else
        {
            // for right shift
            // Last element will always get replaced in a rotation.
            temp = arr[n - 1];
            for (i = n - 1; i > 0; i--)

                // for right shifting the second last element to the last position.
                arr[i] = arr[i - 1];
            // Putting the temp value in the first position
            arr[0] = temp;
        }
        d--;
    }

    // Print the shifted array
    for (i = 0; i < n; i++)
    {
        printf("%d,",arr[i]);
    }
}

程序仅在我不接受用户输入时运行。

int main()
{
    int n;
    int arr[n];
    int dir;
    int d;

    printf("Enter the size of the array: \n");
    scanf("%d",&n);

    printf("Enter the elements of the array: \n");
    for (int i = 1; i <= n; i++)
    {
        printf("Enter element %d",i);
        scanf("%d",&arr[i]);
    }

    printf("Enter the position: \n");
    scanf("%d",&d);

    printf("Enter the direction: \n");
    // 0: Right Direction and 1: Left Direction
    scanf("%d",&dir);


    // Before shifting the array
    for (int i = 1; i <= n; i++)
    {
        printf("%d,arr[i]);
    }

    // After shifting the array
    rotate(arr,n,dir);


    return 0;
}

解决方法

您可能希望在 int arr[n] 之后执行 scanf("%d",&n);,因为在执行 n 时未初始化 int arr[n]。此外,C 中的数组索引从 0 开始,因此 for (int i = 1; i <= n; i++) 将为 for (int i = 0; i < n; i++)

,

这不是一个正确的答案,所以不要把它当作正确的答案。这只是出于教育目的的一种可能实现。

这是一种旋转数组的方法,以便每个元素只移动一次(除了“组”的第一个元素通过临时变量移动)。

旋转量指定为整数,正值向右旋转,负值向左旋转。它将这个数量转换为 0n-1 范围内的数字,这是将被复制到元素 0 的元素的索引。然后它将数组划分为一个或多个相同的交错组大小,使得每组中的连续元素以圆形方式按旋转量分隔,并旋转每组中的元素。 (组数是n和旋转量的最大公约数,每组的元素数是元素总数除以组数。)

#include <limits.h>
#include <stddef.h>

static size_t rotate_modulus(int d,size_t n);
static size_t gcd_size(size_t a,size_t b);

/* Rotate arr[] of length n right by d,or left by -d. */
void rotate(int arr[],int d,size_t n)
{
    size_t md = rotate_modulus(d,n);   /* Get offset in range 0 to n-1. */
    if (md)
    {
        /* Rotation needed. */
        /* Divide into interleaved groups and rotate each group. */
        size_t num_groups = gcd_size(n,md);
        size_t group_size = n / num_groups;
        size_t group;
        for (group = 0; group < num_groups; group++)
        {
            size_t a = group;   /* Index of first element in group. */
            size_t i;
            /* Rotate elements in group. */
            int temp = arr[a];  /* Get first element. */
            for (i = 0; i < group_size - 1; i++)
            {
                /* Get index of next element in group. */
                size_t b = (a + md);
                if (a >= n - md)
                {
                    b -= n;         /* Index wraps around. */
                }
                arr[a] = arr[b];    /* Move an element. */
                a = b;              /* Advance to next index. */
            }
            arr[a] = temp;          /* Move first element to last element. */
        }
    }
}

/*
 * Get modulus for rotation of n elements.
 *
 * d is the amount to rotate right; negative d rotates left by -d.
 *
 * For zero n,the return value is 0.
 *
 * For non-zero n,the return value is n - s,where s is d plus an
 * integer multiple of n such that s is in the range 1 to n,and the
 * return value is in the range 0 to n - 1.
 */
static size_t rotate_modulus(int d,size_t n)
{
    size_t md;
    if (n < 2)
    {
        /* No rotation needed if n < 2. */
        md = 0;
    }
    else if (d >= 0)
    {
        /* Non-negative d will rotate right. */
        md = d % n;
        if (md)
        {
            md = n - md;
        }
    }
    else
    {
        /* Negative d will rotate left. */
        /* -d would overflow if d == INT_MIN && INT_MIN == -INT_MAX - 1. */
        int fix_overflow = (d < -INT_MAX);
        md = -(d + fix_overflow) % n;
        if (fix_overflow)
        {
            if (++md == n)
            {
                md = 0;
            }
        }
    }
    return md;
}

/*
 * If both a and b are non-zero,return the greatest common divisor of a and b.
 * Otherwise,return 0.
 */
static size_t gcd_size(size_t a,size_t b)
{
    if (b == 0)
    {
        a = 0;
    }
    else
    {
        do
        {
            size_t t = b;
            b = a % b;
            a = t;
        }
        while (b);
    }
    return a;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res