
例如数组大小为10,现有9个元素,下标是 0,1,2,3,。。8。
插入位置的下标是 n, 数值是 x
#include<stdio.h>
#include<stdlib.h>
int main(){
int a[10]={1,2,3,4,5,6,7,8,9}
int x
int i,n
printf("input insert location:0 or 1,...9\n")
scanf("%d",&n)//输入插入的(下标)地点
printf("input value x:\n")
scanf("%d",&x) // 输入插入的 值
if (n<0 || n >9) {printf("location error\n")n=9}
if (n==9){
a[n]=x// 如果 插在最后位置
} else {
for (i=9i>ni--) a[i]=a[i-1]
a[n]=x // 如果 插在中间和开始位置
}
for (i=0i<10i++) printf("%d ",a[i])
}
int i[] = { 43, 123, 14, 33, 2222, 4444, 333, 11, 55 }//随意定义的数组java.util.Arrays.sort(i) //java中自带为数组排序的方法,正序,由小到大
for (int x = 0x <(i.length - 1) / 2x++) { //反转数组
int temp = i[x]
i[x] = i[i.length - x - 1]
i[i.length - x - 1] = temp
}
for (int x = 0x <i.lengthx++) { //排序完成后输出
System.out.print(i[x]+ " ")
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)