C语言编写一个程序,将数字序列向右移动一位。 例如: 输入序列的长度:6 输入原始序列:1 2 3 4 5 6 循环向右移动一位后,它变为: 6 1 2 3 4 5 序列输入、序列输出和序列右移分别由三个子功能实现。(序列号由用户通过键盘输入)
时间: 2024-03-25 21:36:21 浏览: 25
下面是实现该功能的C语言程序:
```c
#include <stdio.h>
void input_sequence(int sequence[], int length);
void output_sequence(int sequence[], int length);
void right_shift_sequence(int sequence[], int length);
int main()
{
int length; // 序列长度
int sequence[100]; // 序列数组,假设不超过100个数字
printf("请输入序列的长度:");
scanf("%d", &length);
printf("请输入原始序列:");
input_sequence(sequence, length);
right_shift_sequence(sequence, length);
printf("循环向右移动一位后,它变为:");
output_sequence(sequence, length);
return 0;
}
// 输入序列
void input_sequence(int sequence[], int length)
{
int i;
for (i = 0; i < length; i++)
scanf("%d", &sequence[i]);
}
// 输出序列
void output_sequence(int sequence[], int length)
{
int i;
for (i = 0; i < length; i++)
printf("%d ", sequence[i]);
printf("\n");
}
// 序列右移一位
void right_shift_sequence(int sequence[], int length)
{
int temp = sequence[length - 1]; // 保存最后一个数字
int i;
for (i = length - 1; i > 0; i--)
sequence[i] = sequence[i - 1];
sequence[0] = temp; // 最后一个数字移到了第一个位置
}
```
该程序通过三个函数实现了输入序列、输出序列和循环向右移动一位的功能。其中,`input_sequence` 函数用于输入序列,`output_sequence` 函数用于输出序列,`right_shift_sequence` 函数用于将序列向右移动一位。程序中假设序列不超过100个数字。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)