前言

发现自己还能再参加一次天梯赛,在高兴之余,决定把在赛前将所有的天梯赛真题过一遍,希望自己可以取得理想的成绩。但是由于时间问题,而 L1 的题目相对比较简单,没有必要每一题都写详细的题解,就以文本来记录 L1 的题解吧。

文章目录

L1-001 Hello World (5分)

#include <iostream>
using namespace std;
int main()
{
  cout<<"Hello World!"<<endl;
  return 0;
}

L1-002 打印沙漏 (20分)

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

int main()
{
    int n;
    char ch;
    scanf("%d %c", &n, &ch);
    // target 表示本次需要使用的符号数
    int target = 1, col = 3, row = 1;
    while ((target + col * 2) <= n) {
        target += col * 2;
        col += 2;
        row++;
    }
    col -= 2;
    // 打印上半部分
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col - i; j++) {
            if (j < i) {
                putchar(' ');
            } else {
                putchar(ch);
            }
        }
        putchar('\n');
    }
    // 打印下半部分
    for (int i = row - 2; i >= 0; i--) {
        for (int j = 0; j < col - i; j++) {
            if (j < i) {
                putchar(' ');
            } else {
                putchar(ch);
            }
        }
        putchar('\n');
    }
    printf("%d", n - target);
    return 0;
}

L1-003 个位数统计(15分)

#include <bits/stdc++.h>
using namespace std;
int mapper[10];
char str[1000];

int main()
{
    scanf("%s", str);
    for (int i = 0; i < strlen(str); i++) {
        mapper[str[i] - '0']++;
    }
    for (int i = 0; i < 10; i++) {
        if (mapper[i]) {
            printf("%d:%d\n", i, mapper[i]);
        }
    }
    return 0;
}

L1-004 计算摄氏温度(5分)

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin >> n;
    cout << "Celsius = " << 5 * (n - 32) / 9 << endl;
    return 0;
}

L1-005 考试座位号(15分)

#include <bits/stdc++.h>
using namespace std;
// 试机号 -> (准考证号 ,考试号) 
map<int, pair<string, int> > mark;

int main()
{
    int n, m;
    cin >> n;
    for (int i = 0; i < n; i++) {
        string stuNum;
        int id, target;
        cin >> stuNum >> id >> target;
        mark[id] = make_pair(stuNum, target);
    }
    cin >> m;
    for (int i = 0; i < m; i++) {
        int t;
        cin >> t;
        cout << mark[t].first << " " << mark[t].second << endl;
    }
    return 0;
}

L1-006 连续因子(20分)

因为在 ( n , n ] ( \sqrt{n},n] (n ,n] 中是不存在连续因子的,所以我们只需要遍历区间 [ 2 , n ] [2, \sqrt{n}] [2,n ],求出所有连续因子,并记录最长连续因子的 “开始因数” 和 “连续长度” 即可。

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

int main()
{
    ll n;
    cin >> n;
    int isPrime = 1, maxLen = 0, start;
    for (int i = 2; i <= sqrt(n); i++) {
        if (n % i == 0) {
        	// cnt 表示以 i 为起点,连续因子的长度
            int cnt = 0, temp = n, j = i;
            while (temp % j == 0) {
                temp /= j;
                j++;
                cnt++;
            }
            // 维护 maxLen 和 start
            if (cnt > maxLen) {
                maxLen = cnt;
                start = i;
            }
        }
    }
    // 注意判断 n 为素数的情况
    if (!maxLen) {
        cout << "1" << endl;
        cout << n << endl;
    } else {
        cout << maxLen << endl;
        for (int i = start; i < start + maxLen; i++) {
            if (i != start) {
                cout << "*";
            }
            cout << i;
        }
    }
    return 0;
}

L1-007 念数字(10分)

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

int main() {
    string num;
    cin >> num;
    for (int i = 0; i < num.length(); i++) {
        if (i != 0) {
            putchar(' ');
        }
        switch(num[i]) {
            case '0': printf("ling"); break;
            case '1': printf("yi"); break;
            case '2': printf("er"); break;
            case '3': printf("san"); break;
            case '4': printf("si"); break;
            case '5': printf("wu"); break;
            case '6': printf("liu"); break;
            case '7': printf("qi"); break;
            case '8': printf("ba"); break;
            case '9': printf("jiu"); break;
            default: printf("fu");
        }
    }
    return 0;
}

