poj1008(Maya Calendar)玛雅历(c++实现)

这道题目不难,就是简单的模拟算法。但是还是给了我一个教训,就是以后看题要仔细仔细,

因为刚开始老是评测不能过,后来发现是少输出了个日期数,就是一开始把日期数输入了,然后把他输出一遍就可以了。

1、

#include<iostream>
using namespace std;
#include<string>
int main()
{
	int k,n,i,day,year;
	string month[19]={"pop","no","zip","zotz","tzec","xul","yoxkin","mol","chen","yax","zac","ceh","mac","kankin","muan","pax","koyab","cumhu","uayet"};
	string month1[20]={"imix","ik","akbal","kan","chicchan","cimi","manik","lamat","muluk","ok","chuen","eb","ben","ix","mem","cib","caban","eznab","canac","ahau"};
	string mon;
    	cin>>k;
		char s;
	cout<<k<<endl;
  while (k--)
	{
		cin>>day>>s>>mon>>year;
	       for (i=0;i<19;i++)
	          if (mon==month[i]) n=20*i;
                n=year*365+n+day+1;
               year=(n-1)/260;
	    if (n%260==0) n=260;
		  else n=n%260;
		  if (n%13==0) day=13;
	      else day=n%13;
		  if (n%20==0) n=20;
	      else n=n%20;
	 cout<<day<<' '<<month1[n-1]<<' '<<year<<endl;
	}
return 0;
}


程序有点缺陷,那就是费事多了点,用了16ms,内存占了248k。

2、改进:把一维字符串数组改为二维的字符数组,将cin、cout改为scanf、printf。

效率提高了不少。耗时0s,内存为160k。不过与排名靠前的0s,8k,还是有很大距离。

如果大家有什么好方法,请指教,谢谢!

改进后代码:

#include<stdio.h>
#include<iostream>
using namespace std;
#include<string>
int main()
{
	int k,n,i;
	int day,year;
	char month[19][7]={"pop","no","zip","zotz","tzec","xul","yoxkin","mol","chen","yax","zac","ceh","mac","kankin","muan","pax","koyab","cumhu","uayet"};
	char month1[20][9]={"imix","ik","akbal","kan","chicchan","cimi","manik","lamat","muluk","ok","chuen","eb","ben","ix","mem","cib","caban","eznab","canac","ahau"};
	char mon[7];
    	scanf("%d",&k);
    	printf("%d\n",k);
  while (k--)
	{
		scanf("%d.%s%d",&day,mon,&year);
	       for (i=0;i<19;i++)
	          if (!strcmp(mon,month[i])) n=20*i;
                n=year*365+n+day+1;
               year=(n-1)/260;
	    if (n%260==0) n=260;
		  else n=n%260;
		  if (n%13==0) day=13;
	      else day=n%13;
		  if (n%20==0) n=20;
	      else n=n%20;
	 printf("%d %s %d\n",day,month1[n-1],year);
	}
return 0;
}


你可能感兴趣的:(C++,内存,poj)