Codeforces 849 C From Y to Y 思维

  题目链接: http://codeforces.com/contest/849/problem/C

  题目描述: 给你个一个cost, 让你构造花费为cost的字符串, 每次的 cost = 两个元素公共元素个数乘积

  解题思路: 很明显发现仅仅一个字母可以构造1, 3, 6, 10......两个不同的字符不会得到cost, 所以我们就先构造一种字母, 不行的用下一个字母填充就可以了......

  代码: 

#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iterator>
#include <cmath>
#include <algorithm>
#include <stack>
#include <deque>
#include <map>
#include <set>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define sca(x) scanf("%d",&x)
#define de printf("=======\n")
typedef long long ll;
using namespace std;

int main() {
    int n;
    cin.sync_with_stdio(false);
    string str;
    while( cin >> n ) {
        char c = 'a';
        int num;
        str = "";
        if( n == 0 ) {
            cout << "a" << endl;
            continue;
        }
        while( n ) {
            num = 0;
            while( num <= n ) {
                n -= num;
                str += c;
                num++;
            }
            c++;
        }
        cout << str << endl;
    }
    return 0;
}
View Code

  思考: 我一直不知道为什么, 看起来很显眼的东西我却总是想不到......我打算以后接触一下思维题, 心态炸了......但是还是要控制一下

posted on 2017-09-03 09:05  FriskyPuppy  阅读(151)  评论(0编辑  收藏  举报

导航