L1-008 求整数段和(10分)

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

int main()
{
    int from, to;
    cin >> from >> to;
    int sum = 0, j = 0;
    for (int i = from; i <= to; i++, j++) {
        sum += i;
        printf("%5d", i);
        if ((j + 1) % 5 == 0) {
            putchar('\n');
        }
    }
    // 注意格式问题
    if (j % 5 != 0) {
        putchar('\n');
    }
    printf("Sum = %d", sum);
    return 0;
}

L1-009 N个数求和(20分)

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

ll gcd(ll a, ll b) {
    return (b == 0) ? a : gcd(b, a % b);
}

int main()
{
    int n;
    scanf("%d", &n);
    ll a = 0, b = 1;
    for (int i = 0; i < n; i++) {
        ll ta, tb;
        scanf("%lld/%lld", &ta, &tb);
        a = a * tb + ta * b;
        b = b * tb;
        ll GCD = gcd(a, b);
        a /= GCD;
        b /= GCD;
    }
    if (a % b == 0) {
        printf("%lld\n", a / b);
    } else if (a > b) {
        printf("%lld %lld/%lld\n", a / b, a % b, b);
    } else {
        printf("%lld/%lld\n", a, b);
    }
    return 0;
}

L1-010 比较大小(10分)

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

int main()
{
    int a[3];
    cin >> a[0] >> a[1] >> a[2];
    sort(a, a + 3);
    cout << a[0] << "->" << a[1] << "->" << a[2];
    return 0;
}

L1-011 A-B(20分)

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

int main()
{
    string a, b;
    getline(cin, a);
    getline(cin, b);
    for (int i = 0; i < b.length(); i++) {
        recordSet.insert(b[i]);
    }
    for (int i = 0; i < a.length(); i++) {
        if (!recordSet.count(a[i])) {
            printf("%c", a[i]);
        }
    }
    return 0;
}

L1-012 计算指数(5分)

#include <iostream>
using namespace std;

int main()
{
    int n;
    scanf("%d", &n);
    printf("2^%d = %d", n, 1 << n);
    return 0;
}

L1-013 计算阶乘和(10分)

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    scanf("%d", &n);
    // fac 表示阶乘
    int sum = 0, fac = 1;
    for (int i = 1; i <= n; i++) {
        fac *= i;
        sum += fac;
    }
    printf("%d", sum);
    return 0;
}

L1-014 简单题(5分)

#include <iostream>

using namespace std;
int main()
{
    cout << "This is a simple problem.";
    return 0;
}

L1-015 跟奥巴马一起画方块(15分)

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

int main()
{
    int n;
    char ch;
    scanf("%d %c", &n, &ch);
    for (int i = 0; i < (n + 1) / 2; i++) {
        for (int j = 0; j < n; j++) {
            printf("%c", ch);
        }
        printf("\n");
    }
    return 0;
}

L1-016 查验身份证(15分)

#include <bits/stdc++.h>
using namespace std;
int Z[] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
char M[] = "10X98765432";

int main()
{
    int n;
    cin >> n;
    int isAllPassed = 1;
    while (n--) {
        string num;
        cin >> num;
        int sum = 0, isAllDigit = 1;
        for (int i = 0; i < 17; i++) {
            if (!isdigit(num[i])) {
                isAllDigit = 0;
            }
            sum += (num[i] - '0') * Z[i];
        }
        if (!isAllDigit || (num[17] != M[sum % 11])) {
            cout << num << endl;
            isAllPassed = 0;
        }
    }
    if (isAllPassed) {
        cout << "All passed" << endl;
    }
    return 0;
}

L1-017 到底有多二(15分)

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

int main()
{
    string num;
    cin >> num;
    int len = num.length(); // 数字长度
    int isNegative = (num[0] == '-') ? 1 :0; // 是否为负数
    int isEven = ((num[len - 1] - '0') % 2 == 0); // 是否为偶数
    int twoCnt = 0;
    for (int i = isNegative; i <= len; i++) {
        if (num[i] == '2') {
            twoCnt++;
        }
    }
    double ans = ((double)twoCnt / (len - isNegative)) * (isNegative ? 1.5 : 1) * (isEven ? 2 : 1);
    printf("%.2f%%", ans * 100);
    return 0;
}

L1-018 大笨钟(10分)

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

