11947 - Cancer or Scorpio
Moderator: Board moderators
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 11947 Cancer or Scorpio Runtime Error.
Post your updated code.
Check input and AC output for thousands of problems on uDebug!
Re: 11947 Cancer or Scorpio Runtime Error.
Already solved. Thanks for the help anyway
-
- New poster
- Posts: 5
- Joined: Mon Jan 26, 2015 1:31 am
Re: 11947 - Cancer or Scorpio
Why getting wrong answer?????
Code: Select all
#include <iostream>
#include <stdlib.h>
#include <cstring>
#include <stdio.h>
#include <sstream>
#include <cmath>
#include <algorithm>
using namespace std;
int str2int(string s){stringstream ss(s);int x;ss >> x;return x;}
string int2str(int n){stringstream ss;ss<<n;return ss.str();}
int fD,fM,fY;
int month[13];
bool isLeapYear(int Y){
if((Y%4==0&&Y%100!=0)||(Y%400==0)) return true;
return false;
}
void calc_Date(){
int curDay = 280;
curDay-=(month[fM]-fD+1);
fM++;fD = 1;
while(true){
if(curDay<month[fM]){
fD+=curDay;
break;
}
curDay-=month[fM];
fM++;
if(fM>12){
fM = 1;
fY++;
}
}
}
void print_res1(int cs){
printf ("%d %02d/%02d/%04d ", cs, fM, fD, fY % 10000);
if((fM==1&&fD>=21)||(fM==2&&fD<=19)) printf("aquarius");
if((fM==2&&fD>=20)||(fM==3&&fD<=20)) printf("pisces");
if((fM==3&&fD>=21)||(fM==4&&fD<=20)) printf("aries");
if((fM==4&&fD>=21)||(fM==5&&fD<=21)) printf("taurus");
if((fM==5&&fD>=22)||(fM==6&&fD<=21)) printf("gemini");
if((fM==6&&fD>=22)||(fM==7&&fD<=22)) printf("cancer");
if((fM==7&&fD>=23)||(fM==8&&fD<=21)) printf("leo");
if((fM==8&&fD>=22)||(fM==9&&fD<=23)) printf("virgo");
if((fM==9&&fD>=24)||(fM==10&&fD<=23)) printf("libra");
if((fM==10&&fD>=24)||(fM==11&&fD<=22)) printf("scorpio");
if((fM==11&&fD>=23)||(fM==12&&fD<=22)) printf("sagittarius");
if((fM==12&&fD>=23)||(fM==1&&fD<=20)) printf("capricorn");
printf("\n");
}
int main(){
month[4]=month[6]=month[9]=month[11]=30;
month[1]=month[3]=month[5]=month[7]=month[8]=month[10]=month[12]=31;
int nCase;
string s1;
cin>>nCase;
for(int cs = 1;cs<=nCase;cs++){
cin>>s1;
month[2] = 28;
fM = str2int(s1.substr(0,2));
fD = str2int(s1.substr(2,2));
fY = str2int(s1.substr(4,4));
if(fM>2){
if(isLeapYear(fY+1)==true) month[2] = 29;
}else if(isLeapYear(fY)==true) month[2] = 29;
calc_Date();
print_res1(cs);
}
}
Last edited by brianfry713 on Tue Mar 31, 2015 12:18 am, edited 1 time in total.
Reason: Added code block
Reason: Added code block
Re: 11947 - Cancer or Scorpio
I don't get the reason I get WA here with the following java code.
It perfectly passes the critical input.
12
01042009
02201987
03261776
04229200
05221999
06191887
07211654
08222222
09203001
10214567
11292160
02011914
I already compared it a lot to the suggested solution on http://www.algorithmist.com/index.php/UVa_11947 and I found out that the input of:
1
01010000
leads to problems in this example (it will always print "capricorn" after the line with year 0000)
I tried to replicate this behaviour but also got WA.
I very much appreciate any kind of hint or explanation to my struggles.
mfg Joerg
Code: Select all
public class Main{
public static void main(String[] args) throws Exception{
//boolean bug = false;
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(reader.readLine());
for (int i = 0; i < N; ++i){
String date = reader.readLine();
int month = Integer.parseInt(date.substring(0,2)) - 1; // zero based in Gregorian Calendar
int day = Integer.parseInt(date.substring(2, 4));
int year = Integer.parseInt(date.substring(4,8));
//if (year == 0) bug = true;
//System.out.printf("YY: %d\n", year);
GregorianCalendar gregor = new GregorianCalendar(year, month, day);
//System.out.printf("YY2: %d\n", gregor.get(Calendar.YEAR));
gregor.add(Calendar.DAY_OF_MONTH, 40*7);
int m = gregor.get(Calendar.MONTH) + 1;
int d = gregor.get(Calendar.DAY_OF_MONTH);
int y = gregor.get(Calendar.YEAR);
//System.out.printf("YY: %d\n", y);
System.out.printf("%d %02d/%02d/%04d", i+1, m, d, y);
String[] zodiacs = {"", "aquarius", "pisces", "aries", "taurus",
"gemini", "cancer", "leo", "virgo", "libra", "scorpio",
"sagittarius", "capricorn"};
int[] begins = { 0, 21, 20, 21, 21, 22, 22, 23, 22, 24, 24, 23, 23 };
int[] ends = { 0, 19, 20, 20, 21, 21, 22, 21, 23, 23, 22, 22, 20 };
//if (bug == true) System.out.printf(" capricorn\n");
//else{
for (int mon = 1; mon <= 12; mon++){
if ( (m == mon && d >= begins[mon]) || (m == (mon + 1) % 12 && d <= ends[mon]) ){
System.out.printf(" %s\n", zodiacs[mon]);
break;
}
}
//}
}
}
}
12
01042009
02201987
03261776
04229200
05221999
06191887
07211654
08222222
09203001
10214567
11292160
02011914
I already compared it a lot to the suggested solution on http://www.algorithmist.com/index.php/UVa_11947 and I found out that the input of:
1
01010000
leads to problems in this example (it will always print "capricorn" after the line with year 0000)
I tried to replicate this behaviour but also got WA.
I very much appreciate any kind of hint or explanation to my struggles.
mfg Joerg