首页 > 试题广场 >

最佳投资

[编程题]最佳投资
  • 热度指数:540 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
NowCoder最近在把玩股票,但因为他工作很慢,一天只能做一次“买”和“卖”。
现在告诉你每天从早上9:00到下午17:00,每个整点时刻的股票价格,请你帮他算出当天最多只做一次买卖操作(即当天要么什么都不做,要么买一次并且卖一次),每只股票最大的收益能有多少?

输入描述:
输入有多组数据。

每组数据包含9个实数,分别代表9:00、10:00、...、17:00的股票价格。


输出描述:
对应每一组数据,输出当天只做一次买卖操作的前提下最大收益有多少。

结果保留两位小数(四舍五入)。
示例1

输入

9.3 10.1 8.3 7.7 9.2 9.4 10.5 9.9 9.8
100.3 99.2 102.1 101.8 101.3 101.1 100.9 102.3 99.9

输出

2.80
3.10
 import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        while(scanner.hasNext()){
            ArrayList<Double> arrayList=new ArrayList<Double>();
            double number=scanner.nextDouble();
            arrayList.add(number);
            double max=0;
            int index=0;
            for(int i=1;i<9;i++){
                number=scanner.nextDouble();
                arrayList.add(number);
                if(arrayList.get(i)<arrayList.get(index)){
                    index=i;
                    continue;
                }
                if((arrayList.get(i)-arrayList.get(index))>max){
                    max=arrayList.get(i)-arrayList.get(index);
                }
            }
            System.out.printf("%.2f\n",max);
        }
    }
}

发表于 2018-09-20 12:35:33 回复(0)
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
 double stock;
 while (scanf("%lf", &stock) != -1)
 {
  double max_profit = 0;
  double min_price = stock;
  for (int i = 1; i <= 8; ++i)
  {
   scanf("%lf", &stock);
   max_profit = max(max_profit, stock - min_price);
   min_price = min(min_price, stock);
  }
  printf("%.2lf\n", max_profit);
 }
}

发表于 2017-07-21 22:25:18 回复(0)
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			double[] arr = new double[9];
			for (int i = 0; i < 9; i ++ )
				arr[i] = sc.nextDouble();
			double max = 0;
			for (int i = 0; i < 9; i ++ )
				max = max > profit(arr, i) ? max : profit(arr, i);
			System.out.printf("%.2f\n", max);
		}
	}

	public static double profit(double[] arr, int end) {
		double min = Integer.MAX_VALUE;
		for (int i = 0; i <= end; i ++ )
			min = min < arr[i] ? min : arr[i];
		return arr[end] - min;
	}
}

编辑于 2016-10-13 18:25:43 回复(0)
import java.util.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
		double[] temp=new double[9];
		
		while(in.hasNext()){
			for(int i=0;i<9;++i){
				temp[i]=in.nextDouble();
			}
			

			double[] re=new double[9];
			re[0]=0.0;
			int min=0;
			double out=re[0];
			for(int i=1;i<9;++i){
				if(temp[i]>temp[i-1])
					re[i]=Math.max(temp[i]-temp[min], re[i-1]);
				else{
					re[i]=re[i-1];
					if(temp[i]<temp[min])
						min=i;
				}
				
				if(out<re[i])
					out=re[i];
			}

			System.out.println(String.format("%.2f", out));
        }
    }
}


编辑于 2016-07-01 12:21:43 回复(0)
import java.util.Scanner;
/**
 * 当柳梢下的集句 尽数流过眼底
 * 拈一缕春风浅浅作序
 * 待到行间字里
 * 再不是眼***夕
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            double[] prices = new double[9];
            for (int i = 0; i < prices.length; i++) {
                prices[i] = sc.nextDouble();
            }
            double[] dp = new double[prices.length];
            double max = 0;
            for (int i = 1; i < dp.length; i++) {
                for (int j = 0; j < i; j++) {
                    if (prices[i] > prices[j] && prices[i] - prices[j] > dp[i]) {
                        dp[i] = prices[i] - prices[j];
                    }
                }
                if (dp[i] > max) {
                    max = dp[i];
                }
            }
            System.out.printf("%.2f\n", max);
        }
    }
}
dp即可
编辑于 2018-08-24 10:33:12 回复(0)
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{		
	double num;
	int cnt = 0;
	vector<double> data;
	while(cin >> num)
	{
		data.push_back(num);
		cnt++;
		if (cnt == 9)
		{
			vector<double> res(9, 0);
			for (int i = 1; i < 9; i++)
			{
				for (int j = i - 1; j >= 0; j--)
				{
					if (data[j]<data[i] && res[j] + data[i] - data[j] > res[i])
						res[i] = res[j] + data[i] - data[j];
				}
			}
			sort(res.begin(), res.end());
			printf("%.2f\n", res[8]);
			data.clear();
			cnt = 0;
		}
	}
	return 0;
}

发表于 2017-04-27 15:16:08 回复(0)
#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
    int i = 0,n = 9;//i为控制输入为9次
    double share[9],temp;
    while(cin >> temp)
    {
        share[i] = temp;
        i++;
        if(i == n)//如果输入了9次之后进行算法
        {
            double cha,t = share[0],max = 0;
            for(int j = 1; j < n; j++)
            {
                cha = share[j] - t;//记录差值
                if(cha < 0)//后面的数比当前的小,就将其赋给t
                    t = share[j];
                if(cha > max)//找出最大差值
                    max = cha;
            }
            printf("%.2lf\n",max);
            i = 0;
        }
    }

    return 0;
}

发表于 2016-10-24 23:06:24 回复(0)

动态规划法。从前向后遍历数组,记录当前出现过的最低价格,作为买入价格,并计算以当天价格出售的收益,作为可能的最大收益,整个遍历过程中,出现过的最大收益就是所求

// write your code here cpp
#include<iostream>
#include <iomanip>
#include<vector>
#include<string>
using namespace std;
const int n =9;
int main(){
    double temp;
    int count=0;
    vector<double> data;    
    while(cin>>temp)
    {	
        data.push_back(temp);        
        ++count;
        if(count == n){
            double min_val,max;
            min_val =data[0],max = -1;
            for(int i=1;i<n;++i){
                min_val = data[i]<min_val?data[i]:min_val;
                max = (data[i]-min_val)>max?(data[i]-min_val):max;
            }
            cout<<setiosflags(ios::fixed)<<setprecision(2)<<max<<endl;  
            //cout<<max<<endl;
            data.clear();    
            count =0;
        }     
    }
    return 0;
}

发表于 2016-09-02 07:35:08 回复(0)

问题信息

难度:
8条回答 8755浏览

热门推荐

通过挑战的用户

查看代码