洛谷官方题单【入门】

【入门1】顺序结构

B2002 Hello,World!

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

int main() {

  cout << "Hello,World!" << endl;

  return 0;
}

B2025 输出字符菱形

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

int main(){

    cout << "  *  " << endl;
    cout << " *** " << endl;
    cout << "*****" << endl;
    cout << " *** " << endl;
    cout << "  *  " << endl;

  return 0;
}

P1001 A+B Problem

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

int main(){

  int a, b;
  cin >> a >> b;
  cout << a + b << endl;

  return 0;
}

B2005 字符三角形

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

int main(){

  char c;
  cin >> c;
  cout << "  " << c << "  " << endl;
  cout << " " << c << c << c << " " << endl;
  cout << c << c << c << c << c << endl;

  return 0;
}

P5703 【深基2.例5】苹果采购

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

int main(){

  int a, b;
  cin >> a >> b;
  cout << a * b << endl;

  return 0;
}

P5704 【深基2.例6】字母转换

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

int main(){

  char c;
  cin >> c;
  cout << (char)(c - 32) << endl;

  return 0;
}

P5705 【深基2.例7】数字反转

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

int main() {

  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  cout << s << endl;

  return 0;
}

P5706 【深基2.例8】再分肥宅水

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

int main() {

  double a, b;
  cin >> a >> b;
  printf("%.3f\n", a / b);
  cout << b * 2 << endl;

  return 0;

}

P5708 【深基2.习2】三角形面积

#include<bits/stdc++.h>

using namespace std;

int main ()
{
    double a, b, c;
    cin >> a >> b >> c;
    double p = (a + b + c) / 2;
    double ans = sqrt(p * (p - a) * (p - b) * (p - c));

    printf("%.1f", ans); 

   return 0;
}

P5707 【深基2.例12】上学迟到

#include<bits/stdc++.h>

using namespace std;

int main () {

    int s, v;
    cin >> s >> v;

    int t = (s + v - 1) / v + 10;
    int n = 24 * 60 + 8 * 60 - t;
    if (n >= 24 * 60) {
        n -= 24 * 60;
    }

    int hr = n / 60;
    int mt = n % 60;
    if (hr < 10 && mt < 10) {
        cout << "0" << hr << ":0" << mt << endl;
    }else if (hr < 10 && mt > 10) {
        cout << "0" << hr << ":" << mt << endl;
    }else if (hr > 10 && mt < 10) {
        cout << hr << ":0" << mt << endl;
    }else {
        cout << hr << ":" << mt << endl;
    }

    return 0;
}

B2029 大象喝水

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

const double pi = 3.14;
const int tv = 20000;

int main () {
    int h, r;
    cin >> h >> r;

    double v = pi * r * r * h;
    int ans = (tv + v - 1) / v;

    cout << ans << endl;

    return 0;
}

P1421 小玉买文具

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

int main () {
    int a, b;
    cin >> a >> b;

    int m = a * 10 + b;
    cout << m / 19 << endl;

    return 0;
}

P1425 小鱼的游泳时间

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

int main () {
    int h1, m1, h2, m2;
    cin >> h1 >> m1 >> h2 >> m2;

    int ans = h2 * 60 + m2 - h1 * 60 - m1;
    cout << ans / 60 << " " << ans % 60 << endl;

    return 0;
}

P3954 [NOIP2017 普及组] 成绩

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

int main () {
    double a, b, c;
    cin >> a >> b >> c;
    cout << a * 0.2 + b * 0.3 + c * 0.5 << endl;

    return 0;
}

【入门2】分支结构

P2433 【深基1-2】小学数学 N 合一

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

int main() {
    int T;
    cin >> T;
    if (T == 1) {
        // 粘贴问题 1 的主函数代码,除了 return 0
        cout << "I love Luogu!";
    } else if (T == 2) {
        // 粘贴问题 2 的主函数代码,除了 return 0
        cout << 2 + 4 << " " << 10 - 2 - 4;
    } else if (T == 3) {
        // 请自行完成问题 3 的代码
        cout << 14 / 4 << endl;
        cout << 14 - 14 % 4 << endl;
        cout << 14 % 4 << endl;     

    } else if (T == 4) {
        // 请自行完成问题 4 的代码
        cout << 500.0 / 3 << endl;
    } else if (T == 5) {
        // 请自行完成问题 5 的代码
        cout << (260 + 220) / (12 + 20) << endl;
    } else if (T == 6) {
        // 请自行完成问题 6 的代码
        double a = 6, b = 9;
        double c = sqrt(a * a + b * b);
        cout << c << endl;

    } else if (T == 7) {
        // 请自行完成问题 7 的代码
        cout << 110 << endl;
        cout << 110 - 20 << endl;
        cout << 0 << endl;
    } else if (T == 8) {
        // 请自行完成问题 8 的代码
        int r = 5;
        double pi = 3.141593;
        cout << 2 * pi * r << endl;
        cout << r * r * pi << endl;
        cout << 4.0 / 3 * pi * r * r * r << endl;

    } else if (T == 9) {
        // 请自行完成问题 9 的代码
        cout << (((1 + 1) * 2 + 1) * 2 + 1) * 2 << endl;
    } else if (T == 10) {
        // 请自行完成问题 10 的代码
        cout << 9 << endl;

    } else if (T == 11) {
        // 请自行完成问题 11 的代码
        cout << 100.0 / (8 - 5) << endl;

    } else if (T == 12) {
        // 请自行完成问题 12 的代码
        cout << 'M' - 'A' + 1 << endl;
        cout << char('A' - 1 + 18) << endl;

    } else if (T == 13) {
        // 请自行完成问题 13 的代码
        int r1 = 4, r2 = 10;
        double pi = 3.141593;
        double v = 4.0 / 3 * pi * r1 * r1 * r1 + 4.0 / 3 * pi * r2 * r2 * r2;
        v = pow(v, 1.0 / 3);  //因为C++没有开立方根的函数,求它的三分之一次方可以达到同样的效果。
        printf("%.0lf\n",v);

    } else if (T == 14) {
        // 请自行完成问题 14 的代码
        cout << 50 << endl;
    }
    return 0;
}

P5709 【深基2.习6】Apples Prologue / 苹果和虫子

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

int main() {

    int m, t, s;
    cin >> m >> t >> s;
    if (t == 0) cout << 0 << endl;
    else cout << max(0, m - ((s + t - 1) / t)) << endl;

    return 0;

}

P5710 【深基3.例2】数的性质

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

int main() {

    // f1 是小A态度,0 为不喜欢, 1 为喜欢
    // 同理,f2 是 UIM, f3 是八尾勇,f4 是正妹 
    int f1 = 0, f2 = 0, f3 = 0, f4 = 0;

    // t1 表示是否满足条件 1, t1 = 1 表示满足,0 为不满足 
    int t1 = 0, t2 = 0;
    int x;
    cin >> x;
    if (x % 2 == 0) t1 = 1;
    if (x >= 4 && x <= 12) t2 = 1;

    if (t1 && t2) f1 = 1;
    if (t1 || t2) f2 = 1;
    if ((t1 == 1 && t2 == 0) || (t1 == 0 && t2 == 1)) f3 = 1;
    if (t1 == 0 && t2 == 0) f4 = 1;
    cout << f1 << " " << f2 << " " << f3 << " " << f4 << endl; 

    return 0;

}

P5711 【深基3.例3】闰年判断

#include<bits/stdc++.h>
using namespace std;
// 判断闰年方法:是4的倍数但不是100的倍数,或者是400的倍数。
int is_run(int y) {
    if ((y % 4 == 0) && (y % 100 != 0) || (y % 400 == 0)) return 1;
    return 0;
}
int main() {

    int y;
    cin >> y;
    cout << is_run(y) << endl;

    return 0;

}

