C++#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
void swap(int& a, int& b) {
int t = a;
a = b;
b = t;
}
using std::vector, std::string;
using std::cout, std::cin, std::endl;
void show(string str, vector<int> arr) {
cout << str << ": ";
for (const int& num : arr) {
cout << num << " ";
}
cout << endl;
}
int main(void) {
int a = 1, b = 100, num;
vector<int> arr;
srand((unsigned)time(NULL));
for (int i = 0; i < 10; ++i) {
arr.push_back((rand() % (b - a + 1)) + a);
}
show("初始数组", arr);
for (int i = 0; i < 9; ++i) {
for (int j = i + 1; j < 10; ++j) {
if (arr[i] < arr[j]) {
swap(arr[i], arr[j]);
}
}
}
show("降序排序", arr);
cin >> num;
for (int i = 0; i < 10; ++i) {
if (arr[i] < num) {
arr.insert(arr.begin() + i, num);
break;
}
}
show("插入整数", arr);
return 0;
}
C#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void swap(int *a, int *b) {
int t = (*a);
(*a) = (*b);
(*b) = t;
}
void show(char *str, int *arr, size_t N) {
printf("%s ", str);
for (int i = 0; i < N; ++i) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main(void) {
int a = 1, b = 100, num;
int arr[11];
time_t t;
srand((unsigned)time(NULL));
for (int i = 0; i < 10; ++i) {
arr[i] = (rand() % (b - a + 1)) + a;
}
show("初始数组", arr, 10);
for (int i = 0; i < 9; ++i) {
for (int j = i + 1; j < 10; ++j) {
if (arr[i] < arr[j]) {
swap(&arr[i], &arr[j]);
}
}
}
show("降序排序", arr, 10);
scanf("%d", &num);
for (int i = 0; i < 10; ++i) {
if (arr[i] < num) {
for (int j = 10; j > i; --j) {
arr[j] = arr[j - 1];
}
arr[i] = num;
break;
}
}
show("插入整数", arr, 11);
return 0;
}
Pythonimport random
arr = [random.choice(range(1, 101)) for _ in range(10)]
print("初始数组:", *arr)
for i in range(9):
for j in range(i + 1, 10):
if arr[i] < arr[j]:
arr[i], arr[j] = arr[j], arr[i]
print("降序数组:", *arr)
num = int(input())
for i in range(10):
if arr[i] < num:
arr.insert(i, num)
break
print("插入整数:", *arr)
|