Find(旧键盘打字)、结构体sort(名人堂)、queue(银行业务)、最简分数

STL

1、find用法

1033 旧键盘打字

#include <iostream>
#include <cctype>
using namespace std;
int main() {
  string bad, should;
  getline(cin, bad);
  getline(cin, should);
  for (int i = 0, length = should.length(); i < length; i++) {
    if (bad.find(toupper(should[i])) != string::npos) continue;
    //在bad中未找到should[i]则继续
    //string::nops 可用-1代替
    if (isupper(should[i]) && bad.find('+') != string::npos) continue;
    //大写should[i] 且 在bad中未找到['+']则继续
    cout << should[i];
  }
  return 0;
}

2、结构体sort排序

7-7 名人堂与代金券
*同分同排名借用c计算排名
*结构体数组

#include<bits/stdc++.h>
using namespace std;

struct student{
	string x;
	int y;
};

bool cmp(student a,student b)
{
	if(a.y==b.y)
	{
		return a.x<b.x;;
	}
	else return a.y>b.y;
}

int main()
{
	int n,g,k,sum=0;;
	cin>>n>>g>>k;
	vector<student> v;
    student stu;
	for(int i=0;i<n;i++)
	{
		cin>>stu.x>>stu.y;
        v.push_back(stu);
		if(stu.y>=g) sum+=50;
		else if(stu.y>=60) sum+=20;
	}
	sort(v.begin(),v.end(),cmp);
    cout<<sum<<endl;
	int c = 1;
	for(int i=0; ; i++)
	{
		if(i==0 || v[i].y==v[i-1].y)
			cout << c << " " << v[i].x << " " << v[i].y << endl;
		else 
		{
			c=i+1;
			if(c>k) break;
			cout << c << " " << v[i].x << " " << v[i].y << endl;
		}
	}
}

stack 先进后出

题目:7-3 说反话-加强版

	stack<string> s;//定义: stack<数据类型> 变量名
    string str;    
	while(cin>>str)
	{
		s.push(str);
		//进入stack:变量名.push(数据)
	}
	//输出:
	//数据间用空格隔开且最后一个数据后没有空格
    if(!s.empty())//stack非空
    {
        cout << s.top();
        s.pop();
    }
	while(!s.empty())
	{
		cout << " " << s.top();
		s.pop();
	}

queue 先进先出

题目:7-2 银行业务队列简单模拟


	queue<int> a, b;//定义: queue<数据类型> 变量名
	vector<int> v;
	int i, x;
	for(i=0; i<n; i++)
	{
		cin >> x;
		if(x%2==0) b.push(x);
		//进入queue : 变量名.push(数据)
		else a.push(x); 
	}
	while(!a.empty()||!b.empty())//queue非空
	{
		if(!a.empty())
		{
			v.push_back(a.front());
			a.pop();
		}
		if(!a.empty())
		{
			v.push_back(a.front());
			a.pop();
		}
		if(!b.empty())
		{
			v.push_back(b.front());
			b.pop();
		}
	}
	cout << *v.begin();
	//*v.begin()是指v.begin()指针指向的地址里的数据
	for(auto i=v.begin()+1; i<v.end(); i++)
		cout << " " << *i;
		//i为指针 *i输出i指针指向地址里的数据

1062 最简分数
*注意输出格式

#include <iostream>
using namespace std;
int gcd(int a, int b)
{
    return a % b == 0? b : gcd(b, a % b); //最大公因数b
}
int main() {
    int n1, m1, n2, m2, k;
    scanf("%d/%d %d/%d %d", &n1, &m1, &n2, &m2, &k);
    if(n1 * m2 > n2 * m1) //已知是正分数
    {
        swap(n1, n2);
        swap(m1, m2);
    }
    //保证 (n1/m1) < (n2/m2)
    //由于(n1/m1)可能为小数,不便于对比,所以用(n1 * m2 > n2 * m1)来对比
    int num = 1;
    bool flag = false;
    while(n1 * k >= m1 * num) num++;//(n1/m1)>=(num/k)
    while(n1 * k < m1 * num && m2 * num < n2 * k)
    //保证(num/k)在(n1/m1)和(n2/m2)之间
    {
        if(gcd(num, k)==1) //num与k最大公因数为1则分数为最简状态
        {
            printf("%s%d/%d", flag == true ? " ":"", num, k);
            flag = true;
        }
        num++;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值