菜鸟的代码:
import java.util.*; import java.util.regex.*; import java.text.*; import java.math.*; public class SkiResortsEasy { public int minCost(int[] altitude) { int i,count; count=0; for(i=1;i<altitude.length;i++){ if(altitude[i]>altitude[i-1]){ count+=altitude[i]-altitude[i-1]; altitude[i]=altitude[i-1]; } } return count; } } //Powered by KawigiEdit 2.1.4 (beta) modified by pivanof!
大神的代码(JAVA):
public class SkiResortsEasy { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub } public int minCost(int[] altitude) { int last=altitude[0]; int output=0; for(int i=1;i<altitude.length;i++) if(last<altitude[i]) output+=altitude[i]-last; else last=altitude[i]; return output; } }
大神的代码(C++):
#include <vector> #include <list> #include <map> #include <set> #include <deque> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <ctime> using namespace std; class SkiResortsEasy { public: int minCost(vector <int>); }; int SkiResortsEasy::minCost(vector <int> a) { int sum=0; for(int i=0;i<a.size()-1;i++) { if(a[i+1]>a[i]) {sum+=(a[i+1]-a[i]); a[i+1]=a[i]; } } return sum; } //Powered by [KawigiEdit] 2.0!
分析:
算法:数数
对比:
1.我和C++版本代码算法一样;
2.Java版的代码没有修改原数据。
总结:
差不多。