300 - Maya Calendar

All about problems in Volume 3. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

zaj_tam
New poster
Posts: 5
Joined: Thu Dec 25, 2003 5:44 pm

Post by zaj_tam »

Hm. I checked spelling. Is seems to be ok. Then why would windows run it without complaining? I'll try to check the arrays, not likely that i will find something (a true newbie :) ) though ...


thx for the tips anyway :)

little joey
Guru
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

Post by little joey »

Your arrays are too small. A string of 6 letters uses 7 chars: 6 for the letters and 1 for the terminating '\0'.
strcpy(haab_month[13],"hankin") will copy 7 chars starting from haab_month[13][0] and ending at haab_month[14][0], which will recieve the '\0' character.

zaj_tam
New poster
Posts: 5
Joined: Thu Dec 25, 2003 5:44 pm

Post by zaj_tam »

Thx. I fixed that, but still:

Code: Select all

Your program has died with signal 11 (SIGSEGV). Meaning:

    Invalid memory reference

Before crash, it ran during 0.002 seconds.
Anyway, i dont get it. The string is 6 chars long. The array is smth[6] - > that means 7 chars: smth[0] smth[1] smth[2] smth[3] smth[4] smth[5] smth [6], doesnt it? Or is the last one terminating zero for array? I guess so.

The code, again:

Code: Select all

/* @JUDGE_ID: 40671PZ 300 C++ */ 
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdio.h>

using namespace std;

int get_month (char name [7],char haab_month [19][7]);

int main(int argc, char *argv[])
{

  int haab_year = 0;
  int tzolkin_year = 0;
  int num_entries;
  int current_month_haab;
  
  char haab_month [19][7];
  
  strcpy(haab_month[0], "pop");
  strcpy(haab_month[1], "no");
  strcpy(haab_month[2], "zip");
  strcpy(haab_month[3], "zotz");
  strcpy(haab_month[4], "tzec");
  strcpy(haab_month[5], "xul");
  strcpy(haab_month[6], "yoxkin");
  strcpy(haab_month[7], "mol");
  strcpy(haab_month[8], "chen");
  strcpy(haab_month[9], "yax");
  strcpy(haab_month[10], "zac");
  strcpy(haab_month[11], "ceh");
  strcpy(haab_month[12], "mac");
  strcpy(haab_month[13], "kankin");
  strcpy(haab_month[14], "muan");
  strcpy(haab_month[15], "pax");
  strcpy(haab_month[16], "koyab");
  strcpy(haab_month[17], "cumhu");
  strcpy(haab_month[18], "uayet");
  
  
  char tzolkin_day [20][9];
  int tzolkin_period_count = 13;
  
  strcpy(tzolkin_day[0], "imix");
  strcpy(tzolkin_day[1], "ik");
  strcpy(tzolkin_day[2], "akbal");
  strcpy(tzolkin_day[3], "kan");
  strcpy(tzolkin_day[4], "chicchan");
  strcpy(tzolkin_day[5], "cimi");
  strcpy(tzolkin_day[6], "manik");
  strcpy(tzolkin_day[7], "lamat");
  strcpy(tzolkin_day[8], "muluk");
  strcpy(tzolkin_day[9], "ok");
  strcpy(tzolkin_day[10], "chuen");
  strcpy(tzolkin_day[11], "eb");
  strcpy(tzolkin_day[12], "ben");
  strcpy(tzolkin_day[13], "ix");
  strcpy(tzolkin_day[14], "mem");
  strcpy(tzolkin_day[15], "cib");
  strcpy(tzolkin_day[16], "caban");
  strcpy(tzolkin_day[17], "eznab");
  strcpy(tzolkin_day[18], "canac");
  strcpy(tzolkin_day[19], "ahau");
  
  int buff_first [20];
  char buff_second [20][9];
  int buff_third [20];
  
  char a [4], b [7];
  int c;
  int announce;
  int iks = 0;
  scanf("%d", &announce);
  while ( iks < announce )
  {
    scanf("%s", a);
    scanf("%s", b);
    scanf("%d", &c);
  
    int haab_days;
    haab_days = get_month (b,haab_month) * 20 + atoi(a) + (365 * c);
  
    int tzolkin_first_conv = (haab_days + 1) % 13;
  
    char tzolkin_char_conv [9];
    strcpy(tzolkin_char_conv, tzolkin_day[(((haab_days + 1) % 20)-1)]);
  
    int tzolkin_year_conv = (haab_days - tzolkin_first_conv) / 260;
    if ( tzolkin_year_conv >= 1 ) { } else { tzolkin_year_conv = 0; }
    buff_first[iks] = tzolkin_first_conv;
    strcpy(buff_second[iks], tzolkin_char_conv);
    buff_third[iks] = tzolkin_year_conv;
    iks++;
  }
  for ( int pimp; pimp < iks; pimp++ )
  {
    printf("%d %s %d\n", buff_first[pimp], buff_second[pimp], buff_third[pimp]);
  }
  return 0;
}