P5712 【深基3.例4】Apples

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

int main() {
    int x;
    cin >> x;
    if (x == 1 || x == 0) {
        printf("Today, I ate %d apple.", x);
    }else {
        printf("Today, I ate %d apples.", x);
    }
    return 0;

}

P5713 【深基3.例5】洛谷团队系统

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

int main() {
    // n * 5 > n * 3 + 11
    // 则本地,解得 n = 6 
    int n;
    cin >> n;
    if (n >= 6) {
        cout << "Luogu" << endl;
    }else {
        cout << "Local" << endl;
    }
    return 0;

}

P5714 【深基3.例7】肥胖问题

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

int main() {
    double m, h, bmi;
    cin >> m >> h;

    bmi = m / (h * h);
    if (bmi < 18.5) cout << "Underweight" << endl;
    else if (bmi < 24) cout << "Normal" << endl;
    else cout << bmi << endl << "Overweight" << endl;

    return 0;

}

P5715 【深基3.例8】三位数排序

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

int main() {
    int a, b, c;
    cin >> a >> b >> c;
    if (a > b) swap(a, b);
    if (a > c) swap(a, c);
    if (b > c) swap(b, c);
    // swap 是交换函数,将 a 和 b 的值交换 
    cout << a << " " << b << " " << c << endl;

    return 0;

}

P5716 【深基3.例9】月份天数

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

int days[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

bool is_run(int y) {
    if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0)) return true;
    return false;
}
int main() {
    int y, m;
    cin >> y >> m;
    if (is_run(y)) days[2] = 29;
    else days[2] = 28;
    cout << days[m] << endl;

    return 0;

}

P1085 [NOIP2004 普及组] 不高兴的津津

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

int main() {

    int a, b;
    int ans1 = 0, ans2 = 0;
    for (int i = 1; i <= 7; ++ i) {
        cin >> a >> b;
        int t = a + b;
        if (t > 8 && t > ans2) {
            ans1 = i;
            ans2 = t;
        }
    }
    cout << ans1 << endl;

    return 0;

}

P1909 [NOIP2016 普及组] 买铅笔

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

int main() {

    int n;
    cin >> n;
    int ans = 1e9 + 5;

    for (int i = 0; i < 3; ++ i) {
        int a, b;
        cin >> a >> b;
        ans = min(ans, ((n + a - 1) / a) * b);
    }
    cout << ans << endl;

    return 0;

}

P5717 【深基3.习8】三角形分类

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

int main() {
    int a, b, c;
    cin >> a >> b >> c;

    if (a > b) swap(a, b);
    if (a > c) swap(a, c);
    if (b > c) swap(b, c);

    if (a + b <= c) cout << "Not triangle" << endl;
    if (a + b > c && a * a + b * b == c * c) cout << "Right triangle" << endl;
    if (a + b > c && a * a + b * b > c * c) cout << "Acute triangle" << endl;
    if (a + b > c && a * a + b * b < c * c) cout << "Obtuse triangle" << endl;
    if (a + b > c && (a == b || b == c)) cout << "Isosceles triangle" << endl;
    if (a + b > c && a == b && a == c && b == c) cout << "Equilateral triangle" << endl;

    return 0;

}

P1422 小玉家的电费

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

int main() {

    double ans = 0;
    int x;
    cin >> x;

    ans += min(x, 150) * 0.4463;
    x -= min(x, 150);

    ans += min(x, 250) * 0.4663;
    x -= min(x, 250);

    ans += x * 0.5663;

    printf("%.1lf", ans);

    return 0;

}

P1424 小鱼的航程(改进版)

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

int main() {
    int x, n;
    cin >> x >> n;

    int ans = 0;
    if (n >= 8 - x) {
        n -= 8 - x;
        ans += max(0, 6 - x);

        ans += n / 7 * 5;
        ans += min(n % 7, 5);

    }else {
        ans = min(5, n);
    }

    cout << ans * 250 << endl;

    return 0;

}

P1888 三角函数

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

// 求 a, b 最大公约数 
int gcd(int a, int b) {
    return b ? gcd(b, a % b) : a;
}
int main() {

    int a, b, c;
    cin >> a >> b >> c;

    if (a > b) swap(a, b);
    if (a > c) swap(a, c);
    if (b > c) swap(b, c);

    cout << a / gcd(a, c) << "/" << c / gcd(a, c) << endl;

    return 0;

}

P1046 [NOIP2005 普及组] 陶陶摘苹果

#include<bits/stdc++.h>
using namespace std;
const int N = 15;
int a[N];
int main() {

    for (int i = 0; i < 10; ++ i) {
        cin >> a[i];
    }
    int x;
    cin >> x;
    x += 30;
    int ans = 0;
    for (int i = 0; i < 10; ++ i) {
        if (a[i] <= x) ++ ans;   
    }
    cout << ans << endl;

    return 0;

}

