7 solutions

  • 1
    @ 2022-5-7 12:59:35

    #include<stdio.h> int main() { long long i,n,sum; scanf("%lld",&n); for(i=1,sum=0;i<=n;i++) sum=sum+i; printf("%lld\n",sum); }

    • 0
      @ 2024-12-6 21:48:45
      #include <stdio.h>
      int main()
      {
      	long long n;
      	scanf("%lld", &n);
      	if (n >= 1&&n <= 100000000) 
      	{
      		printf("%lld", ((n+1)*n)/2);
      	}
      	return 0;
      }
      
      • 0
        @ 2024-11-6 21:48:52
        #include <bits/stdc++.h>
        using namespace std;
        int main()
        {
            long long n;
            cin>>n;
            cout<<(n+1)*n/2;
            return 0;
        }
        
        • 0
          @ 2024-10-21 15:37:57

          #include <stdio.h> int main() { int result, x, y;

          scanf_s("%d",&x);
          for (result = 0, y = 1; y <= x; y++)
          {
          	result = result + y;
          
          
          };
          
          printf("%d",result);
          return 0;
          

          }

          • 0
            @ 2023-10-30 12:56:31
            #include <stdio.h>
            int main()
            {
                long i,n,sum;
                i = 1;
                sum = 0;
                scanf("%ld",&n);
                do{
                    sum = sum+i;
                    i++;
                }while(i<=n);
                printf("%ld",sum);
                return 0;
            }
            
            • 0
              @ 2022-4-4 17:11:06

              C++

              这题要注意int的范围可能不够🤔

              #include <iostream>
              using namespace std;
              int main()
              {
                  long long n = 0;
                  long long sum = 0;
                  cin >> n;
                  for(int i = 1; i <= n; i++)
                      sum += i;
                  cout << sum << endl;
                  return 0;
              }
              

              python

              while True:
                  try:
                      n = input()
                      sum = 0
                      for i in range(n+1):
                          sum += i
                      print(sum)
                  except:
                      break
              
              
              • 0
                @ 2022-3-7 15:08:50

                求序列和: 方法一:可以直接暴力循环,时间复杂度为O(n);

                方法二:数学等差数列公式:((A1+An)*n)/2,注意数字的范围可能会超过int。

                #include<bits/stdc++.h>
                using namespace std;
                int main() {
                long long  x,y;
                scanf("%lld",&x);
                printf("%lld\n",((1+x)*x)/2);
                    return 0;
                }
                
                • 1

                Information

                ID
                34
                Time
                1000ms
                Memory
                256MiB
                Difficulty
                8
                Tags
                # Submissions
                2839
                Accepted
                413
                Uploaded By