学习c++(1月21日)

总结

1.时空复杂度的计算

一般忽略常数,如26n就只计n。

2.常用函数

重点

(1)sort快速排序

(2)二分查找:

        lower_bound:找第一个大于等于目标值的元素的位置。

        upper_bound:找第一个大于目标值的元素的位置。

(3)pair的使用

(4)vector的使用

(5)string的使用(特别在数据很大时可以用字符串)

(6)map的使用

3.auto

(1)编译器自动推导变量类型

eg:auto a=14   auto b='a'

   (2)范围 for 循环

eg:  for(auto ai : a)==for(int i = 0 ; i < 5 ; i++)

代码

1

#include<iostream>
using namespace std;
int main()
{
    int n=0;
    cin >> n;
    cout << "L";
    for (int i = 0; i < n; i++)
    {
        cout << "o";
    }
    cout << "ng";
    system("pause");
    return 0;
}

思路:“l”和“ng“拆开,有几个数打几个”o“

2

#include<iostream>
#include<cctype>
using namespace std;

int main() 
{
    int n;
    cin >> n;
    const char s1[] = { 'y', 'e', 's' };
    for (int i = 0; i < n; i++) 
    {
        char s[4] = { 0 };
        cin >> s;
        bool match = true; 
        for (int j = 0; j < 3; j++)
        {
            char lower_s = tolower(s[j]);
            if (lower_s != s1[j]) 
            {
                match = false;
                break;
            }
        }
        if (match)
        {
            cout << "YES\n";
        }
        else 
        {
            cout << "NO\n";
        }
    }
    return 0;
}

思路:先存一个标准模板”yes“,再把输入的字符串全变成小写,并与标准模板一一对应,都符合就”YES“

3

#include<iostream>
#include<string>
using namespace std;
int main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		string s;
		cin >> s;
		int a{s.back()-'0'};
		if (a % 2 == 0)
		{
			cout << "even\n";
		}
		else
		{
			cout << "odd\n";
		}
	}
	return 0;
}

思路:一开始想用long long,但是不行,改用字符串,并判断最后一位是不是偶数

4

#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
    int sum;
    cin >> sum;
    for (int i = 0; i < sum; i++)
    {
        int n, m;
        cin >> n >> m;
        string s;
        cin >> s;
        vector<int>s1(10);
        for (int j = 0; j < n; j++)
        {
            s1[s[j] - 'A']++;
        }
        int b=0;
        for (int k = 0; k < 7; k++)
        {
            b += max(0, m - s1[k]);
        }
        cout << b<<"\n";
    
    }
    return 0;
}

思路:统计已有题目,用总需求去减已满足的,剩下的就是需求量

5

#include<iostream>
using namespace std;
int main()
{
    int n, m, k;
    cin >> n >> m >> k;
    int c = 0;
    for (int i = 0; i < m; i++)
    {
        int count1 = 0;
        for (int j = 0; j < n; j++)
        {
            int a;
            cin >> a;
            if (a == k)
            {
                count1++;
            }
        }
        if (n%2==0)
        {
            if (count1 >= n / 2)
            {
                c++;
            }
        }
        else
        {
            if (count1 > n / 2)
            {
                c++;
            }
        }
    }
    if (m % 2 == 0)
    {
        if (c >= m / 2)
        {
            cout << "YES";
        }
        else
        {
            cout << "NO";
        }
    }
    else
    {
        if (c > m / 2)
        {
            cout<<"YES";
        }
        else
        {
            cout << "NO";
        }
    }
    return 0;
}

思路:直接看k是否满足,其余条件忽略,但注意m的单双数,不要直接判断m/2

6

#include<iostream>
#include<string>
#include<vector>
#include<utility>
using namespace std;
int main()
{
    int n;
    cin >> n;
    string s;
    cin >> s;
    int m;
    cin >> m;
    vector<pair<char, char>>s1(m);
    for (int i = 0; i < m; i++)
    {
        char from, to;
        cin >> from >> to;
        s1[i] = make_pair(from, to);
    }
    char o;
    string s3(26,' ');
    for (int i = 'a'; i <= 'z'; i++)
    {
        o = i;
        for (int j = 0; j < m; j++)
        {
            if (o == s1[j].first)
            {
                o = s1[j].second;
            }
        }
        s3[i - 'a'] = o;
    }
    for (int i = 0; i < n; i++)
    {
        cout << s3[s[i] - 'a'];
    }
    return 0;
}

思路:先把26个字母变成最后的结果,再通过s[i] - 'a'确定原来字母的位置,输出这个位置上的最终结果

7

#include<iostream>
#include<string>
#include<utility>
using namespace std;
int main()
{
    int n, m;
    cin >> n >> m;
    int s[5000][5000] = { 0 };
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            cin >> s[i][j];
        }
    }
    int hang[10000],lie[10000];
    for (int i = 1; i <= n; i++)
    {
        hang[i]=lie[i]=i;
    }
    for (int i = 0; i < m; i++)
    {
        int x , a , b;
        cin >> x >> a >> b;
        if (x)
        {
            swap(hang[a], hang[b]);
        }
        else
        {
            swap(lie[a], lie[b]);
        }
    }
    
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            cout << s[hang[i]][lie[j]]<<" ";
        }
        cout<<endl;
    }
    return 0;
}

思路:利用类似九宫格的行列映射关系,通过改变数字位置来得到结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值