郑轻OJ - 1155: 字符串比较 多实例

本文介绍了一个C++程序,该程序实现了两个字符串的自定义比较逻辑。通过忽略大小写并基于字符的ASCII值进行比较,实现了一种更灵活的字符串对比方式。文章详细解释了比较函数的实现过程,并展示了如何使用这个函数来判断两个输入字符串是否按指定规则排序。
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;

char a[10010];
char b[10010];
int mycmp(char* s1, char* s2)
{
	int re = 0;
	int L1 = strlen(s1);
	int L2 = strlen(s2);
	int L = min(L1, L2);
	for (int i = 0; i < L; i++)
	{
		if (tolower(s1[i]) == tolower(s2[i]))
		{
			if (s1[i] > s2[i])
			{
				re = 1;
				break;
			}
			else if (s1[i] < s2[i])
			{
				re = -1;
				break;
			}
			else
				continue;
		}
		else 
		{
			if (tolower(s1[i]) > tolower(s2[i]))
			{
				re = 1;
				break;
			}
			else
			{
				re = -1;
				break;
			}
		}
	}
	if (re == 0)
	{
		if (L1 == L2) re = 0;
		else if (L1 < L2) re = -1;
		else re = 1;
	}
	return re;
}
int main()
{
	while (cin >> a >> b)
	{
		if (mycmp(a, b) == -1)
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}
	return 0;
}

提供了两道郑轻oj题目的答案: - **郑轻oj1075**:该题是马克思手稿中的趣味数学题,使用穷举法计算男人、女人和小孩的人数。代码如下: ```c #include<stdio.h> #include<string.h> #include<math.h> #include<malloc.h> #define men 3 #define women 2 #define children 1 //马克思手稿中有这样一道趣味数学题:男人、女人和小孩总计n个人,在一家饭店里吃饭,共花了cost先令, //每个男人各花3先令,每个女人各花2先令,每个小孩各花1先令,请用穷举法编程计算男人、女人和小孩各有几个。 //输入两个正整数,表示人数n和花费cost。 //若问题有解,则输出所有解,每行输出三个数据,代表男人、女人和小孩的人数,用空格分隔;若问题无解,则输出“No answer"。 int main() { //思路:跟前面两个类似,但是这个是第一种那个思路,列出全部可能,判断 int n=0,cost=0,flag=1; scanf("%d%d",&n,&cost); for(int m=0;m<=n;m++) for(int wm=0;wm<=n;wm++) for(int chi=0;chi<=n;chi++) { if(chi+m+wm==n&&men*m+women*wm+chi*children==cost) { flag=0; printf("%d %d %d\n",m,wm,chi); } } if(flag)printf("No answer"); return 0;//今天又是没带return 0的一天 } ``` - **郑轻oj1053**:该题要求输入一个实数x,计算公式的前10项和。代码如下: ```c #include<stdio.h> #include<string.h> #include<math.h> // 输入x,计算上面公式的前10项和。1053: 正弦函数 //输入一个实数x。 //输出一个实数,即数列的前10项和,结果保留3位小数。 int main() { //思路:又试了好多次 // int factorial2(int x);//从x开始的阶乘 double factorial2(int x);//int装不下 double n2(int x,double y);//位数,值 double x;double y=0; scanf("%lf",&x);//double类型是lf // scanf("%f",&x);经典错误 for(int a=0,i=1,cnt=1;a<10;a++)//进行10次 { //自增变量最好定义要定义在for的第一个表达式内,不然每次循环都重复定义 // int cnt=1;//奇偶判断位,奇正偶负…… double temp=0; if(cnt%2!=0) temp=(double)(n2(i,x)/factorial2(i)); else temp=-(double)(n2(i,x)/factorial2(i)); y+=temp; cnt++; i+=2; } printf("%.3f",y); return 0; } double factorial2(int x)//分母 { double product=1; for(int i=1;i<=x;i++) { product*=i; } // printf("%.0lf \n",product); return product; } double n2(int x,double y)//分子 { double f1=1; while(x--) { f1*=y; } //printf("%.0lf\n",f1); return f1; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值