您的位置:首页 > 其它

Problem B. Investing at the Market 问题B 市场投资 分析 解决办法

2011-03-15 14:12 459 查看
对英文的阅读理解不是很强大,句子的意思理解了,但是段落的理解还是很困难的。

看了后面的输入输出例子之后就比较明了了。

首先给出了这一年的投资总额M,也就是说一年有M多的钱来投资

接下来给出了12个月份中每个月份的商品的价格P,有多有少

然后只能买卖一次

那么接下来的操作就是

选择一个价格低的买入,选择一个价格高的卖出,中间的差价就是利润

买入的时间必须要比卖出的时间早,当然这是废话,不过也代表着一种逻辑

如果12个月的单价都比M高,那就买不起,利润为IMPOSSIBLE,也就是赚不到钱

最后输出的结果就是输出买入的月份,卖出的月份,和利润

这么理解就顺多了,米国人的思维真是别扭。

下面再从程序的方面分析一下

我们需要有一个N,一个M,和一个P[12]来存放读入的数据

关键是找出买入卖出的月份来

1-11月是买入可选择的月份,选了一个就是月份B

2-12月是卖出可选择的月份,B存在的情况下范围缩小到B-12

怎么找呢

想想,我们首先需要一个S[11]来存放1-11月的P,然后去掉所有M<P的,或者说屏蔽掉,置个0什么的

然后从S中取一个月份的P,假设是B月

在P[12]中,所有B月后面的月份的P都做减法,找最大值。

]#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#define FALSE 0
#define TRUE  !FALSE
#define MaxLine 128
int main(int argc, char *argv[])
{
FILE *fin=NULL, *fout=NULL;
char *filein = "B-small-practice.in";
char *fileout = "resoult.out";
char cLine[MaxLine] = {0};
int  N, M, Bin=0,Bout=0;
int  Max[2],temp, profit=0;
int  P[12] = {0};
int  i,j,k=1;
fin = fopen(filein, "r");
fout = fopen(fileout, "w");
if (fin == NULL) {
fprintf(stderr,"open filein err./n");
return FALSE;
} else if (fout == NULL) {
fprintf(stderr,"open fileout err./n");
return FALSE;
}
while (fgets(cLine, MaxLine, fin) != NULL) { /*一行一行读数据*/
if (k == 1) { //k是用来标识行号的
sscanf(cLine, "%d", &N); /*读出N来*/
//printf("N:%d./n",N);
}
if (k>1 && (k%2==0)) {
sscanf(cLine, "%d", &M); /*读出M来*/
//printf("M:%d./n",M);
}
if (k>1 && (k%2)==1) {
sscanf(cLine, "%d %d %d %d %d %d %d %d %d %d %d %d", /
&P[0],&P[1],&P[2],&P[3],&P[4],&P[5],&P[6],&P[7],&P[8],&P[9],&P[10],&P[11]); /*读出P来*/
#if 0
for (i=0; i<12; i++) {
printf("%d ",P[i]);
}
printf("/n");
#endif
/*----------------------------------*/
//开始分析数据
for (i=0; i<11; i++) {
if (M<P[i]) {
continue;
}
Bin = P[i];
for (j=i+1; j<12; j++) {
Bout = P[j];
temp =(Bout-Bin)*(M/Bin);
if (temp>profit) {
Max[0] = i;
Max[1] = j;
profit = temp;
} else if ((temp==profit)&&(Bin<P[Max[0]])) {
/*
* 如果存在两种利润相同的情况,选择最低价购买
* */
Max[0] = i;
Max[1] = j;
profit = temp;
//printf("%d,%d:%d %d %d/n",i,j,Max[0],Max[1],profit);
}
}
}
/*----------------------------------*/
//printf("%d %d %d/n",Max[0]+1,Max[1]+1,profit);
//printf("%d,%d/n",P[Max[1]]-P[Max[0]],M/P[Max[0]]);
//printf("%d %d %d/n",P[Max[0]],P[Max[1]],profit);
#if 1
if (profit == 0) {
fprintf(fout,"Case #%d: %s/n",((k-2)/2)+1,"IMPOSSIBLE");
} else {
//profit = (P[Max[1]]-P[Max[0]])*(M/P[Max[0]]);
fprintf(fout,"Case #%d: %d %d %d/n",((k-2)/2)+1,Max[0]+1,Max[1]+1,profit);
}
#endif
}
k++;
Max[0]=0;
Max[1]=0;
profit = 0;
}
fclose(fin);
fclose(fout);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