备战蓝桥杯-3

一、
题目:

 解题思路:想法比较暴力,如果输入的数不是十进制,我先转为十进制后再转为我需要转换的进制,这里比较繁琐的一件事就是字符串、单字符、整数之间的转来转去。有点晕。

代码:

#include<bits/stdc++.h>
using namespace std;
//n进制转换十进制
int sjz(string s,int jz) {
	int sum = s.size();
	int count=0;
	for (int i=0; i <sum; i++) {
		int  c;
//是不是数字
		if (isdigit(s[i])) {
			c = s[i]-48;
		}
		else {
//因为A表示10,所以其他字符与A的差再加上10就是该字符表示的数。
			c = s[i] - 'A' + 10;
		}
		count += c*pow(jz, sum - i-1);
	}
	return count;
}

//十进制转换n进制
string njz(string s, int  jz) {
	stack  <char> sta;
	int sum = s.size();
	int count=0;
//因为拿进来的是字符串,所以这里逐一分解回数字。
	for (int i = 0; i < sum; i++) {
		int c = s[i]-48;
		c = c * pow(10, sum - i-1);
		count += c;
	}
//开始进制转换咯
	while (count/jz)
	{
		int y = count % jz;
		if (y < 10) {
			char str = (char)(y+'0');
//进栈
			sta.push(str);
			count = count / jz;
		}
//像是十六进制,余数可能是超过十的,那么就要将其转为A/B/C....
		else {
			int d = y - 10;
			char str = 'A';
			str += d;
//进栈
			sta.push(str);
			count = count / jz;
		}
			
	}
	if (count < 10) {
		char str = (char)(count+'0');
		sta.push(str);
	}
	else {
		int d = count - 10;
		char str = 'A'+d;
		cout << str;
		sta.push(str);
	}
	int shuliang = sta.size();
	string zhi = "";
	for (int i = 0; i < shuliang; i++) {
//开始出栈了,用一个字符串一个个接受,这里接受的时候用的是push_back(),如果不用这个接受A/B/C的时候,会将其变成数字。
		zhi.push_back(sta.top());
		sta.pop();
	}
	return zhi;
}
int main() {
	int n, m;
	string s;
	cin >>n;
	cin >> s;
	cin >> m;
	if (n == 10) {
		cout<<njz(s, m);
	}
	else if (m == 10) {
		cout << sjz(s, n);
	}
	else
	{
//把转为十进制的出来的数要转为字符串
		s = to_string(sjz(s, n));
		cout << njz(s, m);
	}
	
	return 0;
}

二、
题目:

解题思路:一个个数拿进去试,题目是围成一圈,我是想象为排了一条队伍,前面K个是好人,后面K个是坏人,只要每次数到m的时候不是前K个人,并且出局了K个(都是坏人)的时候,那么这个数就是最小的m了。 

代码:

#include<iostream>
using namespace std;
int main() {
	int k;
	cin >> k;
	int count = 0;
	int num = k+1;
	int sum = 2 * k;
	int y = 1;
	while (true)
	{
		 y =(num+y-1) % sum;
		if (y > k||y==0) {
			count++;
			if (y == 0) {
				y = 1;
			}
			if (count == k) {
				break;
			}
			else
			{
				sum--;
			}
		}
		else
		{
			count = 0;
			num++;
			sum = 2 * k;
			y = 1;
		}
	}
	cout << num;
	return 0;
}

三、
题目:

代码(注意一下,有些地方的单词之间是有空格的!):

#include<bits/stdc++.h>
#include <string>
#include <iostream>
using namespace std;
map<string, int> m;
int s, n, k;
string st;
int main() {
    cin >> n >> k;
    getline(cin, st);
    for (int i = 1; i <= n; i++)
    {
        getline(cin, st);
        m[st]++;
    }
    for (int i = 1; i <= k; i++)
    {
        getline(cin, st);
        if (m.count(st) == 1)
            s++;
    }
    cout << s << endl;
    return 0;
}

 四、
题目:

 解题思路:就是让等待时间最短的往前面排,这样后面累加的等待时间就会减少,那么平均的等待时间就是最小的。
代码:

#include <string>
#include <iostream>
#include<iomanip>
#include<algorithm>
using namespace std;
struct  p {
	int num;
	int time;
};
bool cmp(p m1, p m2) {
	return m1.time < m2.time;

}
int main() {
	
	p sta[1005];
	int sum;
	cin >> sum;
	for (int i = 1; i<=sum; i++) {
		int time;
		cin >> time;
		sta[i].num = i;
		sta[i].time = time;
	}
//对结构体排序
	sort(sta+1, sta+sum+1,cmp);
	double sum_time=0;
	for (int i = 1; i <= sum; i++) {
		cout << sta[i].num<<" ";
//每个人要等待的时间累计和
		sum_time += sta[i].time*(sum-i);
	}
	cout << endl;
	printf("%.2f", sum_time /sum);
	
	return 0;
}

五、
题目:

 代码:

#include<bits/stdc++.h>
using namespace std;
int main() {
	string a,b;
	int count = 0;
	cin >> a;
	cin >> b;
		
	for (int i = 1; i < b.length(); i++) {
		if ((b[i] != a[i] && b[i - 1] != a[i - 1])||(b[i - 1] != a[i - 1])) {
			b[i - 1]=b[i - 1] == '*' ? 'o' : '*';
			b[i]=b[i] == '*' ? 'o' : '*';
			count++;
		}
		else if (b[i] != a[i] && b[i + 1] != a[i + 1]) {
			b[i]=b[i] == '*' ? 'o' : '*';
			b[i + 1]=b[i + 1] == '*' ? 'o' : '*';
			count++;
		}
		else if (b[i] != a[i]) {
			b[i]=b[i] == '*' ? 'o' : '*';
			b[i + 1]=b[i + 1] == '*' ? 'o' : '*';
			count++;
		}

	}
	cout << count;
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Aure_xl

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值