P4414 [COCI2006-2007#2] ABC

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

int a[5];
int main() {

    cin >> a[0] >> a[1] >> a[2];
    if (a[0] > a[1]) swap(a[0], a[1]);
    if (a[0] > a[2]) swap(a[0], a[2]);
    if (a[1] > a[2]) swap(a[1], a[2]); 

    string s;
    cin >> s;
    for (int i = 0; i < s.size(); ++ i) {
        int t = s[i] - 'A';
        cout << a[t] << " ";  
    }   
    cout << endl;

    return 0;

}

P1055 [NOIP2008 普及组] ISBN 号码

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

string p = "0123456789X";
int main() {
    string s;
    cin >> s;
    int ans = 0, t = 1, sz = s.size();
    for (int i = 0; i < sz - 1; ++ i) {
        if (s[i] == '-') continue;
        ans +=  (s[i] - '0') * (t ++);
    }
    ans %= 11;
    if (p[ans] == s[sz - 1]) cout << "Right" << endl;
    else {
        s[sz - 1] = p[ans];
        cout << s << endl;
    }

    return 0;

}

【入门3】循环结构

P5718 【深基4.例2】找最小值

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

int main() {
    int n, t;
    cin >> n;

    int x = 1001;
    for (int i = 0; i < n; ++ i) {
        cin >> t;
        if (x > t) {
            x = t;
        }

    }
    cout << x << endl;

    return 0;
}

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

int main() {
    int n, t;
    cin >> n;
    int x = 1001;
    while (-- n) {
        cin >> t;
        if (x >= t) x = t;
    }
    cout << x << endl;

    return 0;
}

P5719 【深基4.例3】分类平均

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

int main() {
    int n, k;
    cin >> n >> k;
    double sum1 = 0, sum2 = 0, cnt1 = 0, cnt2 = 0;
    for (int i = 1; i <= n; ++ i) {
        if (i % k == 0) {
            sum1 += i;
            ++ cnt1;
        }else {
            sum2 += i;
            ++ cnt2;
        }

    }
    printf("%.1lf %.1lf", sum1 / cnt1, sum2 / cnt2);

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

int main() {
    int n, k;
    cin >> n >> k;
    int cnt1 = n / k;
    int cnt2 = n - cnt1;
    double sum1 = cnt1 * (2 * k + k * (cnt1 - 1)) / 2;
    double sum2 = n * (1 + n) / 2 - sum1;

    printf("%.1lf %.1lf", sum1 / cnt1, sum2 / cnt2);

    return 0;
}

P5720 【深基4.例4】一尺之棰

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

int main() {
    int x;
    cin >> x;
    int ans = 1;
    while (x != 1) {
        x /= 2;
        ++ ans;
    }
    cout << ans << endl;

    return 0;

}

P5721 【深基4.例6】数字直角三角形

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

int main() {
    int n;
    cin >> n;
    int tot = 1;
    for (int i = n; i >= 1; -- i) {
        for (int j = 1; j <= i; ++ j) {
            if (tot < 10) cout << "0" << tot;
            else cout << tot;
            ++ tot;
        }
        cout << endl;
    }

    return 0;

}

P1009 [NOIP1998 普及组] 阶乘之和

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

vector<int> add(vector<int> a, vector<int> b) {
    vector<int> ret;
    int len = min((int)a.size(), (int)b.size());
    int at = 0, i = 0;
    for (; i < len; ++ i) {
        int t = a[i] + b[i] + at;
        at = t / 10;
        ret.push_back(t % 10);
    } 
    while (i < (int)a.size()) {
        int t = a[i] + at;
        at = t / 10;
        ret.push_back(t % 10);
        ++ i;
    }
    while (i < (int)b.size()) {
        int t = b[i] + at;
        at = t / 10;
        ret.push_back(t % 10);
        ++ i;
    }
    while (at) {
        ret.push_back(at % 10);
        at /= 10;
    }
    return ret; 
}
vector<int> mul(vector<int> a, vector<int> b) {
    vector<int> ret(a.size() + b.size() + 5, 0);
    for (int i = 0; i < a.size(); ++ i) {
        for (int j = 0; j < b.size(); ++ j) {
            ret[i + j] += a[i] * b[j];
        }
    }   

    for (int i = 0; i + 1 < ret.size(); ++ i) {
        ret[i + 1] += ret[i] / 10;
        ret[i] %= 10;
    } 

    while (ret.size() > 1 && ret.back() == 0) ret.pop_back();

    return ret;

}
vector<int> get(int x) {
    vector<int> ans;
    if (x == 0) {
        ans.push_back(0);
        return ans; 
    }

    while (x) {
        ans.push_back(x % 10);
        x /= 10;
    }
    return ans;
}

vector<int> jie(int n) {
    if (n == 1) return get(1);
    return mul(get(n), jie(n - 1)); 
}

void printV(vector<int> t) {
    for (int i = t.size() - 1; i >= 0; -- i) {
        cout << t[i];
    }
    cout << endl;
}
int main() {
    int n;
    cin >> n;
    vector<int> ans = get(0);

    for (int i = 1; i <= n; ++ i) {
        ans = add(ans, jie(i));
    }

    printV(ans);

    return 0;
}

P1980 [NOIP2013 普及组] 计数问题

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

int main() {
    int n, x;
    cin >> n >> x;
    int ans = 0;
    for (int i = 1; i <= n; ++ i) {
        int t = i;
        while (t) {
            if (x == t % 10) ++ ans;
            t /= 10;
        }
    }
    cout << ans << endl;

    return 0;
}

P1035 [NOIP2002 普及组] 级数求和

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

int main() {

    int n;
    cin >> n;
    double sum = 0;
    int ans = 0;
    while (sum <= n) {
        ++ ans;
        sum += 1.0 / ans; 
    }
    cout << ans << endl;

    return 0;
}

P2669 [NOIP2015 普及组] 金币

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

int main() {

    int n;
    cin >> n;
    int ans = 0, c = 1;

    for (int i = 1; i <= n; i += c ++) {
        for (int j = i; j <= min(n, i + c - 1); ++ j) ans += c;

    }
    cout << ans << endl;

    return 0;
}

P5722 【深基4.例11】数列求和

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

int main() {
    int n;
    cin >> n;
    int sum = 0;
    for (int i = 1; i <= n; ++ i) sum += i;
    cout << sum << endl;

    return 0;
}

P5723 【深基4.例13】质数口袋

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

bool is_prime(int n) {
    if (n <= 1) return false;
    for (int i = 2; i <= sqrt(n); ++ i) {
        if (n % i == 0) return false;
    }
    return true;
}
int main() {
    int n;
    cin >> n;
    int sum = 0, cnt = 0;
    vector<int> v;
    for (int i = 2; ; ++ i) {
        if (is_prime(i)) {
            if (sum + i <= n) {
                sum += i;
                v.push_back(i);
                ++ cnt;
            }else break;
        }
    }
    for (int i = 0; i < v.size(); ++ i) {
        cout << v[i] << endl;
    }
    cout << cnt << endl;

    return 0;
}

P1217 [USACO1.5] 回文质数 Prime Palindromes

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

const int MAXN = 1e7 + 5;
bool isnp[MAXN];
vector<int> primes; // 质数表
void init(int n)
{
    for (int i = 2; i <= n; i++)
    {
        if (!isnp[i])
            primes.push_back(i);
        for (int j = 0; j < primes.size(); ++ j)
        {   
            int p = primes[j];
            if (p * i > n)
                break;
            isnp[p * i] = 1;
            if (i % p == 0)
                break;
        }
    }
}
bool check(int x) {
    string s;
    while (x) {
        char c = '0' + (x % 10);
        x /= 10;
        s += c;
    }
    for (int i = 0, j = s.size() - 1; i <= j; ++ i, -- j) {
        if (s[i] != s[j]) return false;
    }
    return true;
}
int main() {
    init(10000000);
    vector<int> ans;
    int a, b;
    cin >> a >> b;
    for (int i = 0; i < primes.size(); ++ i) {
        int v = primes[i];
        if (v >= a && v <= b && check(v)) ans.push_back(v);
    } 
    for (int i = 0; i < ans.size(); ++ i) {
        cout << ans[i] << endl;
    }

    return 0;
}

P1423 小玉在游泳

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

int main() {
    double n, sum = 0, step = 2;
    int cnt = 0;
    cin >> n;

    while (sum < n) {
        sum += step;
        ++ cnt;
        step *= 0.98;
    }
    cout << cnt << endl;

    return 0;
}

P1307 [NOIP2011 普及组] 数字反转

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

int main() {
    int s = 0, n;
    cin >> n;
    while (n) {
        s = s * 10 + n % 10;
        n /= 10;
    }
    cout << s << endl;

    return 0;
}

P1720 月落乌啼算钱(斐波那契数列)

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

int main() {
    int n;
    cin >> n;
    vector<double> f(n + 5);
    f[1] = 1, f[2] = 1;
    for (int i = 3; i <= n; ++ i) {
        f[i] = f[i - 1] + f[i - 2];
    }
    printf("%.2lf", f[n]);

    return 0;
}

P5724 【深基4.习5】求极差 / 最大跨度值

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

int main() {
    int n;
    cin >> n;
    int mx = 0, mn = 1005;
    for (int i = 0; i < n; ++ i) {
        int x;
        cin >> x;
        mx = max(mx, x);
        mn = min(mn, x);
    }
    cout << mx - mn << endl;

    return 0;
}

P1420 最长连号

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

int main() {
    int n;
    cin >> n;
    int ans = 0, last = -1, cur = 0;
    for (int i = 0; i < n; ++ i) {
        int x;
        cin >> x;
        if (x == last + 1) ++ cur;
        else cur = 1;       
        ans = max(ans, cur);
        last = x;
    }
    cout << ans << endl;

    return 0;
}

P1075 [NOIP2012 普及组] 质因数分解

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

int main() {
    int n;
    cin >> n;
    for (int i = 2; i <= n; ++ i) {
        if (n % i == 0) {
            cout << n / i << endl;
            break;
        }
    }
    return 0;
}

P5725 【深基4.习8】求三角形

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

int main() {
    int n;
    cin >> n;
    int tot = 1;
    for (int i = 0; i < n; ++ i) {
        for (int j = 0; j < n; ++ j) {
            if (tot < 10) {
                cout << "0" << char('0' + tot);
            }else cout << tot;
            ++ tot;
        }
        cout << endl;
    }
    cout << endl;
    tot = 1;
    for (int i = 0; i < n; ++ i) {
        for (int j = 0; j < n; ++ j) {
            if (j < n - i - 1) {
                cout << "  ";
                continue;
            }
            if (tot < 10) {
                cout << "0" << char('0' + tot);
            }else cout << tot;
            ++ tot;
        }
        cout << endl;
    }

    return 0;
}

P5726 【深基4.习9】打分

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

int main() {
    vector<int> v;
    int n;
    cin >> n;
    for (int i = 0; i < n; ++ i) {
        int t;
        cin >> t;
        v.push_back(t);
    }
    sort(v.begin(), v.end());
    double sum = 0;
    for (int i = 1; i < n - 1; ++ i) sum += v[i];
    printf("%.2lf", sum / (n - 2));

    return 0;
}

P4956 [COCI2017-2018#6] Davor

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

int main() {
    int N;
    cin >> N;
    printf("%ld\n%ld\n", N / 364L - 3L * max(1L, (N - 36400L) % 1092L ? (N - 36400L) / 1092L + 1L : (N - 36400L) / 1092L), max(1L, (N - 36400L) % 1092L ? (N - 36400L) / 1092L + 1L : (N - 36400L) / 1092L));

    return 0;
}

P1089 [NOIP2004 提高组] 津津的储蓄计划

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

int main() {
    int sum = 0, cur = 0;
    vector<int> v;
    for (int i = 0; i < 12; ++ i) {
        int x;
        cin >> x;
        v.push_back(x);
    }   

    for (int i = 0; i < 12; ++ i) {
        cur += 300;
        cur -= v[i];

        if (cur < 0) {
            cout << "-" << i + 1 << endl;
            return 0;
        }
        int t = max(0, cur / 100) * 100;
        cur -= t;
        sum += t;
    }
    cout << sum * 1.2 + cur << endl;

    return 0;
}

【入门4】数组

P1428 小鱼比可爱

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

const int N = 105;
int a[N], ans[N];
int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; ++ i) {
        cin >> a[i];
        for (int j = i - 1; j >= 0; -- j) {
            if (a[j] < a[i]) {
                ++ ans[i];
            }
        }
    }
    for (int i = 0; i < n; ++ i) {
        cout << ans[i] << " ";    
    }

    return 0;
}

P1427 小鱼的数字游戏

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

const int N = 105;
int a[N];
int main() {

    int i = 0;
    while (1) {
        cin >> a[i];
        if (a[i] == 0) break;
        ++ i;
    }
    while (-- i >= 0) {
        cout << a[i] << " ";
    }

    return 0;
}

P5727 【深基5.例3】冰雹猜想

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

int main() {

    vector<int> v;
    int n;
    cin >> n;
    v.push_back(n);
    while (n != 1) {
        if (n & 1) {
            n = n * 3 + 1;
            v.push_back(n);
        }else {
            n /= 2;
            v.push_back(n);
        }
    }   
    for (int i = v.size() - 1; i >= 0; -- i) {
        cout << v[i] << " ";
    }

    return 0;
}

P1047 [NOIP2005 普及组] 校门外的树

#include<bits/stdc++.h>
using namespace std;
const int N = 1e4 + 5;

int a[N];
int main() {

    int n, m;
    cin >> n >> m;
    while (m -- ) {
        int l, r;
        cin >> l >> r;
        for (int i = l; i <= r; ++ i) {
            a[i] ++;
        }
    }
    int cnt = 0;
    for (int i = 0; i <= n; ++ i) {
        if (a[i] == 0) ++ cnt;
    }
    cout << cnt << endl;

    return 0;
}

P5728 【深基5.例5】旗鼓相当的对手

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

const int N = 1E3 + 5;
int sc[N][3];

int main() {
    int n;
    cin >> n;

    for (int i = 0; i < n; ++ i) {
        for (int j = 0; j < 3; ++ j) {
            cin >> sc[i][j];
        }   
    }

    int ans = 0;
    for (int i = 0; i < n; ++ i) {
        for (int j = i + 1; j < n; ++ j) {
            int sum = 0, f = 1; 
            for (int k = 0; k < 3; ++ k) {
                sum += sc[i][k] - sc[j][k];
                if (abs(sc[i][k] - sc[j][k]) > 5) {
                    f = 0;
                    break;
                }
            }
            if (f && abs(sum) <= 10) ++ ans;
        }
    }
    cout << ans << endl;

    return 0;
}

P5729 【深基5.例7】工艺品制作

#include<bits/stdc++.h>
using namespace std;
const int N = 25;
int a[N][N][N];
int main() {
    int w, x, h;
    cin >> w >> x >> h;
    int q;
    cin >> q;
    while (q --) {
        int x1, x2, y1, y2, z1, z2;
        cin >> x1 >> y1 >> z1 >> x2 >> y2 >> z2;
        for (int i = x1; i <= x2; ++ i) {
            for (int j = y1; j <= y2; ++ j) {
                for (int k = z1; k <= z2; ++ k) {
                    a[i][j][k] = 1;
                }
            }
        }
    }
    int ans = 0;
    for (int i = 1; i <= w; ++ i) {
        for (int j = 1; j <= x; ++ j) {
            for (int k = 1; k <= h; ++ k) {
                if (a[i][j][k] == 0) ++ ans;
            }
        }
    }
    cout << ans << endl;

    return 0;
}

P2550 [AHOI2001] 彩票摇奖


#include<bits/stdc++.h>
using namespace std;
const int N = 35;
int a[N];
int ans[8];
int main() {
    int n;
    cin >> n;
    for (int i = 0; i < 7; ++ i) {
        int x;
        cin >> x;
        a[x] = 1;
    }

    while (n --) {
        int cnt = 0;
        for (int i = 0; i < 7; ++ i) {
            int x;
            cin >> x;
            if (a[x]) ++ cnt;
        }
        ans[cnt] ++;
    }
    for (int i = 7; i >= 1; -- i) cout << ans[i] << " ";

    return 0;
}

P2615 [NOIP2015 提高组] 神奇的幻方


#include<bits/stdc++.h>
using namespace std;
const int N = 45;
int n, a[N][N], x, y;
int main() {
    cin >> n;
    x = 0, y = n / 2;
    for (int i = 1; i <= n * n; ++ i) {
        a[x][y] = i;
        int xt = (x - 1 + n) % n, yt = (y + 1) % n;
        if (!a[xt][yt]) x = xt, y = yt;
        else x = (x + 1) % n;

    }
    for (int i = 0; i < n; ++ i) {
        for (int j = 0; j < n; ++ j) {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}

P5730 【深基5.例10】显示屏

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
char W[10][5][3]=//W[i][j][k]表示第i个数字的第j行的第k列,(手打累死了)
{
    {//0
        'X','X','X',
        'X','.','X',
        'X','.','X',
        'X','.','X',
        'X','X','X',
    },
    {//1
        '.','.','X',
        '.','.','X',
        '.','.','X',
        '.','.','X',
        '.','.','X',
    },
    {//2
        'X','X','X',
        '.','.','X',
        'X','X','X',
        'X','.','.',
        'X','X','X',
    },
    {//3
        'X','X','X',
        '.','.','X',
        'X','X','X',
        '.','.','X',
        'X','X','X',
    },
    {//4
        'X','.','X',
        'X','.','X',
        'X','X','X',
        '.','.','X',
        '.','.','X',
    },
    {//5
        'X','X','X',
        'X','.','.',
        'X','X','X',
        '.','.','X',
        'X','X','X',
    },
    {//6
        'X','X','X',
        'X','.','.',
        'X','X','X',
        'X','.','X',
        'X','X','X',
    },
    {//7
        'X','X','X',
        '.','.','X',
        '.','.','X',
        '.','.','X',
        '.','.','X',
    },
    {//8
        'X','X','X',
        'X','.','X',
        'X','X','X',
        'X','.','X',
        'X','X','X',
    },
    {//9
        'X','X','X',
        'X','.','X',
        'X','X','X',
        '.','.','X',
        'X','X','X',
    }
};
int n;
char s[110];
int main(){
    cin>>n;//输入n
    for(int i=0;i<n;i++){
        cin>>s[i];//输入要打印的字符
    }
    for(int i=0;i<5;i++){//枚举每一行
        for(int j=0;j<n;j++){//枚举每一个数字
            for(int k=0;k<3;k++){//枚举每个数字的列
                cout<<W[s[j]-'0'][i][k];//输出,因为s[j]为字符,所以要减去'0'
            }
            if(j!=n-1) cout<<'.';//如果最后一列,就不需要打印'.'
        }
        cout<<endl;//换行
    }
    return 0;
}

P1554 梦中的统计

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

int c[15];

int main() {
    int m, n;
    cin >> m >> n;
    for (int i = m; i <= n; ++ i) {
        int t = i;
        while (t) {
            c[t % 10] ++;
            t /= 10;
        }
    }
    for (int i = 0; i < 10; ++ i) cout << c[i] << " ";
    cout << endl;

    return 0;
}

P2141 [NOIP2014 普及组] 珠心算测验

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

int a[105];

int main() {

    int n;
    cin >> n;
    for (int i = 0; i < n; ++ i) {
        cin >> a[i];
    }   
    int ans = 0;
    for (int i = 0; i < n; ++ i) {
        int f = 0;
        for (int j = 0; j < n; ++ j) {
            if (i == j) continue;
            if (f) break;
            for (int k = j + 1; k < n; ++ k) {
                if (i == k) continue;
                if (a[i] == a[j] + a[k]) {
                    ++ ans;
                    f = 1;
                    break;  
                }
            }
        }
    }
    cout << ans << endl;

    return 0;
}

P1614 爱与愁的心痛

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

const int N = 1e4;
int a[N];

int main() {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; ++ i) {
        cin >> a[i];
    }
    int ans = 1e6;
    for (int i = m - 1; i < n; ++ i) {
        int sum = 0;
        for (int j = i - m + 1; j <= i; ++ j) {
            sum += a[j];
        }
        ans = min(ans, sum);
    }
    cout << ans << endl;

    return 0;
}

P2911 [USACO08OCT] Bovine Bones G

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

const int N = 100;
int a[N];

int main() {

    int s1, s2, s3;
    cin >> s1 >> s2 >> s3;
    for (int i = 1; i <= s1; ++ i) {
        for (int j = 1; j <= s2; ++ j) {
            for (int k = 1; k <= s3; ++ k) {
                a[i + j + k] ++;
            }
        }
    }
    int cnt = 0, ans = 0;
    for (int i = 1; i <= s1 + s2 + s3; ++ i) {
        if (a[i] > cnt) {
            cnt = a[i];
            ans = i;
        }
    } 
    cout << ans << endl;

    return 0;
}

P1161 开灯

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

const int N = 2e6 + 5;
int a[N];

int main() {

    int n;
    cin >> n;
    while (n --) {
        double x, y;
        cin >> x >> y;
        for (double i = 1; i <= y; ++ i) {
            int id = x * i;
            if (a[id] == 0) a[id] = 1;
            else a[id] = 0; 

        } 
    }   
    for (int i = 1; ; ++ i) {
        if (a[i] == 1) {
            cout << i << endl;
            break;
        }
    }

    return 0;
}

P5731 【深基5.习6】蛇形方阵

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

int a[10][10];
int main() {
    int tot = 1, n;
    cin >> n;
    int i = 0, j = 0;
    a[i][j] = 1;
    while (tot < n * n) {
        while (j + 1 < n && !a[i][j + 1]) a[i][++ j] = ++ tot;
        while (i + 1 < n && !a[i + 1][j]) a[++ i][j] = ++ tot;
        while (j - 1 >= 0 && !a[i][j - 1]) a[i][-- j] = ++ tot;
        while (i - 1 >= 0 && !a[i - 1][j]) a[-- i][j] = ++ tot; 
    }

    for (int i = 0; i < n; ++ i) {
        for (int j = 0; j < n; ++ j) {
            printf("%3d", a[i][j]);
        }
        cout << endl;
    }

    return 0;
}

P5732 【深基5.习7】杨辉三角

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

int a[25][25];

int main() {

    int n;
    cin >> n;
    a[0][0] = 1;
    for (int i = 1; i <= n; ++ i) {
        for (int j = 1; j <= i; ++ j) {
            a[i][j] = a[i - 1][j] + a[i - 1][j - 1];
            cout << a[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}

P1789 【Mc生存】插火把

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

int s[105][105];
int n, m, k;
bool check(int x, int y) {
    if (x < 0 || y < 0 || x >= n || y >= n) return false;
    return true;
}
int main() {

    cin >> n >> m >> k;
    for (int i = 1; i <= m + k; ++ i) {
        int a, b;
        cin >> a >> b;
        -- a, -- b;
        for (int x = -2; x <= 2; ++ x) {
            for (int y = -2; y <= 2; ++ y) {
                if ((i > m || abs(x) + abs(y) <= 2) && check(a + x, b + y)) {
                    s[a + x][b + y] ++;
                }
            }
        }
    }
    int ans = 0;
    for (int i = 0; i < n; ++ i) {
        for (int j = 0; j < n; ++ j) {
            if (s[i][j] == 0) ++ ans;
        }
    }

    cout << ans << endl;

    return 0;
}

P1319 压缩技术

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

int main() {
    int n;
    cin >> n;
    int i = 0, cnt = 0;
    while (cnt < n * n) {
        int x;
        cin >> x;
        while (x --) {
            if (cnt % n == 0 && cnt) cout << endl;
            if (i % 2) cout << 1;
            else cout << 0;
            ++ cnt;
        }
        ++ i;
    }

    return 0;
}

P1320 压缩技术(续集版)

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

int ans[100000];
int main() {

    char c, last = '0';
    int cnt = 0, n = 0;
    while (cin >> c){
        ++ n;
        if (c == last) ans[cnt] ++;
        else {
            last = c;
            ans[++ cnt] ++;
        } 
    }
    cout << sqrt(n) << " ";
    for (int i = 0; i <= cnt; ++ i) {
        cout << ans[i] << " ";
    }
    cout << endl;
    return 0;
}

P1205 [USACO1.2] 方块转换 Transformations

#include<bits/stdc++.h>
using namespace std;
int n;
char a[15][15],b[15][15],c[15][15],d[15][15];
bool work1()
{
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        b[j][n-i+1]=a[i][j];
    }
    for(int i=1;i<=n;i++)
     for(int j=1;j<=n;j++)
     if(b[i][j]!=c[i][j])
     return 0;
     return 1;
}
bool work2()
{
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        b[n-i+1][n-j+1]=a[i][j];
    }
    for(int i=1;i<=n;i++)
     for(int j=1;j<=n;j++)
     if(b[i][j]!=c[i][j])
     return 0;
     return 1;
}
bool work3()
{
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        b[n-j+1][i]=a[i][j];
    }
    for(int i=1;i<=n;i++)
      for(int j=1;j<=n;j++)
     if(b[i][j]!=c[i][j])
     return 0;
     return 1;
}
bool work4()
{
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        b[i][n-j+1]=a[i][j];
    }
    for(int i=1;i<=n;i++)
      for(int j=1;j<=n;j++)
     if(b[i][j]!=c[i][j])
     return 0;
     return 1;
}
bool work5()
{
    work4();
    for(int i=1;i<=n;i++)
     for(int j=1;j<=n;j++)
      a[i][j]=b[i][j];  
      if(work1())
      return 1;
    for(int i=1;i<=n;i++)
     for(int j=1;j<=n;j++)
      a[i][j]=b[i][j]; 
      if(work2())
      return 1;
    for(int i=1;i<=n;i++)
     for(int j=1;j<=n;j++)
      a[i][j]=b[i][j]; 
      if(work3())
      return 1;
      return 0;
}
bool work6()
{
    for(int i=1;i<=n;i++)
      for(int j=1;j<=n;j++)
     if(b[i][j]!=c[i][j])
     return 0;
     return 1;
}
void work()
{
    if(work1())
    {
        cout<<1;
        return ;
    }
    if(work2())
    {
        cout<<2;
        return ;
    }
    if(work3())
    {
        cout<<3;
        return ;
    }
    if(work4())
    {
        cout<<4;
        return ;
    }
    if(work5())
    {
        cout<<5;
        return ;
    }
    if(work6())
    {
        cout<<6;
        return ;
    }
    cout<<7;
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
     for(int j=1;j<=n;j++)
     {
        cin>>a[i][j];
        d[i][j]=a[i][j];
     }

    for(int i=1;i<=n;i++)
     for(int j=1;j<=n;j++)
      cin>>c[i][j];
    work();
    return 0; //完美的结束QAQ
}

【入门5】字符串

P5733 【深基6.例1】自动修正

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

int main() {

    string s;
    cin >> s;
    for (int i = 0; i < s.size(); ++ i) s[i] = toupper(s[i]);
    cout << s << endl;

    return 0;
}

P1914 小书童——凯撒密码

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

int main() {
    int n;
    string s;
    cin >> n;
    cin >> s;
    for (int i = 0; i < s.size(); ++ i) {
        s[i] = (s[i] - 'a' + n) % 26 + 'a';
    }
    cout << s << endl;

    return 0;

}

P1125 [NOIP2008 提高组] 笨小猴

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

bool is_prime(int x) {
    if (x <= 1) return false;
    for (int i = 2; i <= sqrt(x); ++ i) {
        if (x % i == 0) return false;
    }
    return true;
}
int c[30];
int main() {

    string s;
    cin >> s;

    for (int i = 0; i < s.size(); ++ i) {
        c[s[i] - 'a'] ++;
    } 

    int mx = 0, mn = 101;
    for (int i = 0; i < 26; ++ i) {
        mx = max(mx, c[i]);
        if (c[i])
            mn = min(mn, c[i]);
    }
    int t = mx - mn;
    if (is_prime(t)) {
        cout << "Lucky Word" << endl;
        cout << t << endl;
    }else {
        cout << "No Answer" << endl;
        cout << 0 << endl;
    }

    return 0;

}

P1957 口算练习题

#include <iostream> //cin & cout 用iostream
#include <cstring> //memset & strlen 用cstring
#include <cstdio> //sscanf & sprintf 用cstdio
using namespace std;
int main(){
    char a;//a用于存储运算符
    int n,c,d;//n存储个数不解释,cd存储两个数字
    char s[100],b[10];//s存储最终的字符串,b临时变量
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>b;//输入一串字符,有可能是运算符,也有可能是数字
        if(b[0]>='a' && b[0]<='z'){
            a=b[0];//如果是运算符就存入a,然后输入数字
            cin>>c>>d;
        }else{
            sscanf(b,"%d",&c);//如果是数字就转换b为int存储到第一个数字
            cin>>d;//然后输入剩下的第二个数字
        }
        memset(s,0,sizeof(s));//清空原有的字符串,防止长度判断错误
        if(a=='a')//用sprintf格式化
            sprintf(s,"%d+%d=%d",c,d,c+d);
        else if(a=='b')
            sprintf(s,"%d-%d=%d",c,d,c-d);
        else if(a=='c')
            sprintf(s,"%d*%d=%d",c,d,c*d);
        cout<<s<<endl<<strlen(s)<<endl;//输出字符串和字符串长度
    }
    return 0;
}