int main()
{
    int h, m;
    scanf("%d:%d", &h, &m);
    if ((h < 12) || (h == 12 && m == 0)) {
        printf("Only %02d:%02d.  Too early to Dang.\n", h, m);
    } else {
        for (int i = 0; i < ((h - 12) + (m > 0)); i++) {
            printf("Dang");
        }
    }
    return 0;
}

L1-019 谁先倒(15分)

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

int main()
{
    int a, b, ta, tb;
    cin >> a >> b;
    ta = a, tb = b;
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        int x, tx, y, ty;
        cin >> x >> tx >> y >> ty;
        int isALose = (tx == x + y);
        int isBLose = (ty == x + y);
        // 不同时为1,且不同时为0
        if (isALose ^  isBLose) {
            if (isALose) a--;
            else b--;
        }
        if (a < 0) {
            cout << "A" << endl;
            cout << tb - b << endl;
            break;
        } else if (b < 0) {
            cout << "B" << endl;
            cout << ta - a << endl;
            break;
        }
    }
    return 0;
}

L1-020 帅到没朋友(20分)

#include <bits/stdc++.h>
#include <unordered_set>
using namespace std;
const int maxn = 105;
// 需要用 unordered_set,否则有个点会超时
unordered_set<int> pyq[maxn], vis;
vector<int> ans;

int main()
{
    int n, k, t, m;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> k;
        for (int j = 0; j < k; j++) {
            cin >> t;
            pyq[i].insert(t);
        }
    }

    cin >> m;
    for (int i = 0; i < m; i++) {
        cin >> t;
        if (!vis.count(t)) {
            vis.insert(t);
            int isHeadsome = 1;
            for (int j = 0; j < n; j++) {
            	// 如果他某个朋友圈,且该朋友圈人数大于 1
                if (pyq[j].count(t) && pyq[j].size() > 1) {
                    isHeadsome = 0;
                    break;
                }
            }
            if (isHeadsome) {
                ans.push_back(t); // 为了格式化输出
            }
        }
    }
    if (!ans.size()) {
        printf("No one is handsome\n");
    } else {
        for (int i = 0; i < ans.size(); i++) {
            if (i != 0) {
                putchar(' ');
            }
            printf("%05d", ans[i]);
        }
    }
    return 0;
}

L1-021 重要的话说三遍(5分)

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

int main()
{
    for (int i = 0; i < 3; i++) {
        cout << "I'm gonna WIN!" << endl;
    }
    return 0;
}

L1-022 奇偶分家(10分)

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

int main()
{
    int n;
    cin >> n;
    int evenCnt = 0, oddCnt = 0;
    for (int i = 0; i < n; i++) {
        int t;
        cin >> t;
        if (t & 1) oddCnt++;
        else evenCnt++;
    }
    cout << oddCnt << " " << evenCnt << endl;
    return 0;
}

L1-023 输出GPLT(20分)

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

int main()
{
    string str;
    cin >> str;
    int G = 0, P = 0, L = 0, T = 0;
    // 统计数量
    for (int i = 0; i < str.length(); i++) {
        if (str[i] == 'G' || str[i] == 'g') G++;
        else if (str[i] == 'P' || str[i] == 'p') P++;
        else if (str[i] == 'L' || str[i] == 'l') L++;
        else if (str[i] == 'T' || str[i] == 't') T++;
    }
    // 按统计结果输出
    while (G || P || L || T) {
        if (G) {
            putchar('G');
            G--;
        }
        if (P) {
            putchar('P');
            P--;
        }
        if (L) {
            putchar('L');
            L--;
        }
        if (T) {
            putchar('T');
            T--;
        }
    }
    return 0;
}

L1-024 后天 (5分)

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin >> n;
    cout << (n + 1) % 7 + 1 << endl;
    return 0;
}

L1-025 正整数A+B (15分)

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

int stringToNum(string &str) {
    int ans = 0;
    for (int i = 0; i < str.length(); i++) {
        if (str[i] >= '0' && str[i] <= '9') {
            ans = ans * 10 + (str[i] - '0');
        } else {
            return -1;
        }
    }
    return (ans > 0 && ans <= 1000) ? ans : -1;
}

