#include<cstdio> #include<cstdlib> /*需要stdlib.h提供malloc函数*/ int main() { int i,n,k; int *a; scanf("%d",&k); a=(int *)malloc(sizeof(int)*(k+1));//根据k的大小来规定数组的大小 for(i=0;i<k;i++) scanf("%d",&a[i]);//输入有序数 scanf("%d",&n);//输入要插入的数 for(i=k;a[i-1]>n;i--) { a[i]=a[i-1];//从后往前寻找并移动数组 } a[i]=n;//找到插入位置后插入 for(i=0;i<=k;i++) printf("%d ",a[i]); return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ set<int> s; int n; cin >> n; for(int i = 0; i < n; i++){ int num; cin >> num; s.insert(num); } int m; cin >> m; s.insert(m); for(set<int>::iterator it = s.begin(); it != s.end(); it++) cout << *it << " "; return 0; }
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main(){ int n; cin >> n; vector<int> ans; for(int i = 0; i < n; i++){ int temp; cin >> temp; ans.push_back(temp); } int m; cin >> m; ans.push_back(m); sort(ans.begin(), ans.end()); for(int i = 0; i < ans.size(); i++) printf("%d ", ans[i]); return 0; }
#include <stdio.h> int main(void) { int num, n, index; scanf("%d", &n); int arr[n + 1]; for (int i = 0; i < n; i++) scanf("%d", &arr[i]); scanf("%d", &num); n++; for (int i = 0; i < n; i++) { if (arr[i] > num) { index = i; break; } } for (int i = n; i > index; i--) arr[i] = arr[i - 1]; arr[index] = num; for (int i = 0; i < n; i++) printf("%d ", arr[i]); return 0; }
#include<stdio.h> #include<stdlib.h> int main() { int n,i,x,j; int* p; scanf("%d",&n); p = (int*)malloc((n+1)*sizeof(int)); for(i = 0;i<n;i++) { scanf("%d",&p[i]); } scanf("%d",&x); for(i = 0;i<n;i++) { if(x<p[i]) { for(j = n;j>i;j--) { p[j] = p[j-1]; } p[i] = x; break; } } if(p[n]==0) { p[i] = x; } for(i = 0;i<n+1;i++) { printf("%d ",p[i]); } free(p); p = NULL; return 0; }
# include <stdio.h> int main(){ //输入整数 int n=0; scanf("%d",&n); //输入整数数组 int i=0; int arr[51]={0}; for(i=0;i<n;i++){ scanf("%d",&arr[i]); } //输入要插入的数字 int add=0; scanf("%d",&add); //插入数字 for(i=0;i<n;i++){ if(add < arr[0]){ //要插入的数字比首元素都要小 int k=0; for(k=0;k<n;k++){ arr[n-(k+1)+1]=arr[n-(k+1)]; } arr[0] = add; break; } if(add > arr[n-1]){ //要插入的数字比尾元素都要大 arr[n] = add; break; } if( (add>arr[i]&&add<arr[i+1]) ){ //要插入的数字介于两数之间 int k=0; for(k=0;k<(n-(i+1));k++){ arr[n-(k+1)+1]=arr[n-(k+1)]; } arr[i+1]=add; break; } } for(i=0;i<(n+1);i++){ printf("%d ",arr[i]); } return 0; }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.nextLine()); int[] arr1 = new int[n]; String[] str = sc.nextLine().split(" "); for (int i = 0; i < n; i++) { arr1[i] = Integer.parseInt(str[i]); } int m = Integer.parseInt(sc.nextLine()); int[] arr2 = new int[n + 1]; for (int i = 0; i < arr1.length; i++) { arr2[i] = arr1[i]; } arr2[n] = m; Arrays.sort(arr2); for (int i = 0; i < arr2.length; i++) { System.out.print(arr2[i] + " "); } } }
#include<stdio.h> int main() { int n; scanf("%d",&n); int a[51]={0}; for(int i=0;i<n;i++) { scanf("%d",&a[i]); } int x; scanf("%d",&x); for (int i=0;i<n;i++) { if(x<=a[i]) { for(int j=n;j>i;j--) { a[j]=a[j-1]; } a[i]=x; break; } else { a[n]=x; } } for (int w=0;w<n+1;w++) { printf("%d ",a[w]); } }
while True: try: a=int(input()) b=list(map(int,input().split(" "))) #print(b) c=int(input()) b.append(c) B=sorted(b) print(" ".join(map(str,B))) except: break
int main()
{
int a[55],b[55];//a数组用于输出答案,b数组用于参照
int n=0,i,c,d,e,f,g;//需要用到的变量
scanf("%d",&n);//输入第一行一个整数 //输入第二行的数据,并把a数组的值赋给b数组 for(i=0;i<n;i++) { scanf("%d",&a[i]); b[i]=a[i]; } //输出第三行的数 scanf("%d",&c); //找到需要改变值的下标 for(d=0;d<n;d++) { if(c<b[d]) { e=d;//需要改变值的下标 break;//找到后结束for循环 } } a[e]=c;//把插入的值进行插入 //往后移动其他数据 for(f=e+1;f<=n;f++) { a[f]=b[f-1]; } //输出结果 for(g=0;g<=n;g++) { printf("%d ",a[g]); } return 0;
}
#include<stdio.h> int main() { int N; scanf("%d",&N); int arr[100]={0}; for(int i=0;i<N;i++){ scanf("%d",&arr[i]); } int m; scanf("%d",&m); //输入完毕,现在进行一次插入排序 int end=N-1; while(end>=0){ if(arr[end]>m){ arr[end+1]=arr[end]; end--; } else{break;} } arr[end+1] = m; for(int i=0;i<=N;i++){ printf("%d ",arr[i]); } return 0; }
#include <stdio.h> #define N 50 int main(void) { int i, j, k, t, n, a[N + 1]; scanf("%d", &n); getchar(); for (i = 0; i < n; i++) { scanf("%d", &a[i]); } getchar(); scanf("%d", &j); for (i = 0; i < n; i++) { if (j <= a[i]) { for (k = n - 1 ; k >= i; k--) { a[k + 1] = a[k];//数组向后移动; } a[i] = j;//放置添加的数; break; } } if (i == n)//如果要添加的数比原数组所有元素都大; { a[i] = j;//就放置到数组末尾; } for (i = 0; i < n + 1; i++) { printf("%d ", a[i]); } return 0; }
#include<stdio.h> int main() { int b = 0; int c[100] = { 0 }; scanf("%d", &b);//输入一个数,表示一共有b个数; for (int i = 0; i < b; i++) { scanf("%d", &c[i]);//输入一个有序数组 } int a = 0; scanf("%d", &a); for (int i = b; i >=0; i--)//从最后一个开始比较 { if (a>c[i-1]) { c[i] = a;//如果大于最后一个数,直接赋值,程序循环结束,打印数组 break; } else {//如果小,那就交换 c[i] = c[i - 1]; c[i - 1] = a; } } for (int i = 0; i < b + 1; i++)//打印最后的数组 { printf("%d ", c[i]); } return 0; }
int main() { int n, m; scanf("%d", &n); int arr[n]; for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } scanf("%d", &m); int end = n; int i = 0, j = 0; for (i = 0; i < n; i++) { if (m >= arr[i] && m < arr[i + 1]) { for (j = end; j > i; j--) { arr[j + 1] = arr[j]; } arr[i + 1] = m; break; } else if (m > arr[n - 1]) { arr[end] = m; } else if (m < arr[0]) { for (j = end; j >= 0; j--) { arr[j + 1] = arr[j]; } arr[0] = m; } } for (int i = 0; i < n + 1; i++) { printf("%d ", arr[i]); } return 0; }
#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> #include <malloc.h> int main() { int n = 0; int same_n = 0; int* p = NULL; int tem = 0; int i = 0; scanf("%d", &n); //开辟空间,用来存放数组 p = (int*)malloc(sizeof(int) * (n + 1)); if (!p) { perror("malloc"); return 1; } //录入有序数组 for (i = 0; i < n; i++) { scanf("%d", p + i); } //录入待插入的整数 scanf("%d", &tem); //寻找待插入的位置 for (i = 0; i < n; i++) { if (tem < *(p + i)) { //此时的i,就是tem应该在数组里的位置(下标) break; } } //进行插入操作 same_n = n; while (same_n != i) { //数组从后往前拉 *(p + same_n) = *(p + same_n - 1); same_n--; } *(p + i) = tem; //输出结果 for (i = 0; i <= n; i++) { printf("%d ", *(p + i)); } //释放空间 free(p); p = NULL; return 0; }
#include <stdio.h> int main() { int n = 0; int arr[60] = {0}; while (~scanf("%d", &n))//输入有序数的个数 { int i = 0; for (i = 0; i < n; i++) { scanf("%d", &arr[i]);//输入有序数 } int x = 0; scanf("%d", &x);//要插入的数 for (i = 0; i < n; i++) { if (x < arr[0]) //当小于此组所有的有序数 { for (i = 0; i < n; i++) { arr[n - i] = arr[n - 1 - i]; } arr[0] = x; } else if (x > arr[i] && x < arr[i + 1]) { for (int j = n - 1; j > i; j--) { arr[j + 1] = arr[j]; } arr[i + 1] = x; } else if (x > arr[n - 1]) //当大于此组有序数的所有数 { arr[n] = x; } } for (int k = 0; k <= n; k++) { printf("%d ", arr[k]); } } return 0; }