P5015 [NOIP2018 普及组] 标题统计

#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
    char s[10];
    gets(s);
    int n = strlen(s);
    int ans = 0;
    for(int i = 0; i < n; i ++)
    {
        if (s[i] >= 'A' && s[i] <= 'Z')
        ans ++;
        if (s[i] >= 'a' && s[i] <= 'z')
        ans ++;
        if (s[i] >= '0' && s[i] <= '9')
        ans ++;
    }
    printf("%d",ans);
    return 0;
}

P5734 【深基6.例6】文字处理软件

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

int main() {
    string s;
    int t;
    cin >> t >> s;
    while (t --) {
        int op, a, b;
        string st;
        cin >> op;
        if (op == 1) {
            cin >> st;
            s += st;
            cout << s << endl;
        }else if (op == 2) {
            cin >> a >> b;
            s = s.substr(a, b);
            cout << s << endl;
        }else if (op == 3) {
            cin >> b >> st;
            s.insert(b, st);
            cout << s << endl;
        }else if (op == 4) {
            cin >> st;
            int f = s.find(st);
            if (f == s.size()) cout << -1 << endl;
            else cout << f << endl;
        }
    }
    return 0;
}

P1308 [NOIP2011 普及组] 统计单词数

#include<cstdio>
#include<cstring>
#include <cctype>
char article[1000000+10];
char word[10+10];
int main(){
    //获取输入 
    gets(word);
    gets(article);
    int art_len,wor_len;
    art_len = strlen(article);
    wor_len = strlen(word);
    //全转为小写 
    for(int i=0;i<art_len;++i)     article[i]=tolower(article[i]);
    for(int i=0;i<wor_len;++i)     word[i]=tolower(word[i]);
    int locate=-1,sum=0;
    int i,j;//循环变量 
    for(i=0;i<art_len;++i){
        if(i==0 || article[i-1]==' '){//开头或者文章前一个字符为空格开始匹配 
            for(j=0;j<wor_len;j++){
                if(article[i+j]!=word[j]) break;//不匹配跳出这一层循环 
                else continue;//匹配继续匹配下一个字符 
            }
        }
        else continue;//既不是开头前一个字符也不是空格 读取article下一个字符 
        if((j==wor_len && article[i+j]==' ') || (j==wor_len && (i+j)==art_len)){//刚好匹配一个单词或者匹配到末尾了 
            if(locate==-1) locate=i;
            sum++; 
        } 
    }
    if(sum!=0 && locate!=-1) printf("%d %d\n",sum,locate);
    else printf("-1");
    return 0;
}

