算法2.1:模拟

P1003 [NOIP2011 提高组] 铺地毯

由题意,找出覆盖所需点的最上面的那张毯子,类似栈的先进后出,所以利用栈的思想,从上到下(也就是数组从后往前遍历)找到满足的一组答案记录即可。

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

int n,x,y;
struct node{
    int x,y,a,b;
}mp[10010];

void solve(){
    cin >> n;
    for(int i = 1;  i<= n ; i ++){
        cin >> mp[n - i + 1].x>>mp[n - i + 1].y>>mp[n - i + 1].a>>mp[n - i + 1].b;
    }
    cin >> x >> y;
    for(int i = 1; i <= n ; i ++){
        if(mp[i].x + mp[i].a >= x && mp[i].y + mp[i].b >= y && mp[i].x <= x && mp[i].y <= y){
            cout << n - i + 1<<'\n';
            return;
        }
    }
    cout <<"-1\n";

}

int main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int _= 1;//cin >> _;
    while(_--)solve();
    return 0;
}

P1067 [NOIP2009 普及组] 多项式输出

根据题意模拟即可,注意n = 0时输出单个数字不需要带正号,再特判一下第一项正数无需加正号。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e3 + 10;

int n, a;

void solve(){
    cin >> n;
    if(!n){
        cin >> a;
        cout << a;
        return;
    }
    for(int i = n ; i >= 0 ; i--){
        cin >> a;
        if(a == 0)continue;
        if(i == 0){
            if(a > 0)cout <<"+";
            cout << a;
            continue;
        }
        if(i == n && i != 1){
            if(a == 1)cout << "x^" <<i;
            else if(a == -1)cout << "-x^" << i;
            else cout << a << "x^" <<i;
            continue;
        }
        if(i == 1){
            if(a == 1)cout << "+x";
            else if(a == -1)cout << "-x";
            else {
                if(a > 0)cout <<"+";
                cout << a << "x";
            }
            continue;
        }
        if(a > 0)cout << "+";
        if(a == 1)cout << "x^" <<i;
        else if(a == -1)cout << "-x^" << i;
        else cout << a << "x^" <<i;
    }
}

int main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int _= 1;//cin >> _;
    while(_--)solve();
    return 0;
}

P1328 [NOIP2014 提高组] 生活大爆炸版石头剪刀布

根据表格模拟。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 210;

int a[N], b[N], Ascore, Bscore,n,s1,s2;

bool cmp(int a,int b){
    if(a == b) return 0;
    if(a == 0 && b == 1) return 0;
    if(a == 0 && b == 2) return 1;
    if(a == 0 && b == 3) return 1;
    if(a == 0 && b == 4) return 0;
    if(a == 1 && b == 0) return 1;
    if(a == 1 && b == 2) return 0;
    if(a == 1 && b == 3) return 1;
    if(a == 1 && b == 4) return 0;
    if(a == 2 && b == 0) return 0;
    if(a == 2 && b == 1) return 1;
    if(a == 2 && b == 3) return 0;
    if(a == 2 && b == 4) return 1;
    if(a == 3 && b == 0) return 0;
    if(a == 3 && b == 1) return 0;
    if(a == 3 && b == 2) return 1;
    if(a == 3 && b == 4) return 1;
    if(a == 4 && b == 0) return 1;
    if(a == 4 && b == 1) return 1;
    if(a == 4 && b == 2) return 0;
    if(a == 4 && b == 3) return 0;
    return 0;
}

void solve(){
    cin >> n >> s1 >> s2;
    for(int i = 1; i <= s1; i++)cin >> a[i];
    for(int i = 1; i <= s2; i++)cin >> b[i];
    int i = 1, j = 1,cnt = 0;
    while(cnt != n){
        Ascore += cmp(a[i],b[j]);
        Bscore += cmp(b[j],a[i]);
        i++,j++,cnt++;
        if(i > s1)i = 1;
        if(j > s2)j = 1;
    }
    cout << Ascore <<" " << Bscore;

}

int main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int _= 1;//cin >> _;
    while(_--)solve();
    return 0;
}

P1563 [NOIP2016 提高组] 玩具谜题

向内相当于左右不变,向外相当于左右互换,共有2^2种情况,分情况模拟。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;

ll n, m,lr,num,idx;

struct node{
    ll face;
    string na;
}a[N];

void roll(ll lr,ll num){
    if(lr == 0){
        idx = (idx + n * N - num) % n;
    }
    if(lr == 1){
        idx = (idx + num) % n;
    }
}

void solve(){
    cin >> n >> m;
    for(int i = 0 ; i < n ; i ++)cin >> a[i].face >> a[i].na;
    for(int i = 1 ; i <= m ; i ++){
        cin >> lr >> num;
        if(lr == 0 && a[idx].face){
             roll(1,num);
        }
        else if(lr == 0 && !a[idx].face){
            roll(0,num);
        }
        else if(lr == 1 && a[idx].face){
            roll(0, num);
        }
        else {
            roll(1,num);
        }
    }
    cout << a[idx].na;
}

int main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int _= 1;//cin >> _;
    while(_--)solve();
    return 0;
}