int get_month (char name [7],char haab_month [19][7]) 
{
  int n = 0;
  while ( 0 != strcmp( name, haab_month[n] ) )
  {
    n++;
  }
  return n;
}

little joey
Guru
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

Post by little joey »

zaj_tam wrote:Anyway, i dont get it. The string is 6 chars long. The array is smth[6] - > that means 7 chars: smth[0] smth[1] smth[2] smth[3] smth[4] smth[5] smth [6], doesnt it? Or is the last one terminating zero for array? I guess so.
No, it means 6 chars smth[0] .. smth[5].

I tried to run your code, but it won't even give (correct) answers for the sample case. Also I see you can handle a maximum of 20 cases, while this number can and will be a lot bigger.

One advise (no offence):
Read an elementary book on C/C++ before solving this kind of problems. Also get yourself a good debugger and watch carefully what the code that you write realy does. That can be highly instructive when learning the language.

zaj_tam
New poster
Posts: 5
Joined: Thu Dec 25, 2003 5:44 pm

Post by zaj_tam »

Its hard to get a book in my language. Can you provide me with some links(online howtos)?

thx

(btw: i fucked the other code. first gives results, if you can run it ;))

EDIT: wohoo. I am on wrong answer. and i am happy ... debuging ;) Should i remove the code?

AaronWu
New poster
Posts: 14
Joined: Sat May 10, 2003 4:21 am

300- WA ??

Post by AaronWu »

What's the trap of the problem?
It runs well on my computer , can anybody tell me the problem..
Thank you in advance.

another question is that the following codes don't concern with C++
but if I take away the first line and submit it for C,it will cause compile error. I am confused :o :o


[c]#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void format(int day, int month, int year, char t[][10])
{
int totaldays;
int trest;
int tyear,tmonth,tday;
totaldays = year*365+month*20+day+1;
trest = totaldays%260;

tyear = totaldays/260;
tmonth =(trest-1)%20;
tday = trest%13;
printf("%d %s %d\n",tday, t[tmonth], tyear);

}
int main()
{
char m[][10] = {"pop", "no", "zip", "zotz", "tzec", "xul", "yoxkin"
, "mol", "chen", "yax", "zac", "ceh", "mac", "kankin", "muan",
"pax", "koyab", "cumhu","uayet"};

char t[][10] = {"imix", "ik", "akbal", "kan", "chicchan", "cimi",
"manik", "lamat", "muluk", "ok", "chuen", "eb", "ben", "ix",
"mem", "cib", "caban", "eznab", "canac", "ahau"};
int number;
int i;
int j;
int day;
int month;
int year;
char cday[4];
char cmonth[8];
char cyear[5];
scanf("%d" ,&number);
printf("%d\n" , number);
for(i=0;i<number;i++)
{
scanf("%s",cday);
cday[strlen(cday)-1]='\0';
day=atoi(cday);
scanf("%s",cmonth);
for(j=0;j<20;j++)
{
if(strcmp(cmonth,m[j])==0)
break;
}
month = j;
scanf("%s", cyear);
year= atoi(cyear);

format(day, month, year,t);
}
return 0;
}[/c]
To save time is to lengthen life.

GreenPenInc
Learning poster
Posts: 53
Joined: Sat May 01, 2004 9:31 pm
Contact:

Post by GreenPenInc »

