团体程序设计天梯赛-练习集

  • Post author:
  • Post category:其他


L1-001 Hello World

#include <iostream>
using namespace std;

int main()
{
    cout << "Hello World!";
}

L1-002 打印沙漏

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

int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    
    int n; cin >> n;
    char ch; cin >> ch;
    
    int h = (int)sqrt((n + 1) / 2);
    
    for (int i = h; i; i -- )
    {
        for (int j = 1; j <= h - i; j ++ ) cout << ' ';
        for (int j = 1; j <= 2 * i - 1; j ++ ) cout << ch;
        cout << endl;
    }
    
    for (int i = 2; i <= h; i ++ )
    {
        for (int j = 1; j <= h - i; j ++ ) cout << ' ';
        for (int j = 1; j <= 2 * i - 1; j ++ ) cout << ch;
        cout << endl;
    }
    
    cout << n - 2 * h * h + 1 << endl;
}

L1-003 个位数统计

#include <iostream>
using namespace std;

int cnt[15];

int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    
    string s; cin >> s;
    int n = (int)s.size();
    
    for (int i = 0; i < n; i ++ )
    {
        cnt[s[i] - '0'] ++ ;
    }
    
    for (int i = 0; i <= 9; i ++ )
    {
        if (cnt[i])
            cout << i << ':' << cnt[i] << endl;
    }
}

L1-004 计算摄氏温度

#include <iostream>
using namespace std;

int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    
    int f; cin >> f;
    cout << "Celsius = ";
    cout << 5 * (f - 32) / 9;
}

L1-005 考试座位号

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

unordered_map<int, string> m1;
unordered_map<int, int> m2;

int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    
    int n; cin >> n;
    
    string s1; int sit1, sit2;
    for (int i = 0; i < n; i ++ )
    {
        cin >> s1 >> sit1 >> sit2;
        m1[sit1] = s1;
        m2[sit1] = sit2;
    }
    
    int m; cin >> m;
    while (m -- )
    {
        cin >> sit1;
        cout << m1[sit1] << ' ' << m2[sit1] << endl;
    }
}

L1-006 连续因子

  • 枚举开始的位置和长度
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    
    int start = 0, len = 0;
    int n; cin >> n;
    int half = sqrt(n) + 1;
    
    for (int i = 2; i <= half; i ++ )
    {
        int tmp = 1;
        for (int j = i; j <= n; j ++ )
        {
            tmp *= j;
            if (n % tmp != 0)
            {
                if (j - 1 - i + 1 > len)
                {
                    len = j - 1 - i + 1;
                    start = i;
                }
                break;
            }
        }
    }
    
    if (start == 0) cout << 1 << '\n' << n << '\n';
    else
    {
        cout << len << '\n';
        for (int i = start; i <= start + len - 1; i ++ )
        {
            cout << i;
            if (i != start + len - 1) cout << '*';
        }
    }
}

L1-007 念数字

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

string spell[] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};

int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    
    string s; cin >> s;
    int n = (int)s.size();
    
    for (int i = 0; i < n; i ++ )
    {
        if (s[i] == '-') cout << "fu";
        else cout << spell[s[i] - '0'];
        if (i != n - 1) cout << ' ';
    }
}

L1-008 求整数段和

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

int main()
{
//    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    
    int a, b; scanf("%d%d", &a, &b);
    int sum = 0;
    int cnt = 0;
    
    for (int i = a; i <= b; i ++ )
    {
        sum += i;
        printf("%5d", i);
        cnt ++ ;
        if (cnt % 5 == 0)
        {
            puts("");
        }
    }
    
    if (cnt % 5 != 0) puts("");
    printf("Sum = %d", sum);
}

L1-009 N个数求和

  • -10/3 输出应该是 -3 -1/3
  • 因此,比较的是分子的

    绝对值

    和分母大小
#include <iostream>
#include <cmath>
using namespace std;

int gcd(int a, int b)
{
    return b ? gcd(b, a % b) : a;
}