P1042 [NOIP2003 普及组] 乒乓球

根据题意模拟即可。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e7 + 10;

char s[N];
int cnt;

void score21(){
    int l = 0,r = 0;
    for(int i = 1 ; i <= cnt ; i ++){
        if(s[i] == 'W')l++;
        if(s[i] == 'L')r++;
        if((l >= 21 || r >= 21) && abs(l - r) >= 2){
            cout << l <<":"<<r<<'\n';
            l = 0,r = 0;
        }
        if(s[i] == 'E'){
            cout << l <<":"<<r<<'\n';
            return;
        }
    }
}

void score11(){
    int l = 0,r = 0;
    for(int i = 1 ; i <= cnt ; i ++){
        if(s[i] == 'W')l++;
        if(s[i] == 'L')r++;
        if((l >= 11 || r >= 11) && abs(l - r) >= 2){
            cout << l <<":"<<r<<'\n';
            l = 0,r = 0;
        }
        if(s[i] == 'E'){
            cout << l <<":"<<r<<'\n';
            return;
        }
    }
}

void solve(){
    while(1){
        cnt++;
        cin >> s[cnt];
        if(s[cnt] == 'E')break;
    }
    score11();
    cout <<"\n";
    score21();
}

int main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int _= 1;//cin >> _;
    while(_--)solve();
    return 0;
}

P1179 [NOIP2010 普及组] 数字统计

拆分出每一位看是否有2,数据量很小所以不会超时。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10;

int l, r,ans;

void cnt2(int x){
    while(x != 0){
        if(x % 10 == 2)ans ++;
        x /= 10;
    }
}

void solve(){
    cin >> l >> r;
    for(int i = l ; i <= r;i++){
          cnt2(i);
    }
    cout << ans;
}

int main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int _= 1;//cin >> _;
    while(_--)solve();
    return 0;
}

P2615 [NOIP2015 提高组] 神奇的幻方

根据题意模拟即可。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 40;

int n, a[N][N];

void solve(){
    cin >> n;
    a[1][n + 1 >> 1] = 1;
    int x = 1,y = n + 1 >> 1;
    for(int i = 2 ; i <= n * n; i ++){
        if(x == 1 && y != n){
            x = n,y = y + 1;
            a[x][y] = i;
        }
        else if(y == n && x != 1){
            y = 1,x = x - 1;
            a[x][y] = i;
        }
        else if(y == n && x == 1){
            x = x + 1;
            a[x][y] = i;
        }
        else if(x != 1 && y != n){
            if(!a[x - 1][y + 1]){
                a[x - 1][y + 1] = i;
                x = x - 1,y = y + 1;
            }
            else{
                a[x + 1][y] = i;
                x = x + 1;
            }
        }
    }
    for(int i = 1; i <= n ; i ++){
        for(int j = 1 ; j <= n ; j ++){
            cout << a[i][j] <<' ';
        }
        cout <<'\n';
    }
    
}

int main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int _= 1;//cin >> _;
    while(_--)solve();
    return 0;
}

P3952 [NOIP2017 提高组] 时间复杂度

多个细节需要注意:

1)循环的判定(无法进入循环,常数循环,非常数循环)。

2)错误的判定,变量重复使用,结束少了。

由于先进行的循环后结束,考虑用栈模拟循环的过程。

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

void solve(){
    int level[50],line;
    string O,begin,end,s;
    char op,iter;
    int o = 0,order = 0,maxorder = 0,top = 0, top2 = 0, iserror = 0, nocycle = 0;
    memset(level , 0, sizeof level);
    cin >> line;
    cin >> O;
    if(O[3] == ')') o = 0;
    else if(O[3] == '^'){
        if(O[5] == ')')o = (O[4] - '0');
        else if(O[6] == ')') o = (O[4] - '0') * 10 + (O[5] - '0');
    }
    for(int i = 0; i < line;i++){
        cin >> op;
        if(op == 'F'){
            cin >> iter;
            for(int j = 0; j < top;j++){
                if(s[j] == iter) {iserror = 1;break;}
            }
            if(iserror == 0){s[top] = iter;top++;}
            cin >> begin;
            cin >> end;
            if(begin == "n" && end == "n")level[top2] = 0;
            else if(begin == "n"){level[top2] = -1,nocycle++;}
            else if(end == "n"){level[top2] = 1;}
            else if(begin.length() > end.length() || (begin.length() == end.length() && begin > end)){level[top2] = -1;nocycle++;}
            else level[top2] = 0;
            if(level[top2] == 1 && nocycle == 0){
                order ++;
                if(order > maxorder)maxorder = order;
            }
            top2++;
        }
        if(op == 'E'){
            if(top <= 0)iserror = 1;
            else {
                if(level[top2 - 1] == 1 && nocycle == 0)order --;
                else if(level[top2 - 1] == -1)nocycle--;
                top--,top2--;
            }
        }
    }
    if(top != 0)iserror = 1;
    if(iserror)cout << "ERR" <<'\n';
    else {
        if(o == maxorder)cout <<"Yes\n";
        else cout <<"No\n";
    }
}

int main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int _= 1;cin >> _;
    while(_--)solve();
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值