int main()
{
    string aStr, bStr;
    // 由于输入可能有多个空格,需要注意输入
    cin >> aStr;
    getchar();
    getline(cin, bStr);
    int a = stringToNum(aStr);
    int b = stringToNum(bStr);
    if (a == -1) {
        cout << "?";
    } else {
        cout << a;
    }
    cout << " + ";
    if (b == -1) {
        cout << "?";
    } else {
        cout << b;
    }
    cout << " = ";
    if (a == -1 || b == -1) {
        cout << "?";
    } else {
        cout << a + b;
    }
    return 0;
}

L1-026 I Love GPLT(5分)

#include <bits/stdc++.h>
using namespace std;
int main()
{
    string str = "I Love GPLT";
    for (int i = 0; i < str.length(); i++) {
        cout << str[i] << endl;
    }
    return 0;
}

L1-027 出租(20分)

#include <bits/stdc++.h>
using namespace std;
const int maxn = 15;
int cnt[maxn];   // 用于标记数字是否出现过
int mapper[maxn];// 存储 arr[i] 在号码中的下标
int arr[maxn];   // 需要输出 arr 数组

int main()
{
    string num;
    cin >> num;
    for (int i = 0; i < num.length(); i++) {
        int n = num[i] - '0';
        cnt[n]++;
    }
    int len = 0;
    for (int i = 9; i >= 0; i--) {
        if (cnt[i]) {
            mapper[i] = len;
            arr[len++] = i;
        }
    }
    cout << "int[] arr = new int[]{";
    for (int i = 0; i < len; i++) {
        if (i != 0) {
            cout << ",";
        }
        cout << arr[i];
    }
    cout << "};"<< endl;
    cout << "int[] index = new int[]{";
    for (int i = 0; i < num.length(); i++) {
        if (i != 0) {
            cout << ",";
        }
        int n = num[i] - '0';
        cout << mapper[n];
    }
    cout << "};" << endl;
    return 0;
}

L1-028 判断素数 (10分)

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

bool isPrime(int n) {
    if (n < 2) return false;
    if (n == 2) return true;
    for (int i = 2; i <= sqrt(n); i++) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}

int main()
{
    int n;
    cin >> n;
    while (n--) {
        int t;
        cin >> t;
        if (isPrime(t)) {
            cout << "Yes" << endl;
        } else {
            cout << "No" << endl;
        }
    }
    return 0;
}

L1-029 是不是太胖了(5分)

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    int n;
    cin >> n;
    printf("%.1f", ((n - 100) * 0.9) * 2);
    return 0;
}

L1-030 一帮一 (15分)

#include <bits/stdtr1c++.h>
using namespace std;
const int maxn = 105;
pair<int, string> s[maxn];
bool vis[maxn]; // 标记是否访问过

int main()
{
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> s[i].first >> s[i].second;
    }
    for (int i = 0; i < n / 2; i++) {
        for (int j = n - 1; j >= n / 2; j--) {
            if (!vis[j] && s[i].first != s[j].first) {
                vis[j] = true;
                cout << s[i].second << " " << s[j].second << endl;
                break;
            }
        }
    }
    return 0;
}

L1-031 到底是不是太胖了(10分)

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

int main()
{
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        int h;
        double w;
        cin >> h >> w;
        double sw = (h - 100) * 0.9 * 2;
        if (fabs(w - sw) < sw * 0.1) {
            cout << "You are wan mei!" << endl;
        } else if (w > sw) {
            cout << "You are tai pang le!" << endl;
        } else {
            cout << "You are tai shou le!" << endl;
        }
    }
    return 0;
}

L1-032 Left-pad (20分)

#include <bits/stdc++.h>
using namespace std;
string str, ch;
int n, len;

int main()
{
    cin >> n >> ch;
    getchar();
    getline(cin, str);
    len = str.length();
    if (len < n) {
        for (int i = 0; i < n - len; i++) {
            cout << ch;
        }
        cout << str << endl;
    } else {
        for (int i = len - n; i < len; i++) {
            cout << str[i];
        }
    }
    return 0;
}

L1-033 出生年 (15分)

#include <bits/stdc++.h>
using namespace std;
int y, n;

bool isOk(int y, int n) {
    set<int> cnt;
    if (y < 1000) {
        cnt.insert(0);
    }
    while (y != 0) {
        cnt.insert(y % 10);
        y /= 10;
    }
    return (cnt.size() == n);
}

int main()
{
    cin >> y >> n;
    for (int i = y; i < 99999; i++) {
        if (isOk(i, n)) {
            printf("%d %04d", i - y, i);
            break;
        }
    }
    return 0;
}

