356  Square Pegs And Round Holes
Moderator: Board moderators
Help...........356 WA
I think I have problem creating formula
My formula is:
Cells contain segments of the circle is =8*n4
And
Cells completely contained in the circle is=m=2*n*(n1)
please help
My formula is:
Cells contain segments of the circle is =8*n4
And
Cells completely contained in the circle is=m=2*n*(n1)
please help

 Experienced poster
 Posts: 136
 Joined: Tue Apr 01, 2003 6:59 am
 Location: Jakarta, Indonesia
The first formula is okay. But the second?? it's very far from correct. The failure will occur when n>=5. For n=5, the cells completely contained in the circles are 76, not 40.
I solved this prob using looping method since my geometry isn't so good.
I solved this prob using looping method since my geometry isn't so good.
There are 3 things one need to be successful : motivation, ability and chance. And if you're a believer, make it four : God's will.

 Experienced poster
 Posts: 136
 Joined: Tue Apr 01, 2003 6:59 am
 Location: Jakarta, Indonesia

 Experienced poster
 Posts: 136
 Joined: Tue Apr 01, 2003 6:59 am
 Location: Jakarta, Indonesia
Well, as far as I know, extra blank lines commonly will only cause P.E.
I suggest not to mix the non floatingpoint data type (int & long) with floatingpoint data type (float, double and long double). The method seems alright to me. Just change the int and long type with float or double. As for the extra blank lines, you may use:
[cpp]
if (line) printf("\n");
line=1;
touches_circle = 8*n4;
[/cpp]
Good luck!!
I suggest not to mix the non floatingpoint data type (int & long) with floatingpoint data type (float, double and long double). The method seems alright to me. Just change the int and long type with float or double. As for the extra blank lines, you may use:
[cpp]
if (line) printf("\n");
line=1;
touches_circle = 8*n4;
[/cpp]
Good luck!!
There are 3 things one need to be successful : motivation, ability and chance. And if you're a believer, make it four : God's will.
Formula?.
Greetings!.
I solved problem 356 (http://onlinejudge.uva.es/cgibin/Onli ... Info:42085), but I read there's some formula for it.
Will any of you please email me that formula and why it works?.
Thanks in advance.
bernardolg@cantv.net
I solved problem 356 (http://onlinejudge.uva.es/cgibin/Onli ... Info:42085), but I read there's some formula for it.
Will any of you please email me that formula and why it works?.
Thanks in advance.
bernardolg@cantv.net
_.
356  Square Pegs And Round Holes
My program has the same output with an AC code from n = 1 to n = 150, but I can't understand why it got WA.
[cpp]#include <iostream>
using namespace std;
int
main(void)
{
int n;
bool first = true;
while (cin >> n) {
if (!first)
cout << endl;
first = false;
double radiusp2 = n;
radiusp2 = 0.5;
radiusp2 *= radiusp2;
int segment = 0;
int complete = 0;
for (double i = 0; i < n; i++) {
for (double j = 0; j < n; j++) {
if ( (i+1)*(i+1) + (j+1)*(j+1) < radiusp2 ) {
complete++;
} else if ( i*i + j*j < radiusp2 ) {
segment++;
}
}
}
cout << "In the case n = " << n << ", " << segment * 4
<< " cells contain segments of the circle." << endl
<< "There are " << complete * 4 << " cells completely contained in the circle." << endl;
}
return 0;
}[/cpp]
[cpp]#include <iostream>
using namespace std;
int
main(void)
{
int n;
bool first = true;
while (cin >> n) {
if (!first)
cout << endl;
first = false;
double radiusp2 = n;
radiusp2 = 0.5;
radiusp2 *= radiusp2;
int segment = 0;
int complete = 0;
for (double i = 0; i < n; i++) {
for (double j = 0; j < n; j++) {
if ( (i+1)*(i+1) + (j+1)*(j+1) < radiusp2 ) {
complete++;
} else if ( i*i + j*j < radiusp2 ) {
segment++;
}
}
}
cout << "In the case n = " << n << ", " << segment * 4
<< " cells contain segments of the circle." << endl
<< "There are " << complete * 4 << " cells completely contained in the circle." << endl;
}
return 0;
}[/cpp]

 Experienced poster
 Posts: 154
 Joined: Sat Apr 17, 2004 9:34 am
 Location: EEE, BUET
Can be easier
You can solve this problem in an easier way too. Do some pen & paper works for the first few values of n i.e. n = 1,2,3,4... & you will find 2 formula refer to Concrete Math (Knuth) , floor & ceiling functions chapter.
You should never take more than you give in the circle of life.
 dovier_antonio
 New poster
 Posts: 47
 Joined: Fri Feb 18, 2005 5:00 am
 Location: Havana, Cuba
Help me... I have WA!!!!
My solution for the problem 356 is :
#include <stdio.h>
int main() {
int n;
while (scanf("%d", &n) != EOF) {
printf("In the case n = %d, %d cells contain segments of the circle.\n", n, 8 * n  4);
printf("There are %d cells completely contained in the circle.\n\n", 2 * n * (n  1));
}
return 0;
}
#include <stdio.h>
int main() {
int n;
while (scanf("%d", &n) != EOF) {
printf("In the case n = %d, %d cells contain segments of the circle.\n", n, 8 * n  4);
printf("There are %d cells completely contained in the circle.\n\n", 2 * n * (n  1));
}
return 0;
}

 Experienced poster
 Posts: 154
 Joined: Sat Apr 17, 2004 9:34 am
 Location: EEE, BUET
You need to use long int for calculation. Otherwise your program will fail to inputs greater than some 104 or 105, I guess.
& also, why not mentioning the problem number & name in the topic? Surely it increases the chance of getting a reply.
& also, why not mentioning the problem number & name in the topic? Surely it increases the chance of getting a reply.
You should never take more than you give in the circle of life.