int main()
{
//    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

    int n; scanf("%d", &n);
    int a, b, c, d;
    scanf("%d/%d", &b, &a);

    int t = abs(gcd(a, b));
    a /= t, b /= t;

    for (int i = 1; i < n; i ++ )
    {
        scanf("%d/%d", &d, &c);
        int t = abs(gcd(c, d));
        d /= t, c /= t;

        b = b * c + a * d;
        a = a * c;
        t = abs(gcd(a, b));
        a /= t, b /= t;
    }

    if (b == 0) puts("0");
    else if (abs(b) < a) printf("%d/%d", b, a);
    else
    {
        if (abs(b) % a == 0) printf("%d", b / a);
        else
        {
            printf("%d ", b / a);
            b -= (b / a * a);
            printf("%d/%d", b, a);
        }
    }
}

L1-010 比较大小

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

typedef long long ll;

ll a[5];

int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

    for (int i = 0; i < 3; i ++ )
        cin >> a[i];
    
    sort(a, a + 3);
    
    printf("%lld->%lld->%lld", a[0], a[1], a[2]);
}

L1-011 A-B

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

int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

    unordered_set<char> se;
    string a, b;
    getline(cin, a);
    getline(cin, b);
    
    for (int i = 0; i < b.size(); i ++ )
        se.insert(b[i]);
    
    for (int i = 0; i < a.size(); i ++ )
        if (se.find(a[i]) == se.end())
            cout << a[i];
}

L1-012 计算指数

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

typedef long long ll;

ll qmi(ll a, ll b)
{
    ll res = 1;
    while (b)
    {
        if (b & 1) res = res * a;
        a = a * a;
        b >>= 1;
    }
    return res;
}

int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

    ll n; cin >> n;
    printf("2^%lld = %lld", n, qmi(2, n));
}

L1-013 计算阶乘和

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

typedef long long ll;

ll fi(ll x)
{
    ll res = 1;
    for (ll i = 1; i <= x; i ++ )
        res *= i;
    
    return res;
}

int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

    ll n; cin >> n;
    ll sum = 0;
    for (ll i = 1; i <= n; i ++ )
    {
        sum += fi(i);
    }
    
    printf("%lld", sum);
}

L1-014 简单题

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

typedef long long ll;

int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

    printf("This is a simple problem.");
}

L1-015 跟奥巴马一起画方块

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

int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

    int c; cin >> c;
    int r = round(c / 2.0);
    
    char ch; cin >> ch;
    for (int i = 1; i <= r; i ++ )
    {
        for (int j = 1; j <= c; j ++ )
            cout << ch;
        cout << endl;
    }
}

L1-016 查验身份证

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

char jy[] = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
int power[] = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};

int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

    vector<string> res;
    
    int n; cin >> n;
    string s;
    for (int i = 0; i < n; i ++ )
    {
        getline(cin, s);
        int sum = 0;
        
        bool ok = true;
        for (int j = 0; j < s.size() - 1; j ++ )
        {
            if (!(s[j] >= '0' && s[j] <= '9'))
            {
                ok = false;
                break;
            }
            sum += (s[j] - '0') * power[j];
        }
        
        sum %= 11;
        if (jy[sum] != s[s.size() - 1]) ok = false;
        if (!ok) res.push_back(s);
    }
    
    if (res.size() == 0) puts("All passed");
    else
    {
        for (auto i : res)
            cout << i << endl;
    }
}

L1-017 到底有多二

  • 在printf中打印%要写%%
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
//    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

    string s; cin >> s;
    
    bool fu = false;
    bool even = false;
    
    if (s[0] == '-')
    {
        fu = true;
        s = s.substr(1);
    }
    
    if ((s[s.size() - 1] - '0') % 2 == 0) even = true;
    
    int sum = 0;
    for (int i = 0; i < s.size(); i ++ )
        if (s[i] == '2')
            sum ++ ;
    
    double res = sum * 1.0 / (int)s.size();
    if (fu) res *= 1.5;
    if (even) res *= 2;
    
    printf("%.2lf%%", res * 100);
}

L1-018 大笨钟

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

int main()
{
//    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

    int hh, mm;
    scanf("%d:%d", &hh, &mm);
    
    if ((hh <= 11) || (hh == 12 && mm == 0)) printf("Only %02d:%02d.  Too early to Dang.", hh, mm);
    else
    {
        hh -= 12;
        if (mm != 0) hh ++ ;
        for (int i = 1; i <= hh; i ++ )
            cout << "Dang";
    }
}



版权声明:本文为m0_51448653原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。