L1-6 斯德哥尔摩火车上的题

发布时间 2023-03-28 10:29:48作者: Jinx8823

L1-6 斯德哥尔摩火车上的题

#include<iostream>
#include<cstring>
#define maxn 10005
using namespace std;	
int main()
{
	string s1,s2;
	cin>>s1>>s2;
	string s3,s4;
	for(unsigned int i=1;i<s1.length();i++)
	{
		if((s1[i]-'0')%2 == (s1[i-1]-'0')%2)
		{
			s3+=max(s1[i],s1[i-1]);
		}
	}
	for(unsigned int i=1;i<s2.length();i++)
	{
		if((s2[i]-'0')%2 == (s2[i-1]-'0')%2)
		{
			s4+=max(s2[i],s2[i-1]);
		}
	}
	if(strcmp(s3.c_str(),s4.c_str())==0)//用c_str()函数返回当前字符串的首字符地址
	{
		cout<<s3;
	}
	else
	{
		cout<<s3<<endl;
		cout<<s4;
	}
	return 0;
}

上面思路就顺着题意走就可以

这篇文章就是为了记录用strcmp()函数比较两个字符串是否相等时,要用c_str()函数来进行转化

c_str()是Borland封装的String类中的一个函数,它返回当前字符串的首字符地址。c_str()函数返回一个指向正规C字符串的指针常量,内容与本string串相同。这是为了与C语言兼容,在C语言中没有string类型,故必须通过string类对象的成员函数c_str()把string对象转换成C中的字符串样式。