1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

多段階選抜(C++)

1
Last updated at Posted at 2014-08-04

問題は以下URL
http://nabetani.sakura.ne.jp/hena/ord24eliseq/

ロジックは以下とほぼ同じですが、平方根、立方根を求めるロジックのみを変えています。
http://qiita.com/ryosy383/items/00f8a35eee0a1ba9b296

# include <stdio.h>
# include <stdlib.h>
# include <iostream>
# include <cmath>
# include <vector>
# include <algorithm>

using namespace std;

# define MAX 10000

int main() {
    vector<int> sq;
    vector<int> cb;
    string input;

    for (int i = 1; i <= sqrt(MAX)+1; i++) {
        sq.push_back(i*i);
        cb.push_back(i*i*i);
    }

    while (1) {
        cin >> input;
		if (cin.fail())
			return 0;
        vector<int> ret, tmp;
        for (int i = 1; i <= MAX; i++) {
            ret.push_back(i);
        }
        for (int i = 0; i < input.length(); i++) {
            int count = 0, n,j,k;
            string s;
            copy(ret.begin(), ret.end(), back_inserter(tmp));


            switch (input[i]) {
            case 'S': {
                for (j = 0; j < ret.size(); j++) {
                    for (k = 0; k < sq.size(); k++) {
                        if (ret[j] == sq[k] && (count + 1) < tmp.size()) {
                            tmp[count + 1] = -1;
                        }
                    }
                    count++;

                }
                ret.clear();

                for (j = 0; j < tmp.size(); j++) {
                    if (tmp[j] != -1) {
                        ret.push_back(tmp[j]);
                    }
                }
                tmp.clear();
                break;
            }
            case 's': {
                for (j = 0; j < ret.size(); j++) {
                    for (k = 0; k < sq.size(); k++) {
                        if (ret[j] == sq[k] && (count - 1) >= 0) {
                            tmp[count - 1] = -1;
                        }
                    }
                    count++;

                }
                ret.clear();

                for (j = 0; j < tmp.size(); j++) {
                    if (tmp[j] != -1) {
                        ret.push_back(tmp[j]);
                    }
                }
                tmp.clear();
                break;
            }
            case 'C': {
                for (j = 0; j < ret.size(); j++) {
                    for (k = 0; k < cb.size(); k++) {
                        if (ret[j] == cb[k] && (count + 1) < tmp.size()) {
                            tmp[count + 1] = -1;
                        }
                    }
                    count++;

                }
                ret.clear();

                for (j = 0; j < tmp.size(); j++) {
                    if (tmp[j] != -1) {
                        ret.push_back(tmp[j]);
                    }
                }
                tmp.clear();
                break;
            }
            case 'c': {
                for (j = 0; j < ret.size(); j++) {
                    for (k = 0; k < cb.size(); k++) {
                        if (ret[j] == cb[k] && (count - 1) >= 0) {
                            tmp[count - 1] = -1;
                        }
                    }
                    count++;

                }
                ret.clear();

                for (j = 0; j < tmp.size(); j++) {
                    if (tmp[j] != -1) {
                        ret.push_back(tmp[j]);
                    }
                }

                tmp.clear();
                break;
            }
            case 'h': {
                ret.clear();
                for (j = 100; j < tmp.size(); j++) {
                    ret.push_back(tmp[j]);
                }
                tmp.clear();
                break;
            }
            default:
                ret.clear();
                s = input[i];
                n = atoi(s.c_str());
                for(j = 0; j < tmp.size(); j++) {
                    if (((j+1) % n) != 0) {
                        ret.push_back(tmp[j]);
                    }
                }
                tmp.clear();
                break;

            }
        }
        for (int x = 0; x < 10; x++) {
            printf("%d", ret[x]);
            if (x != 9) {
                printf(",");
            }
        }
        printf("\n");

    }
    return 0;

}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?