zoukankan      html  css  js  c++  java
  • GCJ Qualification Round 2016 B题

    经典的翻饼问题,直接做:从下往上看,已翻好的饼忽略掉;从上往下,连续的已翻好的一起翻过来;整个翻过来。

    /*
     * Author    : ben
     */
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <set>
    #include <map>
    #include <stack>
    #include <string>
    #include <vector>
    #include <deque>
    #include <list>
    #include <functional>
    #include <numeric>
    #include <cctype>
    using namespace std;
    typedef long long LL;
    char pancakes[110];
    bool pan[110];
    int pannum;
    
    void inline flippan(int n) {
        bool *first = pan;
        bool *last = first + n;
        while ((first != last) && (first != --last)) {
            bool tmp = !(*last);
            *last = !(*first);
            *first = tmp;
            ++first;
        }
        if (n % 2 == 1) {
            pan[n / 2] = !pan[n / 2];
        }
    }
    
    int work() {
        int ans = 0;
        while (pannum > 0) {
            while (pannum > 0 && pan[pannum - 1]) {
                pannum--;
            }
            if (pannum <= 0) {
                break;
            }
            int I = 0;
            while (pan[I]) {
                I++;
            }
            if (I > 0) {
                flippan(I);
                ans++;
            }
            flippan(pannum);
            ans++;
        }
        return ans;
    }
    
    int main() {
    //    freopen("b.small.in", "r", stdin);
        int T;
        scanf("%d", &T);
        for (int t = 1; t <= T; t++) {
            scanf(" %s", pancakes);
            pannum = strlen(pancakes);
            for (int i = 0; i < pannum; i++) {
                if (pancakes[i] == '-') {
                    pan[i] = 0;
                } else {
                    pan[i] = 1;
                }
            }
            printf("Case #%d: %d
    ", t, work());
        }
        return 0;
    }
  • 相关阅读:
    sql面试题
    C#基础(1)
    Java中的冒泡排序(减少比较次数)
    Java中面向对象的分拣存储
    Java中分拣存储的demo
    XML序列化
    C#读取csv文件使用字符串拼接成XML
    Java中HashMap(泛型嵌套)的遍历
    Java 中List 集合索引遍历与迭代器遍历
    java 中的try catch在文件相关操作的使用
  • 原文地址:https://www.cnblogs.com/moonbay/p/5371177.html
Copyright © 2011-2022 走看看