L1-034 点赞 (20分)

#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

// first 表示出现标签编号,second表示出现次数
bool cmp(const pair<int, int> &A, const pair<int, int> &B) {
    if (A.second == B.second) {
        return A.first > B.first;
    } else {
        return A.second > B.second;
    }
}

int main()
{
    int t, cnt = 0;
    map<int, int> mapper;
    cin >> t;
    for (int i = 0; i < t; i++) {
        int n;
        cin >> n;
        for (int j = 0; j < n; j++) {
            int k;
            cin >> k;
            mapper[k]++;
        }
    }
    vector<pair<int, int> > a;
    for (auto it = mapper.begin(); it != mapper.end(); it++) {
        a.push_back(make_pair(it->first, it->second));
    }
    sort(a.begin(), a.end(), cmp);
    cout << a[0].first << " " << a[0].second << endl;
    return 0;
}

L1-035 情人节 (15分)

#include <bits/stdc++.h>
using namespace std;
vector<string> vec;
string str;

int main()
{
    while (cin >> str) {
        if (str == ".") {
            break;
        }
        vec.push_back(str);
    }
    int cnt = vec.size();
    if (cnt >= 14) {
        cout << vec[1] << " and " << vec[13] << " are inviting you to dinner..." << endl;
    } else if (cnt >= 2) {
        cout << vec[1] << " is the only one for you..." << endl;
    } else {
        cout << "Momo... No one is for you ..." << endl;
    }
    return 0;
}

L1-036 A乘以B(5分)

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int a, b;
    cin >> a >> b;
    cout << a * b;
    return 0;
}

L1-037 A除以B(10分)

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int a, b;
    cin >> a >> b;
    // 输出的格式主要取决于b的值
    if(b == 0) {
        printf("%d/%d=Error", a, b);
    } else if (b < 0) {
        printf("%d/(%d)=%.2lf", a, b, (double)a / b);
    } else {
        printf("%d/%d=%.2lf",a, b, (double)a / b);
    }
    return 0;
}

L1-038 新世界(5分)

#include <bits/stdc++.h>
using namespace std;
int main()
{
    cout << "Hello World" << endl;
    cout << "Hello New World" << endl;
    return 0;
}

L1-039 古风排版 (20分)

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

int main()
{
    int n;
    string str;
    cin >> n;
    getchar();
    getline(cin, str);
    int len = str.length();
    // 计算总共需要多少列
    int line = (len + n - 1) / n;
    char ans[n + 5][line + 5];
    // 核心是将题意需要的格式先存在字符串数组ans中
    int r = 0, c = line - 1;
    for (int i = 0; i < len; i++) {
        ans[r][c] = str[i];
        r++;
        if (r == n) {
            r = 0;
            c--;
        }
    }
    if (r != 0) {
    	// 最后一列缺少的字符
        for (int i = r; i < n; i++) {
            ans[i][c] = ' ';
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < line; j++) {
            cout << ans[i][j];
        }
        cout << endl;
    }
    return 0;
}

L1-040 最佳情侣身高差(10分)

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        string sex;
        double height;
        cin >> sex >> height;
        if (sex == "M") {
            printf("%.2f\n", height / 1.09);
        } else {
            printf("%.2f\n", height * 1.09);
        }
    }
    return 0;
}

L1-041 寻找250(10分)

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int ans = 0, num;
    while (cin >> num) {
        ans++;
        if (num == 250) {
            cout << ans << endl;
            break;
        }
    }
    return 0;
}

L1-042 日期格式化(5分)

#include <cstdio>
int main()
{
    int a, b, c;
    scanf("%d-%d-%d", &b, &c, &a);
    printf("%04d-%02d-%02d\n", a, b, c);
    return 0;
}

L1-043 阅览室(20分)

坑点:不能只用一个数组来存储书号对应的借书时间,然后用是否有借书时间来判断这是本书是否被借过,因为借书时间有可能是在 00:00,然后进行逻辑判断的时候就会认为这本书没有被借过,最后导致 test3 和 test4 过不去。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1005;
int n, t, h, m;
// vis 表示书籍是否被借过,values 表示借书时间
int vis[maxn], values[maxn];

