当前位置:   article > 正文

zzuli新生选拔赛(一)_7-4 1-4 输入的行数打印*号金字塔

7-4 1-4 输入的行数打印*号金字塔
  • 由于时间原因, 只提供代码和大概思路 (我代码中可能有很多没用到的东西, 不用奇怪)

  • 我的代码里有很多偷懒的语法, 不明白的可以查一下

7-1 整数加减乘计算器

模拟

//#include <bits/stdc++.h>
#include <iostream>
#include <cstring>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#define de(a) cout << #a << " = " << a << "\n";
#define deg(a) cout << #a << " = " << a << " ";
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define VI vector<int>
#define PII pair<int, int>
#define int long long
#define sz() ((int)a.size())
#define LL long long
using namespace std;
const int N = 1e6 + 10, M = N * 2;
int n, m;
int a[N];

void solve() {
    char op;
    cin >> n >> op >> m;
    if (op == '+') {
        cout << n + m;
    } else if (op == '-'){
        cout << n - m;
    } else if (op == '*') {
        cout << n * m;
    } else {
        cout << "error";
    }
}


signed main() {
    IOS;
    int T = 1;
//    cin >> T;
    while (T --) solve();
    return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

7-2 个税查询

模拟 (真的恶心)

//#include <bits/stdc++.h>
#include <iostream>
#include <cstring>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#define de(a) cout << #a << " = " << a << "\n";
#define deg(a) cout << #a << " = " << a << " ";
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define VI vector<int>
#define PII pair<int, int>
#define int long long
#define sz() ((int)a.size())
#define LL long long
using namespace std;
const int N = 1e6 + 10, M = N * 2;
const double eps = 1e-5;
int n, m;
int a[N];

void print(double a, double b) {
    printf("salary=%.2f,tax=%.2f", a, b);
}

void solve() {
    double s; cin >> s;
    if (s <= 12 * 5000) {
        print(s, 0);
        return;
    }
    double r = s;
    s -= 12 * 5000;
    if (s <= 36000) {
        print(r, s * 0.03);
    } else if (s <= 144000) {
        print(r, 36000.0 * 0.03 + (s - 36000) * 0.1);
    } else if (s <= 300000) {
        print(r, 36000.0 * 0.03 + (144000 - 36000) * 0.1 + (s - 144000) * 0.2);
    } else if (s <= 420000) {
        print(r, 36000.0 * 0.03 + (144000 - 36000) * 0.1 + (300000 - 144000) * 0.2
                 + (s - 300000) * 0.25);
    } else if (s <= 660000) {
        print(r, 36000.0 * 0.03 + (144000 - 36000) * 0.1 + (300000 - 144000) * 0.2
                 + (420000 - 300000) * 0.25 + (s - 420000) * 0.3);
    } else if (s <= 960000) {
        print(r, 36000.0 * 0.03 + (144000 - 36000) * 0.1 + (300000 - 144000) * 0.2
                 + (420000 - 300000) * 0.25 + (660000 - 420000) * 0.3 
                 + (s - 660000) * 0.35);
    } else {
        print(r, 36000.0 * 0.03 + (144000 - 36000) * 0.1 + (300000 - 144000) * 0.2
                 + (420000 - 300000) * 0.25 + (660000 - 420000) * 0.3 
                 + (960000 - 660000) * 0.35 + (s - 960000) * 0.45);
    }
}


signed main() {
//     IOS;
    int T = 1;
//    cin >> T;
    while (T --) solve();
    return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66

7-3 1-4 输入的行数打印*号金字塔

模拟 + 找规律

//#include <bits/stdc++.h>
#include <iostream>
#include <cstring>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#define de(a) cout << #a << " = " << a << "\n";
#define deg(a) cout << #a << " = " << a << " ";
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define VI vector<int>
#define PII pair<int, int>
#define int long long
#define sz() ((int)a.size())
#define LL long long
using namespace std;
const int N = 1e6 + 10, M = N * 2;
int n, m;
int a[N];

void solve() {
    cin >> n;
        if (n > 10 || n <= 0) {
            cout << "输入数据有误!";
            return;
        }
        for (int i = 1; i <= n; i ++) {
            for (int j = 1; j <= n - i; j ++) cout << ' ';
            for (int j = 1; j <= 2 * i - 1; j ++) cout << "*";
            if (i != n) cout << "\n";
        }
    
}


signed main() {
//     IOS;
    int T = 1;
//    cin >> T;
    while (T --) solve();
    return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

7-4 查找问题

给的接口函数, 但必须把代码写全才对 (不是很懂

#include  <stdio.h>
#define N 50
int SearchbyNum(long num[], int score[], int n,long number);

int main()
{  long  num[N],number;
   int  score[N],n,i,pos;
     
   scanf("%d", &n);
   for (i=0; i<n; i++)
      scanf("%ld:%d ", &num[i], &score[i]);
   scanf("%ld", &number);
     
   pos=SearchbyNum(num,score,n,number);
     
   if(pos==-1)
      printf("Not found!\n");
   else
      printf("%d\n",score[pos]);
            
   return 0;
}


int SearchbyNum(long num[], int score[], int n,long number) {
    for (int i = 0; i < n; i ++) {
        if (number == num[i]) {
            return i;
        }
    }
    return -1;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

7-5 双曲正弦函数(*)

模拟 模拟 模拟

记录下一次要加上的数的分子和分母, 判断即可

//#include <bits/stdc++.h>
#include <iostream>
#include <cstring>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#define de(a) cout << #a << " = " << a << "\n";
#define deg(a) cout << #a << " = " << a << " ";
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define VI vector<int>
#define PII pair<int, int>
#define int long long
#define sz() ((int)a.size())
#define LL long long
#define db double
using namespace std;
const int N = 1e6 + 10, M = N * 2;
db eps;  // 精度
int n, m;
int a[N];

void solve() {
    db x; cin >> x >> eps;
    db m = 1, z = x;
    db sum = 0;
    
    int mulm = 1;
    db mulz = x * x;
    
    db ne = x;
    while (abs(ne) >= eps) {
        sum += ne;
        ne = z * mulz / (m * (mulm + 2) * (mulm + 1));
        z = z * mulz;
        m = m * (mulm + 2) * (mulm + 1);
        mulm += 2;
    }
    sum += ne;
    printf("%.6f", sum);
    
}


signed main() {
//     IOS;
    int T = 1;
//    cin >> T;
    while (T --) solve();
    return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

7-6 确定最终排名

排序题 (注意相同的数要按编号输出)

#include <bits/stdc++.h>
#define de(a) cout << #a << " = " << a << "\n";
#define deg(a) cout << #a << " = " << a << " ";
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define VI vector<int>
#define PII pair<int, int>
#define int long long
#define sz() ((int)a.size())
#define LL long long
using namespace std;
const int N = 1e6 + 10, M = N * 2;
struct Node {
    string na;
    int sol;
    int pos;
    bool operator> (const Node W) const {  // 重载大于号
        if (sol != W.sol) return sol > W.sol;
        return pos < W.pos;
    }
}peo[110];

int n, m;
int a[N];


void solve() {
    cin >> n;
    for (int i = 0; i < n; i ++) {
        string na;
        int sol;
        cin >> na >> sol;
        peo[i] = {na, sol, i};
    }
    sort(peo, peo + n, greater<Node>());  // 也可以直接写个cmp函数
    for (int i = 0; i < n; i ++) {
        auto [na, sol, p] = peo[i];
        cout << i + 1 << " " << na << ' ' << sol << "\n";
    }
}


signed main() {
    IOS;
    int T = 1;
    cin >> T;
    while (T --) solve();
    return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

7-7 石子合并

环形区间dp

解决环的通用方式, 破环成链: 将前n个数赋值给后n个数就行

#include <bits/stdc++.h>
#define de(a) cout << #a << " = " << a << "\n";
#define deg(a) cout << #a << " = " << a << " ";
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define mem(a, b) memset(a, b, sizeof a)
#define VI vector<int>
#define PII pair<int, int>
#define int long long
#define sz() ((int)a.size())
#define LL long long
using namespace std;
const int N = 2000 + 10, M = N * 2;
int n, m;
int a[N];
int s[N];
int f[N][N];

void solve() {
    cin >> n;
    for (int i = 1; i <= n; i ++) cin >> a[i], a[i + n] = a[i];
    for (int i = 1; i <= 2 * n; i ++) s[i] = s[i - 1] + a[i];
    mem(f, 0x3f);
    for (int len = 1; len <= n; len ++) {
        for (int l = 1; l + len - 1 <= 2 * n; l ++) {
            int r = l + len - 1;
            if (len == 1) f[l][l] = 0;
            for (int k = l; k < r; k ++) {
                f[l][r] = min(f[l][r], f[l][k] + f[k + 1][r] + s[r] - s[l - 1]);
            }
        }
    }
    int res = f[1][n];
    for (int i = 2; i <= n; i ++) res = min(res, f[i][i + n - 1]);
    cout << res << "\n";
    mem(f, -0x3f);
    for (int len = 1; len <= n; len ++) {
        for (int l = 1; l + len - 1 <= 2 * n; l ++) {
            int r = l + len - 1;
            if (len == 1) f[l][l] = 0;
            for (int k = l; k < r; k ++) {
                f[l][r] = max(f[l][r], f[l][k] + f[k + 1][r] + s[r] - s[l - 1]);
            }
        }
    }
    res = f[1][n];
    for (int i = 2; i <= n; i ++) res = max(res, f[i][i + n - 1]);
    cout << res;
}


signed main() {
    IOS;
    int T = 1;
//    cin >> T;
    while (T --) solve();
    return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

7-8 珍珠

双指针

#include <bits/stdc++.h>
#define de(a) cout << #a << " = " << a << "\n";
#define deg(a) cout << #a << " = " << a << " ";
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define mem(a, b) memset(a, b, sizeof a)
#define VI vector<int>
#define PII pair<int, int>
#define int long long
#define sz() ((int)a.size())
#define LL long long
using namespace std;
const int N = 1000000 + 10, M = N * 2;
int n, m;
int a[N];

void solve() {
    cin >> n >> m;
    int res = 100000001;
    for (int i = 1; i <= n; i ++) cin >> a[i];
    for (int i = 1, j = i, s = 0; i <= n; i ++) {
        while (j <= n + 1 && s < m) s += a[j], j ++;
        if (j <= n + 1) res = min(res, j - i);
        else break;
        s -= a[i];
    }
    if (res == 100000001) cout << "0\n";
    else cout << res << "\n";
}


signed main() {
    IOS;
    int T = 1;
    cin >> T;
    while (T --) solve();
    return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

7-9 今年12月不AC?

贪心

以结束时间从小到大排序, 依次选取一定是最优解 (在这不给予证明

#include <bits/stdc++.h>
#define de(a) cout << #a << " = " << a << "\n";
#define deg(a) cout << #a << " = " << a << " ";
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define mem(a, b) memset(a, b, sizeof a)
#define VI vector<int>
#define PII pair<int, int>
#define int long long
#define sz(a) ((int)a.size())
#define LL long long
using namespace std;
const int N = 1000000 + 10, M = N * 2;
int n, m;
int a[N];
vector<PII> act;

void solve() {
    while (cin >> n, n) {
        act.clear();
        for (int i = 1; i <= n; i ++) {
            int b, e; cin >> b >> e;
            act.push_back({e, b});
        }
        sort(act.begin(), act.end());
        int res = 1;
        int las = act[0].first;
        for (int i = 1; i < sz(act); i ++) {
            if (act[i].second >= las) {
                las = act[i].first;
                res ++;
            }
        }
        cout << res;
    }
}


signed main() {
    IOS;
    int T = 1;
    //cin >> T;
    while (T --) solve();
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

7-10 迷宫问题之最短时间

最短路 (刚开始没看见还有边权不为1的边, 就写的bfs, 后来发现不想写其他的了,微微改了一下

:(这个写的并不好, 仅供参考

#include <bits/stdc++.h>
#define de(a) cout << #a << " = " << a << "\n";
#define deg(a) cout << #a << " = " << a << " ";
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define mem(a, b) memset(a, b, sizeof a)
#define VI vector<int>
#define PII pair<int, int>
#define int long long
#define sz(a) ((int)a.size())
#define LL long long
using namespace std;
const int N = 100 + 10, M = N * 2;
int n, m;
int dist[N][N];
char g[N][N];
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int bex, bey;
int enx, eny;

void bfs() {
    mem(dist, 0x3f);
    queue<PII> q;
    q.push({bex, bey});
    dist[bex][bey] = 0;
    while (q.size()) {
        auto [x, y] = q.front(); q.pop();
        for (int i = 0; i < 4; i ++) {
            int a = x + dx[i], b = y + dy[i];
            if (a < 1 || a > n || b < 1 || b > m) continue;
            if (g[a][b] == '#') continue;
            if (isdigit(g[a][b])) {
                if (dist[a][b] <= dist[x][y] + g[a][b] - '0') continue;
                dist[a][b] = dist[x][y] + g[a][b] - '0' + 1;
                q.push({a, b});
            } else {
                if (dist[a][b] <= dist[x][y] + 1) continue; 
                dist[a][b] = dist[x][y] + 1;
                q.push({a, b});
            }
        }
    }
}

void solve() {
    while (cin >> n >> m) {
        for (int i = 1; i <= n; i ++) cin >> g[i] + 1;

        bool has = false;
        for (int i = 1; i <= n; i ++) {
            if (has) break;
            for (int j = 1; j <= m; j ++)
                if (g[i][j] == 'S') {
                    bex = i, bey = j;
                } else if (g[i][j] == 'T') {
                    enx = i, eny = j;
                }
        }
        bfs();
        if (dist[enx][eny] == dist[0][0]) cout << "impossible\n";
        else cout << dist[enx][eny] << "\n";
    }
}


signed main() {
    IOS;
    int T = 1;
    //cin >> T;
    while (T --) solve();
    return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73

7-11 门派

并查集典题

开个数组维护连通块的大小就行

#include <bits/stdc++.h>
#define de(a) cout << #a << " = " << a << "\n";
#define deg(a) cout << #a << " = " << a << " ";
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define mem(a, b) memset(a, b, sizeof a)
#define VI vector<int>
#define PII pair<int, int>
#define int long long
//#define sz(a) ((int)a.size())
#define LL long long
using namespace std;
const int N = 1000 + 10, M = N * 2;
int n, m;
int p[N];
int sz[N];

int find(int x) {
    if (p[x] != x) p[x] = find(p[x]);
    return p[x];
}

void solve() {
    cin >> n >> m;
    iota(p, p + n + 1, 0);
    for (int i = 1; i <= n; i ++) sz[i] = 1;
    while (m --) {
        int a, b; cin >> a >> b;
        if (find(a) != find(b)) {
            sz[find(b)] += sz[find(a)];
            p[find(a)] = p[find(b)]; 
        }
    }
    int cnt = 0, maxx = 0;
    for (int i = 1; i <= n; i ++)
        if (p[i] == i) {
            cnt ++;
            maxx = max(maxx, sz[i]);
        }
    cout << cnt << " " << maxx << "\n";
}


signed main() {
    IOS;
    int T = 1;
    cin >> T;
    while (T --) solve();
    return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/884285
推荐阅读
相关标签
  

闽ICP备14008679号