Codeforces 946a,b,c

  • 2018-03-14
  • 1,239
  • 0
  • 0

· Codeforces 946a

Origin:http://codeforces.com/problemset/problem/946/A

题意:正数的和减去负数的和

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

int main()
{
    int n, ans = 0, t;
    scanf("%d", &n);
    for(int i = 0; i < n; i++)
    {
        scanf("%d", &t);
        ans += abs(t);
    }
    printf("%d\n", ans);

    return 0;
}

· Codeforces 946b

Origin:http://codeforces.com/problemset/problem/946/B

题意:给出a, b,按如下步骤操作

  1. If a = 0 or b = 0, end the process. Otherwise, go to step 2;
  2. If a ≥ 2·b, then set the value of a to a - 2·b, and repeat step 1. Otherwise, go to step 3;
  3. If b ≥ 2·a, then set the value of b to b - 2·a, and repeat step 1. Otherwise, end the process.

最后输出a和b的结果

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

int main()
{
    long long a, b, t;
    scanf("%lld%lld", &a, &b);
    while(1)
    {
        if (a == 0 || b == 0 || a < 2 * b && b < 2 * a) break;
        a = a % (2 * b);
        if (a == 0 || b < 2 * a) break;
        b = b % (2 * a);
    }
    printf("%lld %lld\n", a, b);

    return 0;
}

· Codeforces 946c

Origin:http://codeforces.com/problemset/problem/946/C

题意:给出一个字符串。每次变换中,字符串中每个字符都可以变为它的下一个字符(比如a变为b,m变为n),也可以不变。不限变换次数。要求在最后的字符串中,”abcdefg…wxyz”是它的子串,问能否做到。若能做到输出最后的字符串,若不能,输出”-1″。

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

int main()
{
    char s[100005];
    scanf("%s", s);
    int len = strlen(s), pos = 0;
    char ch = 'a';
    for (int i = 0; i < len; i++) { if (ch == 'z' + 1) break; if (s[i] > ch) continue;
        s[i] = ch++;
    }
    if (ch == 'z' + 1) printf("%s\n", s);
    else printf("-1\n");

    return 0;
}

 

 

评论

还没有任何评论,你来说两句吧

发表评论

冀ICP备19026961号