5 条题解

  • 2
    @ 2023-10-4 14:53:08

    呵呵,好简单,只需要开个变量判断是否到第x个数了,如果到就输出y,不到就正常输出aiai即可

    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);cout.tie(0);
        int n, x, y, a[15];
        cin >> n;
        for(int i=1;i<=n;i++)
            cin >> a[i];
        cin >> x >> y;
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            ans++;
            if(ans == x)
                cout<<y<<" ";
            cout<<a[i]<<" ";
        }
        return 0;
    }
    
    • 1
      @ 2024-2-16 14:17:22

      这道题可以使用vector向量实现快速插入,使用insert函数,vector操作函数如下:

      1. erase删除元素

      2. insert插入元素

      3. pop_back删除最后一个元素

      4. push_back在最后加入

        AC代码如下:

      #include <bits/stdc++.h>
      using namespace std;
      int main()
      {
          vector<int> v;
          int n,x,y;
          cin >> n;
          for (int i = 1;i <= n;i++)
          {
              int t;
              cin >> t;
              v.push_back(t);
          }
          cin >> x;
          cin >> y;
          v.insert(v.begin() + x - 1,y);
          for (int i = 0;i < v.size();i++)
          {
              cout << v[i] << " ";
          }
          return 0;
      }
      
      • 1
        @ 2023-8-19 21:15:08

        半夜打卡第17题

        #include <bits/stdc++.h>
        using namespace std;
        int main()
        {
            int n , a[10] , x , y;
            cin >> n;
            for (int i = 0; i < n; i++) cin >> a[i];
            cin >> x >> y;
            for (int i = 0; i < n; i++)
            { 
                if (i == x - 1) cout << y << " ";//直接用索引找位置
                cout << a[i] << " ";//插入
            }
            return 0;
        }
        
        • 0
          @ 2024-3-5 21:34:12

          这题挺简单,判断到没到插入位置直接输出就行ㄟ( ▔, ▔ )ㄏ

          #include<bits/stdc++.h>
          using namespace std;
          int n, m, y;
          int a[114514];
          int main()
          {
              cin >> n;
              for(int i = 1; i <= n; i ++)
              {
                  cin >> a[i];
              }
              cin >> m >> y;
              for(int i = 1; i <= n; i ++)
              {
                  cout << a[i] << " ";
                  if(i + 1 == m)
                  {
                      cout << y << " ";
                  }
              }
              return 0;
          }
          
          • 0
            @ 2023-1-17 22:25:53
            #include<bits/stdc++.h>
            using namespace std;
            int main()
            {
            	int a[25]={0}, n, x, y;
            	cin >> n;
            	for(int i = 1; i <= n; i++){
            		cin >> a[i];
            	}
            	cin >> x >> y;
            	for(int i = n; i >= x; i--){
            		a[i+1] = a[i];
            	}
            	a[x] = y;
            	for(int i = 1; i <= n+1; i++){
            		cout << a[i] << " ";
            	}
            	return 0;
            }
            
            
            
            • 1

            信息

            ID
            211
            时间
            1000ms
            内存
            16MiB
            难度
            1
            标签
            递交数
            48
            已通过
            41
            上传者