int main()
{
    cin >> n;
    for (int i = 0; i < n; i++) {
        double sum = 0;
        int cnt = 0;
        while (cin >> t) {
            string key;
            cin >> key;
            scanf("%d:%d", &h, &m);
            if (t == 0) {
                break;
            }
            int time = h * 60 + m;
            if (key == "S") {
                vis[t] = 1;
                values[t] = time;
            } else if (key == "E") {
                if (vis[t]) {
                    vis[t] = 0;
                    sum += time - values[t];
                    values[t] = 0;
                    cnt++;
                }
            }
        }
        printf("%d %.0f\n", cnt, round(cnt ? sum / cnt : sum));
    }
    return 0;
}

L1-044 稳赢 (15分)

#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
// C++ 11-14 uniform initialize
unordered_map<string, string> win {
    {"ChuiZi", "Bu"},
    {"Bu", "JianDao"},
    {"JianDao", "ChuiZi"}
};


int main()
{
    int k, cnt = 0;
    cin >> k;
    string str;
    while (cin >> str) {
        if (str == "End") {
            break;
        }
        if (cnt == k) {
            cnt = 0;
            cout << str << endl;
        } else {
            cnt++;
            cout << win[str] << endl;
        }
    }

    return 0;
}

L1-045 宇宙无敌大招呼(5分)

#include <bits/stdc++.h>
using namespace std;
int main() 
{
    string str;
    cin >> str;
    cout << "Hello " << str << endl;
    return 0;
}

L1-046 整除光棍(20分)

涉及到高精度,只能用 Java 的 BigInteger 大法了。

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
	
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		BigInteger num = cin.nextBigInteger();
		for (BigInteger i = new BigInteger("1"); ;i = i.multiply(new BigInteger("10")).add(new BigInteger("1"))) {
			if (i.mod(num).equals(new BigInteger("0"))) {
				System.out.println(i.divide(num) + " " + i.toString().length());
				break;
			}
		}
		cin.close();
	}
}

L1-047 装睡(10分)

#include <iostream>
#include <string>
using namespace std;

int main()
{
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        string str;
        int p, q;
        cin >> str >> p >> q;
        if (((p < 15 || p > 20)) || (q < 50 || q > 70)) {
            cout << str << endl;
        }
    }
    return 0;
}

L1-048 矩阵A乘以B(15分)

#include <iostream>
using namespace std;
const int maxn = 500;
int ra, ca, rb, cb;
int a[maxn][maxn];
int b[maxn][maxn];
int c[maxn][maxn];

int main()
{
    cin >> ra >> ca;
    for (int i = 0; i < ra; i++) {
        for (int j = 0; j < ca; j++) {
            cin >> a[i][j];
        }
    }
    cin >> rb >> cb;
    for (int i = 0; i < rb; i++) {
        for (int j = 0; j < cb; j++) {
            cin >> b[i][j];
        }
    }
    if (ca != rb) {
        cout << "Error: " << ca << " != " << rb << endl;
    } else {
        cout << ra << " " << cb << endl;
        for (int i = 0; i < ra; i++) {
            for (int j = 0; j < ca ; j++) {
                for (int k = 0; k < cb; k++) {
                    c[i][k] += a[i][j] * b[j][k];
                }
            }
        }
        for (int i = 0; i < ra; i++) {
            for (int j = 0; j < cb; j++) {
                if (j != 0) {
                    cout << " ";
                }
                cout << c[i][j];
            }
            cout << endl;
        }
    }
    return 0;
}

L1-049 天梯赛座位分配(20分)

#include <iostream>
#include <cstdio>
#include <vector>
#include <unordered_set>
using namespace std;
vector<int> a[105][15];
vector<int> s, cnt; // s 记录学校队伍数量, cnt记录学校完成分配的队伍数量
unordered_set<int> vis; // 记录已经完成分配的学校
int n;

