c语言中假设数组a中有10个整型数据,循环左移5次,即第一个元素移至尾部,显示每次移动后的结果。
时间: 2023-05-16 17:05:26 浏览: 72
以下是C语言代码:
```c
#include <stdio.h>
int main() {
int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int i, j, temp;
for (i = 0; i < 5; i++) {
temp = a[0];
for (j = 0; j < 9; j++) {
a[j] = a[j + 1];
}
a[9] = temp;
printf("第%d次移动后的结果:", i + 1);
for (j = 0; j < 10; j++) {
printf("%d ", a[j]);
}
printf("\n");
}
return 0;
}
```
输出结果:
```
第1次移动后的结果:2 3 4 5 6 7 8 9 10 1
第2次移动后的结果:3 4 5 6 7 8 9 10 1 2
第3次移动后的结果:4 5 6 7 8 9 10 1 2 3
第4次移动后的结果:5 6 7 8 9 10 1 2 3 4
第5次移动后的结果:6 7 8 9 10 1 2 3 4 5
```
相关问题
c语言将元素个数为10的数组a中的第一个元素移到数组末尾,其余数据依次往前平移一个位置
以下是C语言将元素个数为10的数组a中的第一个元素移到数组末尾,其余数据依次往前平移一个位置的代码实现:
```c
#include <stdio.h>
int main()
{
int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int temp = a[0];
for(int i = 0; i < 9; i++)
{
a[i] = a[i + 1];
}
a[9] = temp;
for(int i = 0; i < 10; i++)
{
printf("%d ", a[i]);
}
return 0;
}
```
运行结果为:2 3 4 5 6 7 8 9 10 1
c语言将元素个数为10的数组a中的第一个元素移到数组末尾,其余数据依次往前平移一个位置
以下是C语言将元素个数为10的数组a中的第一个元素移到数组末尾,其余数据依次往前平移一个位置的代码实现:
```c
#include <stdio.h>
int main() {
int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int temp = a[0];
for (int i = 0; i < 9; i++) {
a[i] = a[i + 1];
}
a[9] = temp;
for (int i = 0; i < 10; i++) {
printf("%d ", a[i]);
}
return 0;
}
```
运行结果为:2 3 4 5 6 7 8 9 10 1
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![cpp](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)