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都做减法,找最大值。
看了后面的输入输出例子之后就比较明了了。
首先给出了这一年的投资总额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; }
相关文章推荐
- Problem B. Investing at the Market 问题B 市场投资 解决办法 Python方法
- Google Code Jam 2011 Qualification Round 资格赛 Problem B. Investing at the Market 问题B.市场投资
- 股票市场问题(The Stock Market Problem)
- MYSQL 执行Insert语句throws "The table 'xxx' is full" 的问题分析及解决办法
- Apache [error] server reached MaxClients setting, consider raising the MaxClients setting问题及解决办法
- 发布iOS程序出现“an error occurred uploading to the itunes store”问题的解决办法
- nova 问题分析及解决办法(一)
- Android项目导入出现The type java.lang.Object cannot be resolved.问题解决办法!! .
- tomcat出现Could not load the Tomcat server configuration at 。。异常解决办法
- Eclipse Android开发遇到:"The type org.apache.http.HttpResponse cannot be resolved."问题的解决办法
- Android开发中遇到的问题(五)——Eclipse导入Android项目出现"Invalid project description overlaps the location of another project"错误的解决办法
- from __future__ imports must occur at the beginning of the file问题解决
- cygwin This indicates that the /etc/passwd (and possibly /etc/group) files should be rebuilt 问题解决办法
- Eclipse中编写servlet时出现"The import javax.servlet cannot be resolved" 问题解决办法
- WordPress升级版本及安装插件出现”Problem with the SSL CA cert”的解决办法
- php中session_start()相关问题分析与解决办法
- VMware虚拟机ubuntu和主操作系统硬盘之间的文件共享的实现和What is the location of the directory of C header files that match your running问题的解决办法
- RAC ASM磁盘扩容执行start_udev命令导致vip漂移问题分析及解决办法
- MySQL提示:The server quit without updating PID file问题的解决办法
- The connection to adb is down, and a severe error has occured.问题解决办法