整型数组左移或右移位简单操作

// 函数功能:将数组元素循环左移或右移一位

/*

输入参数:

输入的数组 Input

输入数组的长度

ShiftN:取值为 1 或  -1 :  1 表示右移, -1表示左移

*/

void shift(int* Input, int n, int shiftN)
{
int temp;
int i;
if( shiftN > 0 )
{
temp = *(Input+n-1);
for(i=n-1;i>0;i--)
*(Input+i) = *(Input+i-1); 
*Input = temp;
}
if (shiftN < 0)
{
temp = *Input;
for(i=0;i<n-1;i++)
*(Input+i) = *(Input+i+1); 
*(Input+i) = temp;
}
}


int _tmain(int argc, _TCHAR* argv[])

{

int a[]={1,2,3,4,5,6,7,8,9};
int n = sizeof(a)/sizeof(int);
int shiftN = -13;
shiftN = shiftN%n;

int i;
for(i=0;i<abs(shiftN);i++)
shift(a,n,shiftN/abs(shiftN));

for(i=0;i<n;i++)
printf("%4d",a[i]);

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值