如何解决无输出 - 程序不提供任何输出
我正在学习数据结构。我尝试编写一个函数 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++)
。
这不是一个正确的答案,所以不要把它当作正确的答案。这只是出于教育目的的一种可能实现。
这是一种旋转数组的方法,以便每个元素只移动一次(除了“组”的第一个元素通过临时变量移动)。
旋转量指定为整数,正值向右旋转,负值向左旋转。它将这个数量转换为 0
到 n-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 举报,一经查实,本站将立刻删除。