救命这个怎么写
随机产生10个1~100之间的整数存放于数组a中,将这些数据按降序排列。再从键盘输入一个整数,将这个整数插入到a数组中使得数组中的整数仍然按降序排列 本帖最后由 临时号 于 2022-6-6 15:10 编辑#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int a;
int i;
int j;
int b;
srand(time(0));
for(i=0;i<10;i++){
a = rand()%100+1;
}
for(i=0;i<10;i++){
b=i;
for(j=b+1;j<10;j++){
if(a<a){
b=j;
}
}
if(b-i){
j=a;
a=a;
a=j;
}
}
scanf("%d",a+10);
for(i=10;i;i--){
if(a<=a){
break;
}
b=a;
a=a;
a=b;
}
// 观察数组
for(i=0;i<11;i++){
printf("%d ",a);
}
printf("\n");
return 0;
} 本帖最后由 jhq999 于 2022-6-6 17:18 编辑
用数组模拟链表
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a={0},i,j;
srand(time(0));
a=a=0;
for( i=2;i<22;i+=2)
{
a=rand()%100+1;
for(j=0;a;j=a)
{
if(a<a])break;
}
a=a;
a=i;
}
j=a;
while(a)
{
printf("%4d",a);
j=a;
}
scanf("%d",a+22);
for(j=0;a;j=a)
{
if(a<a])break;
}
a=a;
a=22;
j=a;
while(a)
{
printf("%4d",a);
j=a;
}
return 0;
} 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 < arr) {
swap(arr, arr);
}
}
}
show("降序排序", arr);
cin >> num;
for (int i = 0; i < 10; ++i) {
if (arr < 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);
}
printf("\n");
}
int main(void) {
int a = 1, b = 100, num;
int arr;
time_t t;
srand((unsigned)time(NULL));
for (int i = 0; i < 10; ++i) {
arr = (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 < arr) {
swap(&arr, &arr);
}
}
}
show("降序排序", arr, 10);
scanf("%d", &num);
for (int i = 0; i < 10; ++i) {
if (arr < num) {
for (int j = 10; j > i; --j) {
arr = arr;
}
arr = num;
break;
}
}
show("插入整数", arr, 11);
return 0;
}Pythonimport random
arr =
print("初始数组:", *arr)
for i in range(9):
for j in range(i + 1, 10):
if arr < arr:
arr, arr = arr, arr
print("降序数组:", *arr)
num = int(input())
for i in range(10):
if arr < num:
arr.insert(i, num)
break
print("插入整数:", *arr) 本帖最后由 jhq999 于 2022-6-6 18:51 编辑
#include <bits/stdc++.h>
#include <vector>
using namespace std;
int numcmp(int num1,int num2)
{
return num1<num2;
}
int main()
{
vector<int> s;
srand(time(0));
for(int i=0;i<10;i+=1)
{
s.push_back(rand()%100+1);
}
sort(s.begin(),s.end(),numcmp);
for(int i:s)printf("%4d",i);
printf("\n");
int r;
scanf("%d",&r);
s.push_back(r);
sort(s.begin(),s.end(),numcmp);
for(int i:s)printf("%4d",i);
return 0;
} 来整一个“现代 C++ ”版本
#include<algorithm>
#include<iostream>
#include<iterator>
#include<random>
#include<vector>
using namespace std;
int main(){
mt19937_64 engine{ random_device()() };
uniform_int_distribution<> distribute(1, 100);
vector<int> array(10);
generate(array.begin(), array.end(), [&engine, &distribute](){ return distribute(engine); });
sort(array.begin(), array.end(), greater<>());
ranges::copy(array, ostream_iterator<int>(cout, " "));
cout << endl;
int new_value;
cin >> new_value;
const auto bound = upper_bound(array.cbegin(), array.cend(), new_value, greater<>());
array.insert(bound, new_value);
ranges::copy(array, ostream_iterator<int>(cout, " "));
cout << endl;
return 0;
}
页:
[1]