P1765 手机

#include<bits/stdc++.h>
using namespace std;
int ans;
string a;
int num[26]={1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 1, 2, 3, 4};        //26个字母打表需要按几次 
int main()
{
    getline(cin, a);
    for(int i = 0; i < a.length(); i ++)
    {
        if(a[i] >= 'a' && a[i] <= 'z') ans += num[a[i] - 'a'];        //不能写a[i]!=' ',因为还有'\n'和'\r' 
        if(a[i] == ' ') ans ++;    //不能写else因为也有'\n'和'\r',这就是这个题的坑点,我交了好几次,欲哭无泪 
    }
    cout << ans << endl;
    return 0;
}

P3741 honoka的键盘

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

int main()
{
    int n;
    string s;
    cin >> n >> s;
    int ans = 0;

    for (int i = 0; i + 1 < n; ++ i) {
        if (s[i] == 'V' && s[i + 1] == 'K') {
            ++ ans;
            s[i] = 'v', s[i + 1] = 'k';
        }
    }
    for (int i = 0; i + 1 < n; ++ i) {
        if (s[i] == s[i + 1]) {
            ++ ans;
            break;
        }
    }
    cout << ans << endl;

    return 0;
}

P1321 单词覆盖还原

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

int main()
{
    string s;
    cin >> s;
    int b = 0, g = 0;

    for (int i = 0; i < s.size(); ++ i) {
        if (i + 2 < s.size()) {
            if (s[i] == 'b' || s[i + 1] == 'o' || s[i + 2] == 'y') {
                ++ b;
            }
        }
        if (i + 3 < s.size()) {
            if (s[i] == 'g' || s[i + 1] == 'i' || s[i + 2] == 'r' || s[i + 3] == 'l') {
                ++ g;
            }
        }
    }
    cout << b << endl;
    cout << g << endl;

    return 0;
}

