数据结构与算法02复盘

a用数组实现,b用链表实现,先a后b

主函数用switch 

注意初始化参数

#include<iostream>
const int MAXN = 10000;
int arr[MAXN];
int m,n;
using namespace std;

void Insert(int i,int x)
{
	cin>>i>>x;
	for(int j = n+1;j > i;j--)
	{
		arr[j] = arr[j-1];
	}
	arr[i] = x;
	n++;
}

void DeleteByIndex(int i)
{
	//cin>>i;
	for(int j = i;j < n;j++)
	{
		arr[j] = arr[j+1];
	}
	n--;
}

int Find(int x)
{
	cin>>x;
	for(int j = 0;j < n;j++)
	{
		if(arr[j] == x)
		{
			return j;
			break;
		}
	}
	return 0;
}

void Count(int x,int y)
{
	//cin>>x>>y;
	int cnt = 0;
	for(int j = 0;j< n;j++)
	{
		if(arr[j]>=x&&arr[j]<=y) cnt++;
	}
	cout << cnt;
}

void EliminateRepeat()
{
	for(int i=0;i<n-1;++i)	
	{
        for(int j=i+1;j<n;++j)
            if(arr[i]==arr[j])	
            {
                DeleteByIndex(j);
    		    --j;				//因为有++j,所以这里先减一下,否则num[i]比较的是移动之后的下一位,会漏掉一个数
   	   		}
   }
}

void DeleteByRange(int x,int y)
{
	cin>>x>>y;
	for(int j = 0;j< n;j++)
	{
		if(arr[j]>=x&&arr[j]<=y)
		{
			DeleteByIndex(j);
		}
	}
}

int main()
{
	int c, i = 0, x = 0, y = 0;
	cin >> m;
	while(m--)
	{
		cin >> c;
		switch(c)
		{
			case 1 : Insert(i,x); break;
			case 2 : cin>>i; DeleteByIndex(i); break;
			case 3 : cout << Find(x) << endl; break;
			case 4 : cin>>x>>y; Count(x,y); break;
			case 5 : EliminateRepeat(); break;
			case 6 : DeleteByRange(x,y); break;
		}
	}
	return 0;
}

deletebyindex可以反复调用,这点要注意

但是反复调用就不能再把cin>>写在函数里面了

这也是我最后一行始终不输出2的原因,看来平时还是练少了!

基础的才是最重要的

思路

扫描二维码关注公众号,回复: 17560633 查看本文章

两个指针遍历a b数组,从小的输出,(把小的先放进c数组)

#include<iostream>
#include<stdio.h>

using namespace std;
int a[1000001];
int b[1000001];
int c[2000001];
int m,n;

int main() {
	cin >> m >> n ;
	for (int i = 0; i < m; i++) scanf("%d", &a[i]);
	for (int i = 0; i < n; i++) scanf("%d", &b[i]);
	
	int p=m+n;
	int x=0;
	int y=0;
	
	for(int i=0;i<p;i++)
	{
		printf("%d ",(y==n||x<m&&a[x]<b[y])?a[x++]:b[y++]);
	}

	return 0;
} 

#include<stdio.h>
#include<malloc.h>

struct node{
    int data;
    node *next;
};

node *createLinkList(int n) {  // 尾插入法 
    node *head, *tail, *p;
    head = tail = (node*) malloc(sizeof(node));
    tail->next = NULL;
    for (int i = 0; i < n; ++ i) {
        int x;
        scanf("%d", &x);
        p = (node*) malloc(sizeof(node));
        p->data = x; p->next = NULL;//每次都可以往后插入一个元素
        tail->next = p; tail = p;
    }
    return head;
}

void LinkList_Print(node *A) {
    node *p = A->next;
    while (p != NULL) {
        printf("%d ", p->data); p = p->next;
    }
}

node *combineLinkList(node *A, node *B) {
node *head, *tail, *p;
head = tail = (node*)malloc(sizeof(node));
tail->next = NULL;

A = A->next; B = B->next;
while (A != NULL && B != NULL) {
if (A->data < B->data) {
A = A->next;
} else if (B->data < A->data) {
B = B->next;
} else {
p = (node*)malloc(sizeof(node));
p->data = A->data; p->next = NULL;
tail->next = p; tail = p;//核心
A = A->next;
B = B->next;
}
}
return head;
}

int main() {
    int m, n;
    scanf("%d%d", &m, &n);
    node *A = createLinkList(m);
    node *B = createLinkList(n);
    node *C = combineLinkList(A, B);
    LinkList_Print(C);
	return 0;
}

在不用别的库的情况下,用这个技巧

#include <cstdio>

char s[1 << 20]; // 定义一个大小为 2^20 的字符数组

int main() {
    scanf("%s", s); // 从输入中读取一个字符串并存储到数组 s 中
    char *st = s; 
    while (*st) // 移动指针 st 到字符串末尾的空字符 '\0'
        st++;
    
    // 不能使用 <cstring>,意味着不能使用标准 C++ 的字符串库,如 strlen、strcpy 等

    scanf("%s", st); // 从输入中读取另一个字符串,并将其追加到数组 s 的末尾

    printf("%s", s); // 打印数组 s 中的字符串
}

字符串的名称本质上是指向字符串首字母的指针

字符串也是字符数组,s = "qwerty";

s[1] = "w";因此以下功能也很好实现

找到对应下标然后按顺序输出一定个数即可

#include <cstdio>

int main() {
    const int maxStringLength = 100; // 最大字符串长度,根据实际情况调整
    char inputString[maxStringLength]; // 你的字符串

    printf("Enter a string: ");
    scanf("%s", inputString);

    int startPosition, substringLength;

    printf("Enter the start position: ");
    scanf("%d", &startPosition);

    printf("Enter the substring length: ");
    scanf("%d", &substringLength);

    int inputStringLength = 0;
    while (inputString[inputStringLength] != '\0') {
        inputStringLength++;
    }

    if (startPosition >= 0 && startPosition + substringLength <= inputStringLength) {
        // 检查起始位置和子串长度的有效性

        printf("Substring: ");
        for (int i = startPosition; i < startPosition + substringLength; i++) {
            printf("%c", inputString[i]);
        }
        printf("\n");
    } else {
        printf("Invalid start position or substring length.\n");
    }

    return 0;
}

回文串的识别 

#include <iostream>
#include <cstring>

bool isPalindrome(const char *str) {
    int len = strlen(str);
    int i = 0;
    int j = len - 1;

    while (i < j) {
        if (str[i] != str[j]) {
            return false; // 如果字符不相等,不是回文串
        }
        i++;
        j--;
    }

    return true; // 如果所有字符都相等,是回文串
}

int main() {
    const int maxStringLength = 100; // 最大字符串长度,根据实际情况调整
    char inputString[maxStringLength];

    std::cout << "Enter a string: ";
    std::cin.getline(inputString, maxStringLength);

    if (isPalindrome(inputString)) {
        std::cout << "The entered string is a palindrome." << std::endl;
    } else {
        std::cout << "The entered string is not a palindrome." << std::endl;
    }

    return 0;
}