int main()
{
    cin >> n;
    for (int i = 0; i < n; i++) {
        int t;
        cin >> t;
        s.push_back(t);
        cnt.push_back(0);
    }
    int id = 1, cnt_t = n;;
    while (cnt_t > 0) {
        for (int i = 0; i < n; i++) {
            if (!vis.count(i) && a[i][cnt[i]].size() < 10) {
                a[i][cnt[i]].push_back(id);
                // 剩一条队伍的时候,保持间隔
                if (cnt_t == 1) {
                    id += 2;
                } else {
                    id++;
                }
            }
            // i校的本条队伍已经完成分配
            if (cnt[i] < s[i] && a[i][cnt[i]].size() == 10) {
                cnt[i]++;
            }
            // i校的队伍已经完成分配
            if (!vis.count(i) && cnt[i] == s[i]) {
                cnt_t--;
                vis.insert(i);
            }
        }
    }
    for (int i = 0; i < n; i++) {
        cout << "#" << i + 1 << endl;
        for (int j = 0; j < s[i]; j++) {
            for (int k = 0; k < 10; k++) {
                if (k != 0) cout << " ";
                cout << a[i][j][k];
            }
            cout <<endl;
        }
    }
    return 0;
}

L1-050 倒数第N个字符串(15分)

这道题其实就相当于数的进制处理,挺有意思的。

#include <bits/stdc++.h>
using namespace std;
int n, m, sum = 1;
stack<int> s;
int main()
{

    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        sum *= 26;
    }
    sum -= m;
    while (sum != 0) {
        s.push(sum % 26);
        sum /= 26;
    }
    // 如果不足 n 位,则用零补齐
    int len = s.size();
    for (int i = 0; i < n - len; i++) {
        s.push(0);
    }
    for (int i = 0; i < n; i++) {
        printf("%c", s.top() + 'a');
        s.pop();
    }
    return 0;
}

L1-051 打折(5分)

#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
    int n, m;
    cin >> n >> m;
    printf("%.2f", n * (m * 0.1));
    return 0;
}

L1-052 2018我们要赢(5分)

#include <iostream>
int main()
{
    printf("2018\nwo3 men2 yao4 ying2 !\n");
    return 0;
}

L1-053 电子汪(10分)

#include <iostream>
using namespace std;
int main()
{
    int a, b;
    cin >> a >> b;
    for (int i = 0; i < a + b; i++) {
        cout << "Wang!";
    }
    return 0;
}

L1-054 福到了(15分)

#include <bits/stdc++.h>
using namespace std;
int n;
string str[105], ch;

int main()
{
    cin >> ch >> n;
    getchar();
    for (int i = 0; i < n; i++) {
        getline(cin, str[i]);
    }
    // 检验是否需要倒转
    int flag = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (str[i][j] != str[n-i-1][n-j-1]) {
                flag = 1;
                break;
            }
        }
        if (flag) {
            break;
        }
    }
    if (!flag) {
        cout << "bu yong dao le" << endl;
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (str[n-i-1][n-j-1] != ' ') {
                cout << ch;
            } else {
                cout << str[n-i-1][n-j-1];
            }
        }
        cout << endl;
    }
    return 0;
}

L1-055 谁是赢家(10分)

#include <iostream>
using namespace std;

int main()
{
    int a, b;
    int zore = 0, one = 0;
    cin >> a >> b;
    for (int i = 0; i < 3; i++) {
        int t;
        cin >> t;
        if (t == 0) {
            zore++;
        } else {
            one++;
        }
    }
    if (zore == 3 || (a + zore > b + one && zore > 0)) {
        cout << "The winner is a: " << a <<" + " << zore << endl;
    } else if (one == 3 || (b + one > a + zore && one > 0)) {
        cout << "The winner is b: " << b <<" + " << one << endl;
    }
    return 0;
}

L1-056 猜数字 (20分)

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

int main()
{
    int n, num;
    double avg = 0;
    string name;
    vector<pair<string, int> > p;

    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> name >> num;
        p.push_back(make_pair(name, num));
        avg += num;
    }
    avg /= n * 2;
    int minn = 100;
    for (int i = 0; i < n; i++) {
        if (fabs(avg - p[i].second) < minn) {
            name = p[i].first;
            minn = fabs(avg - p[i].second);
        }
    }
    printf("%.0f ", avg);
    cout << name << endl;
    return 0;
}

L1-057 PTA使我精神焕发(5分)

#include <iostream>
using namespace std;
int main()
{
    cout << "PTA shi3 wo3 jing1 shen2 huan4 fa1 !" << endl;
    return 0;
}

L1-058 6翻了(15分)

当对原字符串进行处理相对麻烦的时候,可以考虑在一个新开的字符串中进行操作。

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;

