hrbust《程序设计基础》实践3

 //k题
 #include<bits/stdc++.h>
 using namespace std;
 int solve(int x, int y) {
     if (x == 1 && y == 1) return 1;
     if (x < 1 || y < 1) return 0;
     else return solve(x, y - 1) + solve(x - 1, y - 1);
 }
 int main() {
     int t;
     cin >> t;
     while (t--) {
         int n;
         cin >> n;
         for (int i = 1;i <= n;i++) {
             int first = 1;
             for (int j = 1;j <= i;j++) {
                 if (first) first = 0;
                 else cout << " ";
                 cout << solve(j, i);
             }
             cout << endl;
         }
     }
     return 0;
 }
 //A题
 #include <bits/stdc++.h>
 #define endl '\n'
 using namespace std;
 #define int long long
 #define all(a) a.begin(),a.end()
 using pii = pair<int, int>;
 const int N = 5e5 + 10, M = N, inf = 1e9, mod = 1e9 + 7;
 void solve() {
     int n;
     cin >> n;
     for (int i = 1;i <= n;i++) {//行数
         for (int j = 1;j <= n - i;j++) {//空格
             cout << " ";
         }
         for (int j = 1;j <= 2 * i - 1;j++) {//数字
             cout << n;
         }
         cout << endl;
     }
 }
 signed main() {
     cin.tie(0)->ios::sync_with_stdio(false);
     int tt;
     cin >> tt;
     while (tt--) {
         solve();
     }
     return 0;
 }
 //C题目
 #include <bits/stdc++.h>
 #define endl '\n'
 using namespace std;
 using pii = pair<int, int>;
 const int N = 5e5 + 10, M = N, inf = 1e9, mod = 1e9 + 7;
 set<long long>arr;
 void ws(int l, int r) {
     bool first = true;
     for (long long i = l;i <= r;i++) {
         long long sum = 0;
         //找出因子放入set
         for (long long j = 1;j <= i;j++) {
             if (i % j == 0 && i != j) arr.insert(j);
         }
         //把所有因子相加
         for (auto it = arr.begin();it != arr.end();it++) {
             sum += *it;
         }
         //判断:如果因子和等于此数;
         if (sum == i) {
             first = false;
             cout << i << ";";
         }
         arr.clear();
     }
     if (first) {
         cout << "0;" << endl;
     }
     else cout << endl;
 }
 int main()
 {
     int a, n, m;
     cin >> a;
     while (a--)
     {
         cin >> n >> m;
         ws(n, m);
     }
     return 0;
 }
 //D题
 #include <bits/stdc++.h>
 #define endl '\n'
 using namespace std;
 #define int long long
 #define all(a) a.begin(),a.end()
 using pii = pair<int, int>;
 const int N = 5e5 + 10, M = N, inf = 1e9, mod = 1e9 + 7;
 void solve() {
     int m, n;
     vector<int>num;
     while (cin >> m >> n) {
         while (m)
         {
             int u = m % 10;
             num.push_back(u);
             m = m / 10;
         }
         reverse(num.begin(), num.end());
         for (int i = 0;i < n;i++) {
             cout << num[i];
         }
         cout << endl;
         num.clear();
     }
 }
 signed main() {
     cin.tie(0)->ios::sync_with_stdio(false);
     int tt=1;
     while (tt--) {
         solve();
     }
     return 0;
 }
 //E题目
 #include <bits/stdc++.h>
 #define endl '\n'
 using namespace std;
 #define int long long
 #define all(a) a.begin(),a.end()
 using pii = pair<int, int>;
 const int N = 5e7 + 10, M = N, inf = 1e9, mod = 1e9 + 7;
 vector<int>arr;
 bool sushu(int n)
 {
     int f = sqrt(n) + 1;
     for (int i = 2; i <f ; i++)
     {
         if (n % i == 0)
             return false;
     }
     return true;
 }
 void solve() {
 ​
     int n;
     cin >> n;
     for (int i = 2;i <=n;i++) {
         if (sushu(i))arr.push_back(i);
     }
     int ans = 0;
     for (auto it = arr.begin();it != arr.end();it++) {
         for (auto it2 = it;it2 != arr.end();it2++) {
             if (*it + *it2 == n) {
                 ans++;
                 *it = 0;
                 *it2 = 0;
             }
         }
     }
     cout << ans << endl;
     arr.clear();
 }
 signed main() {
     cin.tie(0)->ios::sync_with_stdio(false);
     int tt;
     cin >> tt;
     while (tt--) {
         solve();
     }
     return 0;
 }
 //F题目
 #include <bits/stdc++.h>
 #define endl '\n'
 using namespace std;
 #define int long long
 #define all(a) a.begin(),a.end()
 using pii = pair<int, int>;
 const int N = 5e5 + 10, M = N, inf = 1e9, mod = 1e9 + 7;
 signed main() {
     cin.tie(0)->ios::sync_with_stdio(false);
     int tt;
     cin >> tt;
     while (tt--) {
         int n;
         cin >> n;
         for (int i = 1;i <= n;i++) {
             for (int j = 1;j <= n - i;j++) {
                 cout << " ";
             }
             for (int j = 1;j <= i;j++) {
                 cout << '*';
             }
             cout << endl;
         }
     }
     return 0;
 }
 //G题目
 #include <bits/stdc++.h>
 #define endl '\n'
 using namespace std;
 #define int long long
 #define all(a) a.begin(),a.end()
 using pii = pair<int, int>;
 const int N = 5e5 + 10, M = N, inf = 1e9, mod = 1e9 + 7;
 signed main() {
     cin.tie(0)->ios::sync_with_stdio(false);
     int tt;
     cin >> tt;
     while (tt--) {
         int n;
         cin >> n;
         int sum = 0;
         int nine(0);
         int eight(0);
         int seven(0);
         int six(0);
         int sb(0);
         for (int i = 0;i < n;i++) {
             int score;
             cin >> score;
             if (score >= 90)nine++;
             else if (score >= 80)eight++;
             else if (score >= 70)seven++;
             else if (score >= 60)six++;
             else sb++;
             sum += score;
         }
         cout << "Avg:" << sum / n<<endl;
         cout << "90-100:" << nine << endl;;
         cout << "80-89:" << eight << endl;
         cout << "70-79:" << seven << endl;
         cout << "60-69:" << six << endl;
         cout << "0-59:" << sb << endl;
 ​
     }
     return 0;
 }
 //H题目
 #include <bits/stdc++.h>
 #define endl '\n'
 using namespace std;
 #define all(a) a.begin(),a.end()
 using pii = pair<int, int>;
 const int N = 5e5 + 10, M = N, inf = 1e9, mod = 1e9 + 7;
 int Fibo(int n) {
     if (n == 0) return 0;
     if (n == 1 || n == 2)return 1;
     int one = 1, two = 1, res = 0;
     //如果n>3使用迭代
     for (int i = 3;i <= n;i++) {
         res = one + two;
         one = two;
         two = res;
     }
     return res;
 }
 bool sushu(int x) {
     int f = sqrt(x) +1;
     for (int i = 2;i <f;i++) {
         if (x % i == 0)return false;
     }
     return true;
 }
 void fibPrime(int m) {
     int res = Fibo(m);
     if (sushu(res))cout << res << ":YES" << endl;
     else cout<< res << ":NO" << endl;
 }
 int main()
 {
     int m, n;
     cin >> n;
     while (n--)
     {
         cin >> m;
         fibPrime(m);
     }
     return 0;
 }
 //I题目
 #include <bits/stdc++.h>
 #define endl '\n'
 using namespace std;
 #define int long long
 #define all(a) a.begin(),a.end()
 using pii = pair<int, int>;
 const int N = 50, M = N, inf = 1e9, mod = 1e9 + 7;
 int arr[N][N];
 signed main() {
     cin.tie(0)->ios::sync_with_stdio(false);
     int max = 0;
     int min = 100000000;
     int x1, y1, x2, y2;
     for (int i = 0;i <3;i++) {
         for (int j = 0;j <4;j++) {
             cin >> arr[i][j];
             if (arr[i][j] < min) {
                 min=arr[i][j];
                 x1 = i;
                 y1 = j;
             }
             if (arr[i][j] > max) {
                 max=arr[i][j];
                 x2 = i;
                 y2 = j;
             }
         }
     }
     cout << "max=" << max << "," << x2 << "," << y2 << endl;
     cout << "min=" << min << "," << x1 << "," << y1 << endl;
 ​
     return 0;
 }
 //J题
 #include <bits/stdc++.h>
 #define endl '\n'
 using namespace std;
 #define int long long
 #define all(a) a.begin(),a.end()
 using pii = pair<int, int>;
 const int N = 5e5 + 10, M = N, inf = 1e9, mod = 1e9 + 7;
 vector<int>arr;
 signed main() {
     cin.tie(0)->ios::sync_with_stdio(false);
     int tt;
     cin >> tt;
     while (tt--) {
         int n;
         cin >> n;
         for (int i = 0;i < n;i++) {
             int num;
             cin >> num;
             arr.push_back(num);
         }
         int num2;
         cin >> num2;
         arr.push_back(num2);
         sort(arr.begin(), arr.end());
         bool first = true;
         for (auto it:arr) {
             if (first)first = false;
             else {
                 cout << ' ';
             }
             cout << it;
         }
         cout << endl;
         arr.clear();
     }
     return 0;
 }
 //由于量太少,直接暴力!
 //L题目
 #include <bits/stdc++.h>
 #define endl '\n'
 using namespace std;
 #define int long long
 #define all(a) a.begin(),a.end()
 using pii = pair<int, int>;
 const int N = 5e2 + 10, M = N, inf = 1e9, mod = 1e9 + 7;
 char ch[N];
 signed main() {
     gets(ch);
     char f;
     cin>>f;
     int len=strlen(ch); 
         for(int i=0;i<len;i++){
         if(ch[i]!=f)    
         cout<<ch[i];
         /*
         另一种写法
         for(int i=0;arr[i]!='\0';i++ {
         }
         
         */
          
     }
     cout<<endl;
 }
 //M题
 #include <bits/stdc++.h>
 #define endl '\n'
 using namespace std;
 #define int long long
 #define all(a) a.begin(),a.end()
 using pii = pair<int, int>;
 const int N = 5e5 + 10, M = N, inf = 1e9, mod = 1e9 + 7;
 char ch[N];
 signed main() {
     while (cin>>ch) {
         int len = strlen(ch);
         for (int i = 0;i < len;i++) {
             if (isupper(ch[i])) {
                 ch[i] = tolower(ch[i]);
             }
             else ch[i] = toupper(ch[i]);
         }
         cout << ch << endl;
         memset(ch, '\n', sizeof(ch));
     }
     return 0;
 }
 //N题
 #include <bits/stdc++.h>
 #define endl '\n'
 using namespace std;
 #define int long long
 #define all(a) a.begin(),a.end()
 using pii = pair<int, int>;
 const int N = 5e2 + 10, M = N, inf = 1e9, mod = 1e9 + 7;
 int arr[N][N];
 signed main() {
     cin.tie(0)->ios::sync_with_stdio(false);
     int m, n;
     cin >> m >> n;
     for (int i = 0;i < m;i++) {
         for (int j = 0;j < n;j++) {
             cin >> arr[i][j];
         }
     }
     for (int j = 0;j < n;j++) {
         int first = 1;
         for (int i = 0;i < m;i++) {
             if (first)first = 0;
             else cout << ' ';
             cout << arr[i][j];
         }
         cout << endl;
     }
 }
 //O题
 #include <bits/stdc++.h>
 #define endl '\n'
 using namespace std;
 #define int long long
 #define all(a) a.begin(),a.end()
 using pii = pair<int, int>;
 const int N = 5e5 + 10, M = N, inf = 1e9, mod = 1e9 + 7;
 signed main() {
     cin.tie(0)->ios::sync_with_stdio(false);
     int tt;
     cin >> tt;
     while (tt--) {
         char c;
         cin >> c;
         int num;
         cin >> num;
         for (int i = c;i <c + num;i++) {
             char ch = i;
             cout << ch;
         }
         cout << endl;
     }
     return 0;
 }
 //B题目
 #include <bits/stdc++.h>
 #define endl '\n'
 using namespace std;
 using pii = pair<int, int>;
 const int N = 5e5 + 10, M = N, inf = 1e9, mod = 1e9 + 7;
 long long ask(int x) {
     long long ans = 1;
     for (int i = 2;i <= x;i++) {
         ans *= i;
     }
     return ans;
 }
 long long sumFactor(int n, int m) {
     long long res = 0;
     for (int i = n;i <= m;i++) {
         res += ask(i);
     }
     return res;
 }
 ​
 int main()
 {
     int n, m, s, a;
     cin >> a;
     while (a--) {
         cin >> n >> m;
         s = sumFactor(n, m);
         cout << s << endl;
     }
     return 0;
 }

 花了两小时21分钟做完,慢了三分钟

 //L题的另一种用vector的写法

#include<bits/stdc++.h>
using namespace std;
vector<char>arr;
int main()
{
    char f;
    int ans = 0;
    while ((f = getchar()) != '\n') {
        arr.push_back(f);
    }
    char h;
    cin >> h;
    for (auto it = arr.begin();it != arr.end();) {
        if (*it == h) {
            it = arr.erase(it);
        }
        else it++;
    }
    /*
    或者
        for (auto it = arr.begin();it != arr.end();) {
        arr.erase(remove(arr.begin(),arr.end(),h),arr.end());
    }
    */
    for (auto it = arr.begin();it != arr.end();it++) {
        cout << *it;
    }
    cout << endl;
}

 

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ou_fan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值