C语言学习过程记录21 练习1

文章描述了两个编程问题:一个是根据日期和车牌尾号规则计算某辆车在2023年能过桥的日子;另一个是处理运算式子,根据输入要求输出整数或英文数字表示的和。
摘要由CSDN通过智能技术生成

1.过桥

有一座桥有着这样的限行方案:禁止当日日期前后两位车牌尾号数字的小型载客汽车通过。例如今天是10月6号,那么尾号是5和7的车将不允许通过。以下有一些车牌号,在2023年这些车分别有多少天能过桥?

#include<iostream>
#include<vector>
#include<string>
#include<sstream>
#include<algorithm>
using namespace std;
int c2i(char s)
{
	int x = 0;
	stringstream ss;
	ss<<s;
	ss>>x;
	return x;
}
int main()
{
	vector<string> vec;
	string test;
	int N = 0;
	int i = 0;
	cin>>N;
	for(i=0;i<N;i++)
	{
		cin>>test;
		vec.push_back(test);
	}
	for(i=0;i<vec.size();i++)
	{
		int count = 0;
		string::reverse_iterator it = vec[i].rbegin();
		if(isdigit(*it)==0||isdigit(*(it+1))==0)
		{
			cout<<365<<endl;
			continue;
		}
		int j = c2i(*it);
		int month = 0;
		for(month =1;month<=12;month++)
		{
			int date = 0;
			if((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12))
			{
				for(date=1;date<=31;date++)
				{
					int loc1 = (j+1)%10;
					int loc2 =	(j+9)%10;
					if((date%10)==loc1||(date%10)==loc2)
					{
						count++;
					}
				}
			}
			else if((month==4)||(month==6)||(month==9)||(month==11))
			{
				for(date=1;date<=30;date++)
				{
					int loc1 = (j+1)%10;
					int loc2 =	(j+9)%10;
					if((date%10)==loc1||(date%10)==loc2)
					{
						count++;
					}
				}
			}
			else
			{
				for(date=1;date<=28;date++)
				{
					int loc1 = (j+1)%10;
					int loc2 =	(j+9)%10;
					if((date%10)==loc1||(date%10)==loc2)
					{						count++;
					}
				}
			}		
		}
		int dac = 365-count;
		cout<<dac<<endl;	
	}
	return 0;
} 

2.计算A+B

第一行输入T,代表T组数据。

 下面每组数据: 

第一行输入0或1。0为输出整数,1为输出英文(英文只输出每一位代表的数字:如80则输出 eight zero)。输入输出的均为小写字母。 第二行输入运算式子,形式为:A + B =(A,B可以都是整数,也可以都是英文,也可以A,B一个为整数,一个为英文)。

输入样例

3
0
zero + 1 =
1
eight + eight =
1
eight zero + eight one =

输出样例:

1
one six
one six one

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<sstream>
#include<math.h>
using namespace std;
int s2i (string s)
{
	int X = 0;
	stringstream ss;
	ss<<s;
	ss>>X;
	return X;
}
string i2s(int o)
{
	string p;
	stringstream sss;
	sss<< o;
	sss>> p;
	return p;
}
string turnint(int q)
{
	string t;
	if(q==0)
	t = "zero";
	else if(q==1)
	t = "one";
	else if(q==2)
	t = "two";
	else if(q==3)
	t = "three";
	else if(q==4)
	t = "four";
	else if(q==5)
	t = "five";
	else if(q==6)
	t = "six";
	else if(q==7)
	t = "seven";
	else if(q==8)
	t = "eight";
	else if(q==9)
	t = "nine";
	return t;
}
int turnss(string st1)
{
	int x = 0;
	if(st1.compare("one")==0)
	x = 1;
	else if(st1.compare("two")==0)
	x = 2;
	else if(st1.compare("three")==0)
	x = 3;
	else if(st1.compare("four")==0)
	x = 4;
	else if(st1.compare("five")==0)
	x = 5;
	else if(st1.compare("six")==0)
	x = 6;
	else if(st1.compare("seven")==0)
	x = 7;
	else if(st1.compare("eight")==0)
	x = 8;
	else if(st1.compare("nine")==0)
	x = 9;
	else if(st1.compare("zero")==0)
	x = 0;
	return x;
}
int becomeint(string st2)
{
	int y = 0;
	int ref = st2.find_first_of(" ");
	if(ref == string::npos)
	{
		y = turnss(st2);
		return y;
	}
	else
	{
		while(ref!=string::npos)
		{
			int j = 0;
			j = turnss(st2.substr(0,ref));
			y = y+j;
			y*=10;
			st2.erase(0,ref+1);
			ref=st2.find_first_of(" ");
		}
		y+=turnss(st2);
		return y;
	}
}
int main()
{
	int T = 0;
	int n = 0;
	vector<string> vec;
	cin>>T;
	while(T--)
	{
		cin>>n;
		getchar();
		string str;
		string strA;
		string strB;
		int res = 0;
		getline(cin,str);
		int loc1 = str.find("+");
		strA = str.substr(0,loc1-1);
		str.erase(0,loc1+2);
		int loc2 = str.find("=");
		strB = str.substr(0,loc2-1);
		int test1 = isdigit(strA[0]);
		int test2 = isdigit(strB[0]);
		if(test1!=0&&test2!=0)
		{
			int A = s2i(strA);
			int B = s2i(strB);
			res = A+B;
		}
		else if(test1==0&test2!=0)
		{
			int B = s2i(strB);
			int A = becomeint(strA);
			res = A+B;	
		}
		else if(test1!=0&&test2==0)
		{
			int A = s2i(strA);
			int B = becomeint(strB);
			res = A+B;
		}
		else if(test1==0&&test2==0)
		{
			int A = becomeint(strA);
			int B = becomeint(strB);
			res = A+B;
		}
		
		
		if(n==0)
		{
			string Res = i2s(res);
			vec.push_back(Res);
		}
		else if(n==1)
		{
			string Res;
			while(res!=0)
			{
				int j = 0;
				string tt;
				j = res%10;
				tt = turnint(j);
				Res.insert(0," ");
				Res.insert(0,tt);
				res = res/10;
			}
		vec.push_back(Res);
		}
	}
	int i = 0;
	for(i=0;i<vec.size();i++)
	cout<<vec[i]<<endl;
	return 0;
}

运行结果:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值