int main()
{
    string str, ans = "";
    getline(cin, str);
    int len = 0, start = 0;
    for (int i = 0; i < str.length(); i++) {
        if (str[i] == '6') {
            int j = i;
            while (j < str.length() && str[j] == '6') j++;
            if (j - i > 9) {
                ans += "27";
            } else if (j - i > 3) {
                ans += "9";
            } else {
                ans += str.substr(i, j - i);
            }
            i = j - 1;
        } else {
            ans += str[i];
        }
    }
    cout << ans << endl;
    return 0;
}

L1-059 敲笨钟 (20分)

合理的使用 STL 中的函数,可以帮助我们降低题目的难度。

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int n;
string str;

int main()
{
    cin >> n;
    getchar();
    for (int i = 0; i < n; i++) {
        getline(cin, str);
        int idx1 = str.find(','); //find 函数返回的是该字符的下标
        int idx2 = str.find('.');
        cout << idx1 << " " << idx2 << endl;
       	// 注意判断范围,不要造成段错误
        if (idx1 - 3 >= 0 && str.substr(idx1 - 3, 3) == "ong" && str.substr(idx2 - 3, 3) == "ong") {
            int j = idx2, cnt = 0;
            while (j > idx1 && cnt < 3) {
                if (str[j] == ' ') {
                    cnt++;
                }
                j--;
            }
            // 截掉后面三个字,再补上" qiao ben zhong."
            cout << str.substr(0, j + 1) << " qiao ben zhong." << endl;
        } else {
            cout << "Skipped" << endl;
        }
    }
    return 0;
}

L1-060 心理阴影面积(5分)

#include <iostream>
using namespace std;
int main()
{
    int x, y;
    cin >> x >> y;
    // 大三角形面积 - 小正方形面积 - 下面小三角形的面积 - 右边小三角形的面积
    cout << 5000 - (100 - x) * y - (x * y / 2) - (100 - x) * (100 - y) / 2;
    return 0;
}

L1-061 新胖子公式 (10分)

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int main()
{
    double a, b;
    cin >> a >> b;
    double ans = a / (b * b);
    printf("%.1lf\n", ans);
    if (ans > 25) {
        cout << "PANG" << endl;
    } else {
        cout << "Hai Xing" << endl;
    }
    return 0;
}

L1-062 幸运彩票(15分)

#include <iostream>
#include <string>
using namespace std;

int getNum(char ch) {
    return ch - '0';
}

int main()
{
    int n;
    string str;
    cin >> n;
    while (n--) {
        cin >> str;
        int a = getNum(str[0]) + getNum(str[1]) + getNum(str[2]);
        int b = getNum(str[3]) + getNum(str[4]) + getNum(str[5]);
        if (a == b) {
            cout << "You are lucky!" << endl;
        } else {
            cout << "Wish you good luck." << endl;
        }
    }
    return 0;
}

L1-063 吃鱼还是吃肉(10分)

做种题的时候要冷静,看清楚的条件,理清楚之后再开始写。基本上只要写男性的情况就可以了,女性的情形只要将前面写好的复制过去,然后根据题意改一下条件句就可以了。

#include <iostream>
using namespace std;

int main()
{
    int n;
    cin >> n;
    while (n--) {
        int s, h, w;
        cin >> s >> h >> w;
        if (s == 0) {
            if (h == 129) {
                cout << "wan mei!";
            } else if (h < 129) {
                cout << "duo chi yu!";
            } else {
                cout << "ni li hai!";
            }
            if (w == 25) {
                cout << " wan mei!" << endl;
            } else if (w < 25) {
                cout << " duo chi rou!" << endl;
            } else {
                cout << " shao chi rou!" << endl;
            }
        } else {
            if (h == 130) {
                cout << "wan mei!";
            } else if (h < 130) {
                cout << "duo chi yu!";
            } else {
                cout << "ni li hai!";
            }
            if (w == 27) {
                cout << " wan mei!" << endl;
            } else if (w < 27) {
                cout << " duo chi rou!" << endl;
            } else {
                cout << " shao chi rou!" << endl;
            }
        }
    }
    return 0;
}

2020年11月19日 01:51:36 更新,完成至 L1-031
2020年11月20日 02:10:25 更新,完成至 L1-042
2020年11月22日 19:03:24 更新,完成至 L1-063

2022年04月21日 02:16:23 更新,寄语:

“相信能看到这里的,都是对算法有一定热爱的人,我建了一个叫做「算法小学徒」公众号,主要输出算法相关的内容,有兴趣的朋友可以来一起玩哦~”

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