Your problem seems to stem (correct me if I'm wrong) from a misunderstanding of how mod works. For example, 26 mod 13 gives 0, not 13.

By some strange coincidence, I also started working on this problem today and I got a weird segfault followed by a WA. I'm not sure what's up, but hopefully I'll remember to post if I get AC.
_-(GPI)-_

"Finally I have freed myself from the clutches of the garbage fairy!"

GreenPenInc
Learning poster
Posts: 53
Joined: Sat May 01, 2004 9:31 pm
Contact:

Post by GreenPenInc »

Hah! That was quick! :)

Helps if you copy-paste all the months, not just the ones you like. Turns out I'd missed the twentieth holy month. MAY THE GODS BE MERCIFUL UNTO ME!

Here's a very useful debugging tool: write a program to generate an input file which prints out the first 2 years of creation in the non-holy calendar. Use this as the input file for your program, then check your output for the patterns in the holy calendar. Worked like a charm! In fact, hey, I'll even copy-paste the code. :)

[cpp]
#include <iostream>

using namespace std;

/* Globals */
string months[] = {"pop", "no", "zip", "zotz", "tzec", "xul", "yoxkin", "mol", "chen", "yax", "zac", "ceh", "mac", "kankin", "muan", "pax", "koyab", "cumhu", "uayet"};
string holy_months[] = {"imix", "ik", "akbal", "kan", "chicchan", "cimi", "manik", "lamat", "muluk", "ok", "chuen", "eb", "ben", "ix", "mem", "cib", "caban", "eznab", "canac"};
int main(int argc, char **argv)
{
int count = 0;
for (int i = 0; i < 2; i++) // year
{
for (int j = 0; j < 19; j++) // month
{
for (int k = 0; k < 20; k++) //day
{
if (k >= 5 && j == 18) break; // last month is short
cout << k << ". " << months[j] << " " << i << endl;
count++;
}
}
}
cout << count;
return 0;
}
[/cpp]

Now, all you have to do is edit the resulting text file and put the number on the bottom, at the top of the file. If you're using vi like you should be, it's as easy as "Gdd1GP". Have fun kids! :)
_-(GPI)-_

"Finally I have freed myself from the clutches of the garbage fairy!"

AaronWu
New poster
Posts: 14
Joined: Sat May 10, 2003 4:21 am

Post by AaronWu »

I'm afraid I don't understand your meaning.
Can you just give me some input cases to show the defect with my codes.
To save time is to lengthen life.

GreenPenInc
Learning poster
Posts: 53
Joined: Sat May 01, 2004 9:31 pm
Contact:

Post by GreenPenInc »

Sure, I'll spell it out for you. The following is a complete, ready-to-go program which will generate an input file. Copy-paste the code into a file called generate.cpp

[cpp]
#include <iostream>

using namespace std;

