Valley of Los Xinshoucun training - problem solution "BOSS War 2 Getting Started Exercises"

P1426 fish danger it

Link Title: https://www.luogu.com.cn/problem/P1426
Title effect:
once the fish along a line from A to the right travel, a first second fish can swim 7 m seconds from the second only 98% of the second travel distance before the start of one second. There are an extremely wicked hunter at a distance A at the right s m, the installation of a concealed detector, the detector around the range of x meters is to detect. Once the fish enter the range of the detector, the detector will be at the end of this second transfer signal to the hunter, the hunter must probe for water range arrest after one second, then if the fish further within this range on the dangerous. That is, once the fish into the detector range, if 1 second immediately next swim time range of the detector, is safe. Now given data and x s, please can you tell the fish will not be dangerous? If the output danger 'y', there is no risk of the output 'n'.
Outline of Solution:
moving away until the cycle has been displaced \ (\ GE \) to Sx, one second and then moves, determines whether \ (\ gt \) S + X can.
Codes are as follows:

#include <bits/stdc++.h>
using namespace std;
double S, s, x, v = 7;
bool check() {
    while (s < S-x) {
        s += v;
        v *= 0.98;
    }
    s += v;
    return s > S+x;
}
int main() {
    cin >> S >> x;
    puts( check() ? "n" : "y" );
    return 0;
}

P1464 Function

Topic links: https://www.luogu.com.cn/problem/P1464
subject to the effect:
Seeking a recursive function.
Problem-solving ideas:
there are many overlapping sub-problems need to use memo (memory search).
Codes are as follows:

#include <bits/stdc++.h>
using namespace std;
bool vis[22][22][22];
long long f[22][22][22];
long long w(long long a, long long b, long long c) {
    if (a <= 0 || b <= 0 || c <= 0) return 1LL;
    if (a > 20 || b > 20 || c > 20) return w(20, 20, 20);
    if (vis[a][b][c]) return f[a][b][c];
    vis[a][b][c] = true;
    if (a < b && b < c) f[a][b][c] = w(a,b,c-1) + w(a,b-1,c-1) - w(a,b-1,c);
    else f[a][b][c] = w(a-1,b,c) + w(a-1,b-1,c) + w(a-1,b,c-1) - w(a-1,b-1,c-1);
    return f[a][b][c];
}
int main() {
    long long a, b, c;
    while (~scanf("%lld%lld%lld", &a, &b, &c) && !(a==-1 && b==-1 && c==-1)) {
        printf("w(%lld, %lld, %lld) = %lld\n", a, b, c, w(a, b, c));
    }
    return 0;
}

P1014 Cantor table

Topic links: https://www.luogu.com.cn/problem/P1014
subject to the effect:
to find the item N Cantor table.
Problem-solving ideas:
enumeration find.
Explore the law:
our current set of molecules is \ (A \) , the denominator is \ (b \) , then:

  • If \ (a + b \) is even:
    • If \ (A \ lt. 1 \) , then \ (a = a-1; b = b + 1 \)
    • Otherwise, \ (b = b + 1 \)
  • If \ (a + b \) is odd:
    • If \ (B \. 1 lt \) , then \ (a = a + 1; b = b-1 \)
    • Ina则, \ (A = A Tasu 1 \)

A start \ (A = B =. 1 \) , follow this law cycle \ (N-1 \) times.

Codes are as follows:

#include <bits/stdc++.h>
using namespace std;
int n, a = 1, b = 1;
int main() {
    cin >> n;
    while (-- n) {
        if ( (a+b) % 2 == 0 ) {
            if (a > 1) a --, b ++;
            else b ++;
        }
        else {
            if (b > 1) a ++, b --;
            else a ++;
        }
    }
    cout << a << "/" << b << endl;
    return 0;
}

P1022 Calculator improvement

Topic links: https://www.luogu.com.cn/problem/P1022
subject to the effect: solving a linear equation.
Outline of Solution:
This question is more complicated to handle the input.
I started so that the processing into the form of a plurality of character string "symbol" + "number" + "a / b", after which the normalized string format, then I use stringstream processed.
Then deal with some of the details have to deal with, a little nerve-racking, brain cells die a lot of crazy stuff.
Codes are as follows:

#include <bits/stdc++.h>
using namespace std;
string s;
char cc;
int main() {
    cin >> s;
    bool flag = false;

    for (int i = 0; i < s.length(); i ++) {
        if (isalpha(s[i])) {
            cc = s[i];
            s[i] = 'a';
        }
        if (s[i] == '=') { flag = true; }
        else if (flag && s[i] == '+') s[i] = '-';
        else if (flag && s[i] == '-') s[i] = '+';
    }
    for (int i = 0; i < s.length(); i ++) {
        if (s[i] == '=') {
            if (s[i+1] != '+' && s[i+1] != '-') s[i] = '-';
            else s = s.substr(0, i) + s.substr(i+1);
            break;
        }
    }
    if (s[0] == 'a') s = "1" + s;
    while (true) {
        int idx = -1;
        int len = s.length();
        for (int i = 1; i < len; i ++) {
            if (s[i] == 'a' && !isdigit(s[i-1])) {
                idx = i;
                break;
            }
        }
        if (idx == -1) break;
        else s = s.substr(0, idx) + "1" + s.substr(idx);
    }
    while (true) {
        int idx = -1;
        int len = s.length();
        for (int i = 0; i < len; i ++) {
            if (isdigit(s[i]) && (i==len-1 || s[i+1]=='+' || s[i+1]=='-' )) {
                idx = i;
                break;
            }
        }
        if (idx == -1) break;
        else s = s.substr(0, idx+1) + "b" + s.substr(idx+1);
    }
    stringstream ss(s);
    char c;
    int num, a = 0, b = 0;
    while (ss >> num >> c) {
        if (c == 'a') a += num;
        else b += num;
    }
    double res = - (double) b / (double) a;
    printf("%c=%.3lf\n", cc, res);
}

P1307 digit reversal

Topic links: https://www.luogu.com.cn/problem/P1307
subject to the effect: Signed Digital Flip.
Problem-solving ideas: first remove the symbol, and then flip the data, and finally if a start is negative then the plus sign.
Codes are as follows:

#include <bits/stdc++.h>
using namespace std;
bool flag = false;
int a, b;
int main() {
    cin >> a;
    if (a < 0) { flag = true; a = -a; }
    while (a) {
        b = b * 10 + a % 10;
        a /= 10;
    }
    if (flag) b *= -1;
    cout << b << endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/quanjun/p/11954123.html