2020011125 梁烁烁 2023.2.28

概论 8 判断字符串是否为回文 

#include<string> 
#include <iostream>
using namespace std;
bool Isopp(string str){
	int i = 0,j = str.length()-1;
	while(i<j){
		if(str[i]!=str[j])
			return false;
		i++;
		j--;
	}
	return true;
}

int main(){
	string str;
	cout<<"请输入字符串:"<<endl;
	cin>>str;
	if(Isopp(str))
		cout<<str<<"是回文"<<endl;
	else
		cout<<str<<"不是回文"<<endl;
} 

概论 12 相差最小元素对个数(1)

#概论 12 相差最小元素对个数(1)
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int solve(vector<int> &myv){
	sort(myv.begin(),myv.end());
	int ans = 1;
	int mindif = myv[1] - myv[0];
	for(int i = 2;i<myv.size();i++){
		if(myv[i]-myv[i-1]<mindif){
			ans = 1;
			mindif = myv[i] - myv[i-1];
		}
		else if(myv[i] - myv[i-1]==mindif){
			ans++;
		}
	}
	return ans;
}

int main(){
	int a[] = {4,1,2,3};
	int n = sizeof(a)/sizeof(a[0]);
	vector<int> myv(a,a+n);
	cout<<"相差最小的元素对个数"<<solve(myv)<<endl;
}

概论 12 相差最小元素对个数其它排序算法

#概论 12 相差最小元素对个数其它排序算法 
#include<stdio.h>
int Min(int a[],int n);
void BubbleSort(int a[],int n);
void InsertSort(int a[],int n);
void ShellSort(int a[], int n);
void swap(int a[],int low,int high);
int partition(int a[],int low,int high);
void quicksort(int a[],int low,int high);

int main(){
	int a[] = {9,8,1,3,2,4,5,0};
	int n = sizeof(a)/sizeof(a[0]);
//	BubbleSort(a,n);
//	InsertSort(a,n);
//	ShellSort(a,n);
	quicksort(a,0,n-1);
	printf("%d",Min(a,n));
	return 0;
	
} 

int Min(int a[],int n){
	int num = 1;
	int findmin = a[1] - a[0];
	for(int i = 2;i<n;i++){
		if(a[i]-a[i-1]<findmin){    
			num = 1;
			findmin = a[i] - a[i-1];
		}
		else if(a[i]-a[i-1]==findmin){
			num++;
		}
	}
	return num;
}

void BubbleSort(int a[],int n){     //冒泡排序
	for(int i = 0;i<n;i++)        
		for(int j = i+1;j<n;j++){
			if(a[i]>a[j]){
				int temp;
				temp = a[i];
				a[i] = a[j];
				a[j] = temp;
			}
		}
} 

void InsertSort(int a[],int n){    //插入排序 
	int temp;
	for(int i = 0;i<n-1;i++){
		temp = a[i+1];
		int k = i;
		while(k>=0){
			if(a[k]>temp){
				a[k+1] = a[k];
				--k;
			}
			else
				break;
		}
		a[k+1] = temp;
		}
}

void ShellSort(int a[], int n){   //希尔排序 
	int gap = n/2;
	int j,temp;
	while(gap!=0){
		for(int i=gap;i<n;i++){
			temp = a[i];
			for(j=i-gap;j>=0;j=j-gap){
				if(a[j]<temp)
					break;
				a[j+gap] = a[j];
			}
			a[j+gap] = temp;
		}
		gap = gap/2;	
	}
}

void swap(int a[],int low,int high){
	int t;
	t = a[low];
	a[low] = a[high];
	a[high] = t;
}

int partition(int a[],int low,int high){
	int point = a[low];
	while(low<high){
		while(low<high&&a[high]>=point)
			high--;
		swap(a,low,high);
		while(low<high&&a[low]<=point)
			low++;
		swap(a,low,high);
	}
	return low;
}

void quicksort(int a[],int low,int high){    //快排 
	if(low<high){
		int point = partition(a,low,high);
		
		quicksort(a,low,point-1);
		quicksort(a,point+1,high);
	}	
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值