学习笔记4.23-万能库函数和积分赛第二周题目(字符串分组问题)

1.

#include<bits/stdc++.h>

常见stl库函数:见链接(72条消息) STL 中的常见实用库函数(适合初学算法者阅读)(持续更新中)_stl库函数_H-rosy的博客-CSDN博客

2. 而老师用的

#include<stdafx.h>//这是自己写的,

会有一个stdfax.cpp来生成预编译头

但一般只用于vscode,不同编译器之间是不兼容的。

(72条消息) #include“stdafx.h”详解_"#include \"stdafx.h"_qingkong8832的博客-CSDN博客

3.积分赛K题,只用看第一个和最后一个是否相同就行了,因为有上就必有下

取string对象的长度,C语言中使用strlen()来获取字符串长度,C++中使用str.size()str.length().

string str("hello!");
int len1 = str.size();
int len2 = str.length();

读入了一个int n之后,要把后面的这个换行符“吃掉”,否则后续读入字符串会有问题。

#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
int main() {
    int n;
    cin>>n;
    cin.ignore();
    for(int i=1;i<=n;++i){    
    string s;
    getline(cin, s);
    int t=s.size();
    if(s.at(0)==s.at(t-1)){
        cout<<s<<endl;
    }
    else if(s.at(0)<s.at(t-1)){
        s[t-1]='a';
        cout<<s<<endl;
    }
    else {
        s[t-1]='b';
        cout<<s<<endl;
    }}
    return 0;
}
 

关于常用string的函数(72条消息) C++-string常用函数整理(建议收藏)_c++string类函数_翟天保Steven的博客-CSDN博客

3.最大分组问题:

若运行框抛出terminate called after throwing an instance of 'std::out_of_range'   what():  basic_string::insert: 

多半是容器访问越界

#include<iostream>
#include<cstring>
using namespace std;
int main(){
    int t;
    int n,r,b;//r为A队赢得次数,b为B队赢得,A强于B队
    cin>>t;
    for(int i=1;i<=t;++i){
        cin>>n>>r>>b;
        int xu=0;
        string a(r,'R');
        //分成了b+1组
        int yu=r%(b+1);
        int q=r/(b+1);
        if(yu==0){
            for(int j=1;j<=b;++j){
                xu+=q;
                a.insert(xu,1,'B');
                xu+=1;
            }}
        else{
            for(int j=1;j<=yu-1;++j){
                xu+=q+1;
                a.insert(xu,1,'B');
                xu+=1;
           }
            for(int j=1;j<=(b+1)-(yu-1)-1;++j){
                xu+=q;
                a.insert(xu,1,'B');
                xu+=1;
            }
        }
        cout<<a<<endl;
        }
    return 0;
    } 

是一个非常妙的数学思路:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值