3 solutions

  • 1
    @ 2026-2-23 12:10:36
    #include <iostream>
    using namespace std;
    int main() {
        int n;
        cin >> n; 
        int total = n * (n + 1) * (2 * n + 1) / 6;
        cout << total << endl;  
        return 0;
    }
    
    
    • 0
      @ 2026-1-25 20:46:54
      #include<bits/stdc++.h>
      using namespace std;
      int main()
      {
          int a;
          int s=0;
          cin>>a;
          while(a!=1)
          {
              s+=a*a;
              a--;
          }
          s+=a*a;
          cout<<s;
      }
      
      • -2
        @ 2025-11-27 11:54:18

        C++ :

        #include <iostream>
        using namespace std;
        
        int main() {
            int n;
            cin >> n;  // 读取金字塔的层数
            
            // 计算1² + 2² + ... + n²的和,使用公式n(n+1)(2n+1)/6
            int total = n * (n + 1) * (2 * n + 1) / 6;
            
            cout << total << endl;  // 输出总石块数
            return 0;
        }
        
        • 1

        Information

        ID
        24
        Time
        1000ms
        Memory
        128MiB
        Difficulty
        5
        Tags
        # Submissions
        21
        Accepted
        13
        Uploaded By