(13、14)/48(5_20) 编程

1、参数解析

void CmdlineParse(const string &str)
{
	vector<string> svec;
	string temp = "";
	bool flag = false;  //判断是否处于字符串的状态
	for (int i = 0; i<str.size(); ++i){
		if (str[i] == '"')//字符串的起始或结束
		{
			flag = !flag;
		}
		else if (str[i] == ' '&&!flag)  //参数的分隔符或是否为字符串的内容
		{
			svec.push_back(temp);
			temp = "";
		}
		else
		{
			temp += str[i];
		}
	}
	svec.push_back(temp);   //最后一个参数
	cout << svec.size() << endl;
	for (int i = 0; i<svec.size(); ++i){
		cout << svec[i] << endl;
	}
}

int main() {
	string str = "";
	while (getline(cin, str)) {
		CmdlineParse(str);
	}
}

2、跳石板(动态规划)

void get_div_num(vector<int>& a, int x) {
	for (int i = 2; i <= sqrt(x); ++i) {
		if (x % i == 0) {
			a.push_back(i);
			if (x / i != i)
				a.push_back(x / i);
		}
	}
}

int Jump(int n, int m) {
	vector<int> step(m + 1, INT_MAX);
	step[n] = 0;
	for (int i = n; i < m; ++i) {
		if (step[i] == INT_MAX)
			continue;
		vector<int> a;
		get_div_num(a, i);
		for (int j = 0; j < a.size(); j++) {
			if (i + a[j] <= m && step[i + a[j]] != INT_MAX) {
				step[j + a[j]] = step[i + a[j]] > step[i] + 1 ? step[i] + 1 : step[i + a[j]];
			}
			else if (a[j] + i <= m) {
				step[a[j] + i] = step[i] + 1;
			}
		}
	}
	return step[m] == INT_MAX ? -1 : step[m];
}

int main() {
	int n, m;
	int min_step = 0;
	while (cin >> n >> m) {
		min_step = Jump(n, m);
		cout << min_step << endl;
	}
	return 0;
}

3、计算日期到天数转换

//判断闰年
bool isleap(int year)
{
	return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}
//根据年与月返回当月的天数
int GetDaysByYM(int year, int month)
{
	int days[13] = { 29, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	if (month == 2 && isleap(year))
	{
		month = 0;
	}
	return days[month];
}
//根据年月日返回当前日期是当年的第几天
int GetdateByYMD(int year, int month, int day)
{
	int days = 0;
	//循环加上月数减一的整天数
	for (int i = 1; i<month; ++i){
		days += GetDaysByYM(year, i);
	}
	//最后加上当月已经过的天数day
	days += day;
	return days;
}

int main() {
	int year, month, day;
	while (cin >> year >> month >> day) {
		day = GetdateByYMD(year, month, day);
		cout << day << endl;
	}
}

4、幸运袋子(回溯)

int getLuckyPacket(vector<int> &x, int n, int pos, int sum, int multi){
	int count = 0;
	for (int i = pos; i<n; i++)
	{
		sum += x[i];
		multi *= x[i];
		//判断幸运
		if (sum>multi)
		{
			count += 1 + getLuckyPacket(x, n, i + 1, sum, multi);
		}
		else if (x[i] == 1)  //1 1 3
		{
			count += getLuckyPacket(x, n, i + 1, sum, multi);
		}
		else    //1 3 1   -----   1 1 3
		{
			break;
		}
		sum -= x[i];
		multi /= x[i];

		while (i<n - 1 && x[i] == x[i + 1])
		{
			i++;
		}
	}
	return count;
}

int main() {
	int n, m;
	while (cin >> n) {
		vector<int> x(n);
		for (int i = 0; i<n; ++i)
		{
			cin >> x[i];
		}
		sort(x.begin(), x.end());
		cout << getLuckyPacket(x, n, 0, 0, 1) << endl;
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值