1.6-1两种排序方法


题目描述 :


考拉有n个字符串字符串,任意两个字符串长度都是不同的
方法:1根据字符串的字典序排序。例如
car<carriage<cats<doggies
方法2根据字符串的长度排序。例如
car<cars<doggies<carriage
考拉想知道自己的这些字符串排列顺序是否满足这两种
输入描述:
第一行为字符串个数n(n≤100)
接下来的n行,每行一个字符串,字符串长度均小于100,
输出描述:
如果这些字符串是根据字典序排列而不是根据长度排序islexicalorder
如果根据长度排列而不是字典序排列输出lengths
如果两种方式都符合输出"both",否则输出none


#include <iostream>
#include <vector>
#include <string>
using namespace std;
 
//看一个字符串数组中的数据是不是按照“字典”顺序排序
// 看一个字符串数组中的数据是不是按照字典顺序排序
bool is_dict(vector<string> &myv){
	
	for(int i =0;i < myv.size() - 1;i ++){
		if(myv[i] > myv[i+1])
			return false;
	} 
	return true; 
}
// 看一个字符串数组中的数据是不是按照长度排序 
bool is_len(vector<string> &myv){
	
	for(int i =0;i < myv.size() - 1;i++){
		if(myv[i].size() > myv[i+1].size())
			return false;
	} 
	return true; 
}

int main() {
	int n;
	cin >> n;
	cin.get();
	vector<string> myv(n);
	for(int i = 0; i < n; i ++) {
		getline(cin,myv[i]);
	}
	if(is_dict(myv) && is_len(myv)){
		cout << "both" << endl;
	} else if(is_dict(myv)){
		cout << "islexicalorder" << endl; 
	} else if(is_len(myv)){
		cout << "lengths" << endl; 
	} else{
		cout << "none" << endl; 
	}

	return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值