栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

模拟、枚举、贪心(一)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

模拟、枚举、贪心(一)

A-[NOIP2007]字符串的展开

简单模拟即可 

#include
#define ll long long
using namespace std;
int p1,p2,p3;
string s;
vector ans;

void rep(char l,char r){
    ans.push_back(l);
    if(r<=l){ans.push_back('-');}
    else if(r==l+1){}
    else {//l-r
        if(p1==1 && p3==1){
            for(char i=l+1;i<=r-1;i++)
                for(int k=0;k=l+1;i--)
                for(int k=0;k=l+1;i--)
                for(int k=0;k>p1>>p2>>p3>>s;
    int len=s.size();
    for(int i=0;i 

B-[NOIP2017]时间复杂度
2

 C-[NOIP2010]机器翻译

注意这里cnt不但可以记录个数,模m后表示字典中下一个要被删除的单词 

#include
using namespace std;
int dict[110],pas[1010];
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int m,n,cnt=0;
    cin>>m>>n;
    for(int i=0;i>pas[i];
    for(int i=0;i 

D-四舍五入 
4

 E-安卓图案解锁

1.判断是否重复出现

2.判断是否跳过数字(将横竖斜都判断一遍)

3.将一些边的判断合并,如经过5的,然后(9,1)(3,7)配对判断

ps:详细看代码,结合图理解

#include
using namespace std;
int main(){
    string s1;
    while(cin>>s1){
        vector dict,s;
        bool check=1;
        for(int i=0;i 

F- 从后台研发到跑路
2

 G-回文数

1.注意前导0(00也算,0不算)(要出现前导0,有且仅有2种数字出现,并且非0的数字只有1个)

2.数量为奇数个的数不超过1个

#include
using namespace std;
int a[10];
bool check1=1,check2=0,check=1;
vector ans;
int main(){
    for(int i=0;i<10;i++)cin>>a[i];

    int cnt=0;//记录奇数个的数的个数
    for(int i=0;i<10;i++){if(a[i]&1) cnt++;}
    if(cnt>1)check=false;

    if(a[0]>=2){
        int sum=0;
        for(int i=0;i<10;i++){sum+=a[i];}
        if(a[0]+1==sum || a[0]==sum){
            printf("-1");
            return 0;
        }
    }
    if(check){
        int ji=-1;
        for(int i=0;i<10;i++)if(a[i]&1){ji=i;break;}
        for(int i=1;i<10;i++){
            if(a[i]>=2){
                ans.push_back(i);
                a[i]-=2;
                break;
            }
        }
        for(int i=0;i<10;i++){
            while(a[i]>=2)ans.push_back(i),a[i]-=2;
        }
        for(int i=0;i=0;i--)cout<

 H-回文数
1

 I-[NOIP2016]玩具谜题
1

 J-[NOIP2015]神奇的幻方

 按题目所给条件模拟即可

#include
using namespace std;
int n;
int a[40][40];
pair pos;//记录上一个点的位置
int main(){
    cin>>n;
    a[1][(1+n)/2]=1;
    pos={1,(1+n)/2};
    for(int i=2;i<=n*n;i++){
        if(pos.first==1 && pos.second!=n){
            a[n][pos.second+1]=i;
            pos={n,pos.second+1};
        }
        else if(pos.second==n && pos.first!=1){
            a[pos.first-1][1]=i;
            pos={pos.first-1,1};
        }
        else if(pos.first==1 && pos.second==n){
            a[pos.first+1][pos.second]=i;
            pos={pos.first+1,pos.second};
        }
        else {
            if(a[pos.first-1][pos.second+1]==0){
                a[pos.first-1][pos.second+1]=i;
                pos={pos.first-1,pos.second+1};
            }
            else {
                a[pos.first+1][pos.second]=i;
                pos={pos.first+1,pos.second};
            }
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cout<

K-Tic-tac-toe
5

L-I love you 

dp比较难想到,想到之后挺简单的

#include
using namespace std;
char ch;
int dp[8];//表示前i个字符在字符串中出现了dp[i]次
const int p=20010905;
int main(){
    string s;
    cin>>s;
    for(int i=0;i 

M-[NOIP2016]回文日期 

分解题目:

1.回文函数                2.找到下一个日期                3.闰年判断(当然可以和2合并) 

#include
using namespace std;
const int run_mon[]={0,31,29,31,30,31,30,31,31,30,31,30,31};
const int mon[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
inline bool run(int year){
    if(year%4==0 &&(year%100!=0  || year%400==0)) return true;
    return false;
}

inline bool huiwen(int date){
    vector s;
    while(date>0){
        s.push_back(date%10);
        date/=10;
    }
    for(int i=0;i<=(int)s.size()/2;i++){
        if(s[i]!=s[s.size()-1-i])return false;
    }
    return true;
}

inline int next_date(int date){
    int year,month,day;
    year=date/10000;
    month=date/100%100;
    day=date%100;
    if(run(year)){
        day++;
        if(day>run_mon[month]){day=1; month++;};
        if(month>12){month=1,year++;};
    }
    else {
        day++;
        if(day>mon[month]){day=1; month++;};
        if(month>12){month=1,year++;};
    }
    return year*10000+month*100+day;
}

int main(){
    int l,r,cnt=0;
    cin>>l>>r;
    if(huiwen(l))cnt++;
    while(next_date(l)<=r){
        l=next_date(l);
        if(huiwen(l))cnt++;
    }
    cout< 

 N-校门外的树

签到

#include
using namespace std;
bool tree[10010];
int cnt,l,r,L,M;
int main(){
    cin>>L>>M;
    for(int i=0;i>l>>r;
        for(int j=l;j<=r;j++)tree[j]=1;
    }
    for(int i=0;i<=L;i++){
        if(tree[i]==0)cnt++;
    }
    cout< 

O-值周

首先考虑算法可行性:考虑n的规模,时间复杂度要小于nlgn(可以用sort了) 

思路:排序后合并区间(见下图)

两个区间有交集的时候,合并成大区间;不然则开辟新区间,并将上一个区间的人赶走

#include
#define ll long long
using namespace std;
vector< pair > tree;
int cnt,L,M,l,r,len;
bool cmp(pair a,pair  b){
    return a.first<=b.first;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin>>L>>M;
    int x,y;
    for(int j=0;j>x>>y;
        tree.push_back({x,y});
    }
    sort(tree.begin(),tree.end(),cmp);
    l=tree[0].first,r=tree[0].second;
    for(int i=1;ir+1){
            len+=r-l+1;
//          cout< 

P-货物种类
1

Q-储物点的距离
2

R-糖糖别胡说,我真的不是签到题
5

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/832576.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号