P1553 数字反转(升级版)

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

string reverse2(string s) {
    int zeroCount = 0;
    reverse(s.begin(), s.end()); 

    for (int i = 0; i < s.size(); ++ i)
        if (s[i] == 48) ++ zeroCount;
        else break;
    s.erase(s.begin(), s.begin() + zeroCount);
    return (s != "" ? s : "0"); 
}

string deleteTail(string s) { 
    int zeroCount = 0;
    for (int i = s.size() - 1; i >= 0; --i)
        if (s[i] == 48) ++zeroCount;
        else break;
    s.erase(s.end() - zeroCount, s.end());
    return (s != "" ? s : "0");
}

int main() {
    string s;
    cin >> s;
    if (s[s.size() - 1] == '%') {
        cout << reverse2(s.substr(0, s.size() - 1)) << "%" << endl;
        return 0;
    }
    for (int i = 0; i < s.size(); ++ i) {
        string left, right;
        if (s[i] == '/') {
            left = s.substr(0, s.find("/"));
            right = s.substr(s.find("/") + 1);
            cout << reverse2(left) << "/" << reverse2(right) << endl;
            return 0;
        }
        if (s[i] == '.') {
            left = s.substr(0, s.find("."));
            right = s.substr(s.find(".") + 1);
            cout << reverse2(left) << "." << deleteTail(reverse2(right)) << endl;
            return 0;
        }
    }
    cout << reverse2(s) << endl;
    return 0;
}

