考试题

发布时间 2024-01-01 19:28:46作者: 何平安

元素前移

#include<stdio.h>

#include<stdlib.h>

int main()

{ int n,i,j,a[200];

scanf("%d",&n);

for(i=0;i<n;i++)

{
scanf("%d",&a[i]);

}

printf("\n");
for(i=j=n-1;i>=0;i--)

if(a[i])a[j--]=a[i];

for(;j>=0;)a[j--]=0;

for(i=0;i<n;i++)
for(i=0;i<n;i++)

printf("%d ",a[i]);

return 0;

}

 选择排序法

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void paixu(int n,int a[]) {
int i,j,k,t;
for (i=0;i<n-1;i++) { // 外循环控制趟数(n-1)
k = i; // 假设当前第一个数为最值,存在k中
for(j=i+1;j<n;j++) { // 内循环从第二个数开始
if (a[k]>a[j]) // 如果k的值大于正在比较的值
k = j; // 存入k中
}
if(k!=i) { // 如果较小的值不是一开始存的值,开始互换
t = a[k];
a[k] = a[i];
a[i] = t;
}
}
}

int main() {
char str[100];
int p[100];
int num = 0;
fgets(str,sizeof(str),stdin); // 在Linux下,使用fgets代替fgets()
char *token = strtok(str, " ");
while(token != NULL) {
// printf("%s ", token); // 如果你想打印每个token,可以取消注释这一行
int a = atoi(token); // 使用atoi代替atoi()
p[num] = a;
num++;
token = strtok(NULL, " "); // 使用strtok代替strtok()
}
int arr[num+1];
int i;
for(i=0;i<num+1;i++) {
arr[i] = p[i];
}
paixu(num, arr);
for(i=0; i< num;i++) {
printf("%d ",arr[i]);
}
return 0;
}

 合并有序数组

#include<stdio.h>
int main(){
int a[20],b[20],c[40];

int i,j,m,n,t;

scanf("%d" ,&m);
for(i=0;i<m;i++){
scanf("%d",&a[i]);
c[i] = a[i];

}
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&b[i]);
c[i+m] = b[i];

}
for(j=1;j<m+n;j++)
for(i=0;i<m+n-j;i++){
if(c[i]>c[i+1]){
t=c[i];
c[i]= c[i+1];
c[i+1] =t;
}
}

for(i=0;i<m+n;i++)
printf("%d ",c[i]);
return 0;

}

 单链表的创建

#include <iostream>

using namespace std;

// 定义单链表节点结构体
struct Node {
int data; // 数据域
Node* next; // 指针域,指向下一个节点
};

// 头插法创建单链表
Node* createListFromHead(int len) {
Node* head = new Node;
head->next = NULL;
for (int i = 0; i < len; i++) {
int val;
cin >> val;
Node* newNode = new Node;
newNode->data = val;
newNode->next = head->next;
head->next = newNode;
}
return head->next;
}

// 尾插法创建单链表
Node* createListFromTail(int len) {
Node* head = new Node;
head->next = NULL;
Node* tail = head;
for (int i = 0; i < len; i++) {
int val;
cin >> val;
Node* newNode = new Node;
newNode->data = val;
newNode->next = NULL;
tail->next = newNode;
tail = newNode;
}
return head->next;
}

// 输出单链表
void printList(Node* head) {
if (head == NULL) {
cout << "list is empty!" << endl;
return;
}
Node* p = head;
while (p != NULL) {
cout << p->data << " ";
p = p->next;
}
cout << endl;
}

int main() {
int method, len;
cin >> method >> len;
cout << "" << endl;
Node* head = NULL;
if (method == 1) {
head = createListFromHead(len);
}
else if (method == 2) {
head = createListFromTail(len);
}
cout << "" << endl;
printList(head);
return 0;
}

快速排序

#include <iostream>
using namespace std;

void quickSort(int arr[], int low, int high, int& count) {
if (low < high) {
int pivot = arr[low];
int i = low, j = high;

while (i < j) {
while (i < j && arr[j] >= pivot) {
j--;
}
if (i < j) {
arr[i++] = arr[j];
count++;
}

while (i < j && arr[i] <= pivot) {
i++;
}
if (i < j) {
arr[j--] = arr[i];
count++;
}
}

arr[i] = pivot;
count++;

quickSort(arr, low, i - 1, count);
quickSort(arr, i + 1, high, count);
}
}

void printArray(int arr[], int size) {
for (int i = 0; i < size; ++i) {
cout << arr[i] << " ";
}
cout << endl;
}

int main() {
int arr[200];
int num;

int i = 0;
while (cin >> num && num != -1) {
arr[i++] = num;
}
int size = i;

cout << "before:";
printArray(arr, size);

int count = 0;
quickSort(arr, 0, size - 1, count);

cout << "after:";
printArray(arr, size);

return 0;
}

链表操作

#include <iostream>
using namespace std;

struct ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(NULL) {}
};

ListNode* insertNode(ListNode* head, int num) {
ListNode* newNode = new ListNode(num);
newNode->next = head;
head = newNode;
return head;
}

ListNode* deleteNode(ListNode* head, int num) {
if (head == NULL) {
return head; // 空链表,直接返回
}

ListNode* prev = NULL;
ListNode* curr = head;

while (curr != NULL) {
if (curr->val == num) {
if (prev == NULL) {
head = curr->next;
}
else {
prev->next = curr->next;
}
delete curr;
break;
}
prev = curr;
curr = curr->next;
}

return head;
}

void printList(ListNode* head) {
ListNode* current = head;
while (current != NULL) {
cout << current->val << " ";
current = current->next;
}
cout << endl;
}

int main() {
int num;
ListNode* head = NULL;

while (cin >> num && num != -1) {
head = insertNode(head, num);
}

cout << "" << endl;
int target;
cin >> target;
head = deleteNode(head, target);

cout << "" << endl;
printList(head);

return 0;
}

顺序表的插入

#include <iostream>
#include <vector>

using namespace std;

bool insertElement(vector<int>& seqList, int pos, int elem) {
int len = seqList.size();

// 判断插入位置是否合法
if (pos < 1 || pos > len + 1) {
cout << "The insertion position does not exist" << endl;
cout << "" << len << endl;
return false;
}

// 在指定位置插入元素
seqList.insert(seqList.begin() + pos - 1, elem);

// 输出插入后的顺序表
cout << "";
for (int i = 0; i < seqList.size(); i++) {
cout << seqList[i] << " ";
}
cout << endl;

return true;
}

int main() {
int len;
vector<int> seqList;
int pos, elem;

// 输入顺序表的长度
cin >> len;

// 输入顺序表的数据元素
for (int i = 0; i < len; i++) {
int num;
cin >> num;
seqList.push_back(num);
}

// 输入插入位置和插入元素
cin >> pos >> elem;

// 调用插入函数
insertElement(seqList, pos, elem);

return 0;
}