蓝桥题目:B-13、数列排序,B-14、时间转换,B-15、字符串对比

B-13、数列排序


法一、sort()

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
	int n, i;
	
	cin >> n;
	
	int *num = new int[n];
	
	for(i = 0; i < n;){
		cin >> num[i++];
	}
	
//	法一 sort()	
	sort(num ,num + n);

	for(i = 0; i < n;){
		cout <<	num[i++] << " ";
	}
	
	return 0;
} 

法二、选择排序

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
	int n, i;
	
	cin >> n;
	
	int *num = new int[n];
	
	for(i = 0; i < n;){
		cin >> num[i++];
	}

//	法二  选择排序  O(n2) 
	int temp;
	for(i = 0; i < n-1; ++i){
		for(int j = i+1; j < n; ++j)
			if(num[i] > num[j]){
				temp = num[i];
				num[i] = num[j];
				num[j] = temp;
			}
	}
	return 0;
} 

法三、冒泡排序

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
	int n, i;
	
	cin >> n;
	
	int *num = new int[n];
	
	for(i = 0; i < n;){
		cin >> num[i++];
	}
	
//	法三	冒泡排序  O(n)~O(n2)
	int temp;
	for(i = 0; i < n; ++i){
		for(int j = 0; j < n-i-1; ++j){
			if(num[j] > num [j+1]){
				temp = num[j];
				num[j] = num[j+1];
				num[j+1] = temp;
			}
		}
	}

	for(i = 0; i < n;){
		cout <<	num[i++] << " ";
	}
	
	return 0;
} 

其余的多种排序,快拍,归并,插入,哈希等到链表再整理。

B-14、时间转换

#include <iostream>
using namespace std;

int main() {
	
	int n, h, m, s;
	cin >> n;
	
	h = n / 3600 % 24;
	m = n % 3600 / 60;
	s = n % 60;
	
	cout << h << ':' << m << ':' << s;
	
	return 0;
}

B-15、字符串对比


法一,调用三个内置函数,需头文件

#include<iostream>
#include<cstring>
using namespace std;
int main(){
	char arr[11],brr[11];
	cin>>arr;
	cin>>brr;
	if(strlen(arr)!=strlen(brr)) cout<<1<<endl; //先分长度是否相等情况
	else{
		if(!strcmp(arr,brr)) cout<<2<<endl;
		else if(!stricmp(arr,brr)) cout<<3<<endl;
		else cout<<4<<endl;}
	return 0;
}
strcmp/stricmp/strcmpi
strcmp比较两个字符串是否相同,区分大小写。
stricmp比较两个字符串是否相同,不区分大小写。
strcmpi是stricmp的宏定义。
用上述函数需要包涵#include"cstring"

法二、直接通过ASCII码判断,我确实没有想到

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
 
char str1[15],str2[15];
 
int main()
{
    int len1,len2;
    int i;
    scanf("%s%s",str1,str2);
    len1=strlen(str1);
    len2=strlen(str2);
    if(len1!=len2)
        printf("1\n");
    else if(strcmp(str1,str2)==0)
        printf("2\n");
    else
    {
        for(i = 0; i < len1; i++)
            if( str1[i]!=str2[i] && abs(str1[i]-str2[i])!=32 )
                break;
        if(i == len1)
            printf("3\n");
        else
            printf("4\n");
    }
    return 0;
}

法三,全部手写函数

#include <iostream>
#include <cstring>
using namespace std;

void StrToLower(char *str, int len);

int main() {
	
	char str1[10], str2[10];
	
	cin >> str1 >> str2;
	
	int len1 = strlen(str1);
	int len2 = strlen(str2);
	
	if(len1 == len2){
		for(int i = 0; i <= len1; ++i){
			if(str1[i] != str2[i]){
				break;
			}else if(i == len1){
				cout << '2';
				return 0;
			}
		}
	
		StrToLower(str1, len1);
		StrToLower(str2, len2);
		
		for(int i = 0; i <= len1; ++i){
			if(str1[i] != str2[i]){
				cout << '4';
				return 0;
			}else if(i == len1){
				cout << '3';
				return 0;
			}
		}
		
	}else{
		cout << '1'; 
	} 
	
	return 0;
}

void StrToLower(char *str, int len){
	int i = 0;
	while(str[i]){
		if(str[i] >= 'A' && str[i] <= 'Z'){
			str[i] = str[i] + 'a' - 'A';
		}
		//或者直接调用函数 str[i] = tolower(str[i]); 但需要头文件<ctype.h> 
		i++;
	}
}
char test[10] = "12345";
string tt = "123";

cout << sizeof(test) << " " << strlen(test) << " ";
cout << sizeof(tt) << " " << tt.length() << " " << tt.size() << " ";

输出 10 5 3 3 

需要头文件<cstring>即c中的<string.h> 
sizeof() = 所占全部空间,初始化时空间
strlen() 只能针对char数组用 
string类要用需调用类内的函数将自身改为char数组类型 如:strlen(tt.c_str()) 
length() 和 size() 都是针对string的长度函数,无区别 length是c的遗留物 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值