1. 程式人生 > >牛客網第五天的訓練

牛客網第五天的訓練

基礎題:數字顛倒

用個棧,先把這個數字從尾到頭依此入棧,然後再依此輸出就好了。

#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 30;
int main()
{
	int num,stc;
	int a[maxn];
	while(cin>>num)
	{
		stc=0;								//棧頂
		while(num)						//將這個數從尾到頭依此進棧
		{
			a[stc++]=num%10;
			num/=10;	
		}
		for(int i=0;i<stc;++i)		   //輸出
		{
			cout<<a[i];
		}
		cout<<endl;
	}
	return 0;	
} 

進階題: 牛牛的遊戲

直接判斷第一個字串的最後一個字母和第二個字串的第一個的字母是否相等,
同時再判斷,第二個字串個最後一個和第三個字串的第一個字母是否相等就完了。

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	string s1,s2,s3;
	while(cin>>s1>>s2>>s3)
	{
		int alen=s1.length()-1;
		int blen=s2.length()-1;
		if(s1[alen]==s2[0] && s2[blen]==s3[0])
		{
			cout<<"YES"<<endl;
		}else cout<<"NO"<<endl; 		
	}
	return 0;
}