P1603 斯诺登的密码

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

map<string, int> q;

int main() {
    q["one"]=1;q["two"]=2;q["three"]=3;q["four"]=4;q["five"]=5;q["six"]=6;q["seven"]=7;q["eight"]=8;q["nine"]=9;q["ten"]=10;
    q["eleven"]=11;q["twelve"]=12;q["thirteen"]=13;q["fourteen"]=14;q["fifteen"]=15;q["sixteen"]=16;q["seventeen"]=17;q["eighteen"]=18;q["nineteen"]=19;q["twenty"]=20;
    q["a"]=1;q["both"]=2;q["another"]=1;q["first"]=1;q["second"]=2;q["third"]=3;

    string s;
    vector<int> v;
    for (int i = 0; i < 6; ++ i) {
        cin >> s;
        if (q[s]) {
            int k = q[s] * q[s] % 100;
            if (k == 0) continue;
            v.push_back(k);
        }
    }
    if (v.size() == 0) v.push_back(0);
    sort(v.begin(), v.end());
    cout << v[0];
    for (int i = 1; i < v.size(); ++ i) {
        if (v[i] < 10) cout << 0;
        cout << v[i];
    }

    return 0;
}

P1200 [USACO1.1] 你的飞碟在这儿 Your Ride Is Here

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

int main() {
    string s1, s2;
    cin >> s1 >> s2;
    int a = 1, b = 1;
    for (int i = 0; i < s1.size(); ++ i) {
        a = a * (s1[i] - 'A' + 1) % 47;
    }
    for (int i = 0; i < s2.size(); ++ i) {
        b = b * (s2[i] - 'A' + 1) % 47;
    }
    if (a == b) cout << "GO" << endl;
    else cout << "STAY" << endl;

    return 0;
}

P1597 语句解析

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

int a[3];char s1,s2;
int main()
{
    while (scanf("%c:=%c;", &s1, &s2) == 2) {
        a[s1 - 'a'] = s2 >= '0' && s2 <= '9' ? s2 - '0' : a[s2 - 'a']; //赋值语句简洁明了
    }
    printf("%d %d %d", a[0], a[1], a[2]);

    return 0;
}

P1598 垂直柱状图

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int ff[26];//定义计数数组
int main()
{
    int i,j,n,maxn=0;char a[81];
    for(i=0;i<4;i++)
    {
        gets(a);//gets读入
        n=strlen(a);
        for(j=0;j<n;j++)if(a[j]>='A'&&a[j]<='Z')ff[a[j]-'A']++;//统计字符出现次数
    }
    for(i=0;i<26;i++)maxn=max(maxn,ff[i]);//最多次数(最高柱状图)
    for(i=maxn;i>0;i--){
    for(j=0;j<26;j++)
    if(ff[j]>=i)printf("* ");else printf("  ");//模拟,是可以输出的就输出*,否则跳过
    printf("\n");}//换行
    for(i=0;i<26;i++)printf("%c ",i+'A');//输出a~z
}

【入门6】函数与结构体

P5735 【深基7.例1】距离函数

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

double dist(pair<double, double> a, pair<double, double> b) {
    double x1 = a.first, y1 = a.second;
    double x2 = b.first, y2 = b.second;
    double dx = x1 - x2;
    double dy = y1 - y2;
    return sqrt(dx * dx + dy * dy);
}

int main() {

    pair<double, double> a, b, c;
    double tx, ty;
    cin >> tx >> ty;
    a = {tx, ty};

    cin >> tx >> ty;
    b = {tx, ty};

    cin >> tx >> ty;
    c = {tx, ty};

    printf("%.2lf", dist(a, b) + dist(a, c) + dist(b, c));

    return 0;

}

P5736 【深基7.例2】质数筛

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

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

int main() {
    int n;
    cin >> n;
    vector<int> ans;
    for (int i = 0; i < n; ++ i) {
        int t;
        cin >> t;
        if (is_prime(t)) {
            ans.push_back(t);
        }
    }
    for (int i = 0; i < ans.size(); ++ i) {
        cout << ans[i] << " ";
    }

    return 0;

}

P5737 【深基7.例3】闰年展示

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

bool is_run(int y) {
    if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0)) {
        return true;
    }
    return false;
}
int main()
{
    int x, y;
    vector<int> ans;
    cin >> x >> y;
    for (int i = x; i <= y; ++ i) {
        if (is_run(i)) {
            ans.push_back(i);
        }
    }
    cout << ans.size() << endl;
    for (int i = 0; i < ans.size(); ++ i) {
        cout << ans[i] << " ";
    }

    return 0;
}

P5738 【深基7.例4】歌唱比赛

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

int n, m;
double get(vector<int> t) {
    sort(t.begin(), t.end());
    double sum = 0;
    for (int i = 1; i < m - 1; ++ i) {
        sum += t[i];
    }
    return sum / (m - 2);
}
int main()
{
    cin >> n >> m;
    double ans = 0;
    for (int i = 0; i < n; ++ i) {
        vector<int> t;
        for (int j = 0; j < m; ++ j) {
            int v;
            cin >> v;
            t.push_back(v);
        }
        ans = max(ans, get(t));
    }
    printf("%.2lf", ans);

    return 0;
}

P5739 【深基7.例7】计算阶乘

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

int jie(int x) {
    if (x == 1) return 1;
    return x * jie(x - 1);
}

int main()
{
    int n;
    cin >> n;
    cout << jie(n) << endl;

    return 0;
}