/* Globals */
string months[] = {"pop", "no", "zip", "zotz", "tzec", "xul", "yoxkin", "mol", "

int main(int argc, char **argv)
{
cout << 730 << endl; // number of test cases
for (int i = 0; i < 2; i++) // year
{
for (int j = 0; j < 19; j++) // month
{
for (int k = 0; k < 20; k++) //day
{
if (k >= 5 && j == 18) break; // last month is s
cout << k << ". " << months[j] << " " << i << en
}
}
}
return 0;
}
[/cpp]

Now do this from the shell.

Code: Select all

$ g++ generate.cpp -o generate
$ ./generate > myinput
At this point the file myinput contains every day, in order, for the first two years, in the normal calendar. It is a ready-to-go input file for the *real* program. Now to run the real program with the file you just made as input, do the following from the shell, assuming the name of your program is "a.out":

Code: Select all

$ ./a.out < myinput > myoutput
Now, the file "myoutput" contains the first 730 days in the holy calendar. Check through the file to see whether it matches the pattern that it should.
_-(GPI)-_

"Finally I have freed myself from the clutches of the garbage fairy!"

AaronWu
New poster
Posts: 14
Joined: Sat May 10, 2003 4:21 am

Post by AaronWu »

Thank you ,I see.
To save time is to lengthen life.

HerrAachen
New poster
Posts: 12
Joined: Sat Apr 03, 2004 11:01 pm

Post by HerrAachen »

Can any of you lucky people that gotten accepted provide me with some Input for this Maya problem? I get WA and cant figure why.

Thanx in advance

Herr Aachen

prasad_3483
New poster
Posts: 1
Joined: Thu Jun 30, 2005 8:47 am

VIII - 300 Maya Calendar

Post by prasad_3483 »

Hello there !
I got WA : for the following C code, I couldn't figure out why, could someone pls
pin-point the error(s) in it

Code: Select all

#include <stdio.h>
#include <string.h>

typedef struct 
{
	int day;
	char dname[10];
	int year;
} Date;

char * mnth[] = { "", "pop", "no", "zip", "zotz", "tzec", "xul", "yoxkin", \
			   "mol", "chen", "yax", "zac", "ceh", "mac", "kankin", "muan",\
			   "pax", "koyab", "cumhu" 
		      };

char * days[] = { "", "imix", "ik", "akbal", "kan", "chicchan", "cimi", \
			   "manik", "lamat", "muluk", "ok", "chuen", "eb", "ben", "ix",\
			   "mem", "cib", "caban", "eznab", "canac", "ahau"
		      };	

/* @param darr: pointer to an array of structure date
 * @param n: no of input dates to read in
 */
void read_input(Date * darr, int n)	
{
	int i = 0;
	for(i = 0; i < n; i++)
		scanf("%d%*c%s%d", &darr[i].day, darr[i].dname, &darr[i].year);
	return;
}

/* @param darr: arrays of input Haab dates
 * @param n: no of input dates
 */
void find_tzolkin_date(Date * darr, int n)
{
	int i = 0, m = 0, d = 0, y = 0;
	unsigned long t = 0;
	printf("%d\n", n);
	for(i = 0; i < n; i++)
	{
		m = get_mnth_indx(darr[i].dname);
		if(m > 0)
		{
			t = (darr[i].year * 365) + ((m - 1) * 20) + darr[i].day + 1;
			m = t % 20;
			d = t % 13;
			y = t / 260;
			printf("%d %s %d\n", d, days[m], y);
		}
		else
			fprintf(stderr, "%s: month index negative !\n", __FUNCTION__);
	}
	return;
}

/* @param name: name of the month, whos index is expected */
int get_mnth_indx(char * name)
{
	int i = 0;
	for(i = 1; i < 20; i++)
	{
		if(strcmp(name, mnth[i]) == 0)
			return (i);
	}
	return (-1);
}

/* @param argc: no of command line arguments
 * @param argv: array of command line arguments
 */
int main(int argc, char * argv[])
{
	int no = 0;
	Date * arr = NULL;
	scanf("%d", &no);
	arr = malloc(no * sizeof(Date));
	if(arr != NULL)
	{
		read_input(arr, no);
		find_tzolkin_date(arr, no);
	}
	return (0);
}
I've submitted my solution three times for this problem and everytime I got WA...pls someone help me out !!

Sakib
New poster
Posts: 24
Joined: Fri Jan 28, 2005 5:27 pm
Location: Bangladesh

Problem 300 Maya Calender WA !

Post by Sakib »

i am getting WA!!
i use a big array to stroe all the tzolkin dates.
spending a lot of memory.
then i find the index using the haab date input and print the tzolkin date.
everything seems ok.
But WA!!
please give me some inputs.
which is the biggest haab date?
4. cumhu 4999 ?

please give me correct outputs for those -------

10
19. zac 1000
13. pop 2000
18. zac 1995
1. zotz 1000
3. tzec 4000
8. yoxkin 1996
19. mol 1050
17. chen 2050
14. cumhu 1999
0. pop 1010
/* Sorry For Nothing */

mamun
A great helper
Posts: 286
Joined: Mon Oct 03, 2005 1:54 pm
Location: Bangladesh
Contact:

Post by mamun »

which is the biggest haab date?
4. cumhu 4999 ?
The last month is uayet
Outputs for your inputs are
  • 10
    11 ahau 1404
    12 ix 2807
    4 ix 2801
    9 ik 1404
    2 kan 5615
    6 muluk 2802
    1 ok 1474
    5 lamat 2878
    1 ok 2807
    10 chuen 1417
Read other posts about this topic.

Post Reply

Return to “Volume 3 (300-399)”