1 条题解

  • 0
    @ 2021-6-15 10:09:42

    C++ :

    #include<cstdio>
    using namespace std;
    int n,i,a=0,b,ans=0;
    int main()
    {
    	scanf("%d",&n);
    	for(i=1;i<=n;i++)
    	{
    		scanf("%d",&b);
    		if(a<b)ans+=(b-a);
    		a=b;
    	}
    	printf("%d",ans);
    	return 0;
    }
    

    Pascal :

    var
       n,x,ans,i,y:longint;
    begin
     readln(n);
     read(x);
     ans:=x;
     for i:=2 to n do
      begin
       read(y);
       if x<y then ans:=ans+(y-x);
       x:=y;
      end;
     writeln(ans);
    end.
    

    Java :

    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args){
            Scanner scanner = new Scanner(System.in);
            int n = scanner.nextInt();
            int d[] = new int[100005];
            for(int i=0;i<n;i++){
                d[i] = scanner.nextInt();
            }
            int ans = 0;
            for(int i=1;i<n;i++){
                if (d[i]>d[i-1])
                    ans+=d[i]-d[i-1];
            }
            System.out.println(ans+d[0]);
        }
    }
    /*
    #include <bits/stdc++.h>
    #include <iostream>
    using namespace std;
    int n,ans=0;
    int d[100005];
    int main ()
    {
        cin >> n;
        for (int i=0;i<n;i++)
            cin >> d[i];
        for (int i=1;i<n;i++)
            if (d[i]>d[i-1])
                ans+=d[i]-d[i-1];
        cout << ans+d[0];
    }
     */
    
    • 1

    信息

    ID
    318
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者