|
|
back to boardCommon BoardA Question about 1149, including a fundamental question about strings in C. Please answer! Can anyone tell me where my mistake is? I get CRASH(ACCESS VIOLATION) but I cannot figure out the reason. Can I not use sprintf? Or I have to use itoa? Please help, because I cannot see where my mistake can be except from the strings' operations #include <stdio.h> #include <string.h> #define MAX 2000 #define MAX_OUT 100000 char a[201][MAX]; char st[MAX_OUT]; int n; int main(void) { int i,j; char s[MAX],s2[MAX]; short sign; scanf("%d",&n); if (n==1) { printf("sin(1)+1\n"); return 0; } /* Builds Ai for each i<=200 */ for (i=1;i<=n;i++) { sign=0; strcat(a[i],"sin(1"); for (j=2;j<=i;j++) { if (sign) strcat(a[i],"+"); else strcat(a[i],"-"); sprintf(s,"%d",j); strcat(a[i],"sin("); strcat(a[i],s); sign=(sign+1)%2; } for (j=1;j<=i;j++) strcat(a[i],")"); } /* Ai are built */ /* Building the output string */ for (i=1;i<n;i++) strcat(st,"("); strcat(st,a[1]); strcat(st,"+"); sprintf(s2,"%d",n); strcat(st,s2); strcat(st,")"); for (i=2;i<n;i++) { strcat(st,a[i]); strcat(st,"+"); sprintf(s2,"%d",(n-i+1)); strcat(st,s2); strcat(st,")"); } strcat(st,a[n]); strcat(st,"+1"); /* Output string built */ printf("%s\n",st); return 0; } Re: A Question about 1149, including a fundamental question about strings in C. Please answer! MAX_OUT is not big enough , i change it to 200000 and got ACC. Btw , ur algorithm is slow and use too much memory :) Allow me not to agree with your comment First, 10x. I agree that the algorithm is slow. BUT I just cannot agree that this is bad for a solution. Imagine you are at a contest. Imagine I solve the problem in 20 minutes, just as I use a solution of about 50 rows code. It's simple, uses lots of memory, works lots of time, but under the limit. You write your solution for 1 hour. It's faster, better. In the end, we both get the same quantity of points. BUT, I have had 40 minutes more to think over the other problems. I saw this problem and wrote the simplest solution. And it worked. I just cannot make up another solution(I have not tried hard, but....). Tell me your faster solution at subssi@yahoo.com if you don't mind. |
|
|