P5461 赦免战俘

#include<bits/stdc++.h>
using namespace std;
int a[1025][1025];
int main()
{
    int n;
    cin >> n;
    n = 1 << n;
    a[0][n + 1] = 1;
    for (int i = 1; i <= n; ++ i) {
        for (int j = 1; j <= n; ++ j) {
            a[i][j] = a[i - 1][j] ^ a[i - 1][j + 1];
            cout << a[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}
#include<bits/stdc++.h>
using namespace std;
int a[1025][1025], n;
void f(int len, int x, int y) {
    if (len == 2) {
        a[x][y] = 1;
        return;
    }
    for (int i = x; i < x + len / 2; ++ i) {
        for (int j = y; j < y + len / 2; ++ j) {
            a[i][j] = 1;
        }
    }
    f(len / 2, x + len / 2, y);
    f(len / 2, x + len / 2, y + len / 2);
    f(len / 2, x, y + len / 2);
}
int main()
{
    cin >> n;
    n = 1 << n;
    f(n, 0, 0);
    for (int i = 0; i < n; ++ i) {
        for (int j = 0; j < n; ++ j) {
            cout << (a[i][j] == 0) << " ";
        }
        cout << endl;
    } 

    return 0;
}

P5740 【深基7.例9】最厉害的学生

#include<bits/stdc++.h>
using namespace std;
struct MyStruct
{
    string a;
    int b, c, d;
    int sum = 0;
}q[1005];
int main()
{
    int n; cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> q[i].a >> q[i].b >> q[i].c >> q[i].d;
    }
    for (int i = 0; i < n; i++)
    {
        q[i].sum =q[i].b + q[i].c + q[i].d;
    }
    int max = 0, ans = 0; 
    for (int i = 0; i < n; i++)
    {
        if (q[i].sum > max) {
            max = q[i].sum;
            ans = i;
        }

    }
    cout << q[ans].a << " " << q[ans].b << " " << q[ans].c << " " << q[ans].d << endl;

    return 0;
}

P5741 【深基7.例10】旗鼓相当的对手 - 加强版

#include<bits/stdc++.h>
using namespace std;
struct eee {
    int yuwen,shuxue,yingyu,zf;
    string s;
};
int n;
eee a[1001];
int main() {
    cin >> n;
    for (int i = 1; i <= n; ++ i) {
        cin >> a[i].s >> a[i].yuwen >> a[i].shuxue >> a[i].yingyu;
        a[i].zf = a[i].yuwen + a[i].shuxue + a[i].yingyu;
    }
    for (int i = 1; i < n; ++ i)
        for (int j = i + 1; j <= n; ++ j) {
            if (a[i].s > a[j].s) {
                swap(a[i].s, a[j].s);
                swap(a[i].yuwen, a[j].yuwen);
                swap(a[i].shuxue, a[j].shuxue);
                swap(a[i].yingyu, a[j].yingyu);
            }
        }
    for (int i = 1; i < n; ++ i) {

        for (int j = i + 1; j <= n; ++ j) {
            if(abs(a[i].yuwen - a[j].yuwen) <= 5 && abs(a[i].shuxue - a[j].shuxue) <= 5 && abs(a[i].yingyu - a[j].yingyu) <= 5 && abs(a[i].zf - a[j].zf) <= 10) {
                cout << a[i].s << " " << a[j].s << endl;
            }
        }
    }
    return 0;
}

P5742 【深基7.例11】评等级

#include<bits/stdc++.h>
using namespace std;
struct node {
    int id;//学号
    double sc1, sc2;//学业成绩和素质拓展成绩
    int score;//学业成绩和素质拓展成绩的和
    double final_score;//综合分数
}a[1000];//结构体定义,由于本人习惯(懒),sc1和sc2定义成了double。
int main() {
    int n;
    cin >> n;
    for(int i=0; i<n; i++) {
        cin >> a[i].id >> a[i].sc1 >> a[i].sc2;
        a[i].score = a[i].sc1 + a[i].sc2;
        a[i].final_score = a[i].sc1 * 0.7 + a[i].sc2 * 0.3;//计算综合分数
    }
    for(int i=0; i<n; i++) {
        if(a[i].score > 140 && a[i].final_score >= 80) {//一定看清题
            cout << "Excellent" << endl;
        }
        else {
            cout << "Not excellent" << endl;
        }
    }
    return 0;
}

P1075 [NOIP2012 普及组] 质因数分解

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

int main() {
    int n;
    cin >> n;
    for (int i = 2; i <= n; ++ i) {
        if (n % i == 0) {
            cout << n / i << endl;
            break;
        }
    }
    return 0;
}

P1304 哥德巴赫猜想

#include <cstdio>
#include <algorithm>
using namespace std;
bool is_prime(int x)
{
    for(int i = 2;i * i <= x;i++)
    {
        if(x % i == 0)
            return false;
    }
    return true;
}
void write(int a)
{
    if(a == 4)
    {
        printf("4=2+2\n");
        return;
    }
    for(int i = 3;i + 2 <= a;i += 2)
    {
        if(is_prime(i) && 2 + i == a)
        {
            printf("%d=2+%d\n",a,i);
            return;
        }
    }
    for(int i = 3;i + 3 <= a;i += 2)
    {
        if(is_prime(i) && is_prime(a - i))
        {
            printf("%d=%d+%d\n",a,min(i,a - i),max(i,a - i));
            return;
        }
    }
}
int n;
int main()
{
    scanf("%d",&n);
    for(int i = 4;i <= n;i += 2)
        write(i);
    return 0;
}

P1217 [USACO1.5] 回文质数 Prime Palindromes

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

const int MAXN = 1e7 + 5;
bool isnp[MAXN];
vector<int> primes; // 质数表
void init(int n)
{
    for (int i = 2; i <= n; i++)
    {
        if (!isnp[i])
            primes.push_back(i);
        for (int j = 0; j < primes.size(); ++ j)
        {   
            int p = primes[j];
            if (p * i > n)
                break;
            isnp[p * i] = 1;
            if (i % p == 0)
                break;
        }
    }
}
bool check(int x) {
    string s;
    while (x) {
        char c = '0' + (x % 10);
        x /= 10;
        s += c;
    }
    for (int i = 0, j = s.size() - 1; i <= j; ++ i, -- j) {
        if (s[i] != s[j]) return false;
    }
    return true;
}
int main() {
    init(10000000);
    vector<int> ans;
    int a, b;
    cin >> a >> b;
    for (int i = 0; i < primes.size(); ++ i) {
        int v = primes[i];
        if (v >= a && v <= b && check(v)) ans.push_back(v);
    } 
    for (int i = 0; i < ans.size(); ++ i) {
        cout << ans[i] << endl;
    }

    return 0;
}

P2415 集合求和

#include<bits/stdc++.h>
using namespace std;
int main(){
    long long x = 0, s = 0, n = 0;
    while (cin >> x) {
        ++ n;
        s += x;
    }
    cout << s * (long long)pow(2, n - 1) << endl;
    return 0; 
}

P5743 【深基7.习8】猴子吃桃

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n, ans = 1;
    cin >> n;
    for (int i = 1; i < n; ++ i) {
        ans += 1;
        ans *= 2;
    }
    cout << ans << endl;
    return 0; 
}

P5744 【深基7.习9】培训

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

struct Luogu {
    string name;
    int age, score;
} a[10];

int main () {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i].name >> a[i].age >> a[i].score;
        a[i].age++, a[i].score = a[i].score *6 / 5;
        if (a[i].score > 600) a[i].score = 600;
        cout << a[i].name << " " << a[i].age << " " << a[i].score << endl;
    }
    return 0;
}
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