Long long, unsigned, C & C++ issues

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 9
Reputation: Infeligo is an unknown quantity at this point 
Solved Threads: 0
Infeligo Infeligo is offline Offline
Newbie Poster

Long long, unsigned, C & C++ issues

 
0
  #1
Feb 24th, 2008
Hello,

I'm trying to solve an ACM problem "Light, more light". The problem is actually solved, but I can get it accepted. So browsing Internet I found a C code, which is accepted:

  1. #include<stdio.h>
  2. #include<math.h>
  3.  
  4. int main(void) {
  5. long long int n;
  6. while(scanf("%lld", &n)==1) {
  7. if(n==0)
  8. break;
  9. else if( (long long int)sqrt((double)n) * (long long int)sqrt((double)n) == n)
  10. printf("yes\n");
  11. else
  12. printf("no\n");
  13. }
  14. return 0;
  15. }

However, it doesnt' work in Eclipse's console (MinGW GCC toolchain). Why is that?

But the main problem is not that. The task states, that the number won't exceed 2^32 - 1, so in my own solution I used unsigned int:

  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int main()
  5. {
  6. unsigned int n;
  7. while (scanf("%d", &n) == 1)
  8. {
  9. if (n == 0)
  10. break;
  11. else
  12. {
  13. unsigned int root = (unsigned int)sqrt((double)n);
  14. if (root*root != n)
  15. {
  16. printf("no\n");
  17. }
  18. else
  19. {
  20. printf("yes\n");
  21. }
  22. }
  23.  
  24. }
  25.  
  26. return 0;
  27. }

... which is almost the same, works fine in Eclipse's console, but doesn't get accepted on ACM neither as ANSI C, nor as C++. Can anyone tell me where is the mistake?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,381
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Long long, unsigned, C & C++ issues

 
0
  #2
Feb 24th, 2008
what are the errors when you attempt to compile on ACM?
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 9
Reputation: Infeligo is an unknown quantity at this point 
Solved Threads: 0
Infeligo Infeligo is offline Offline
Newbie Poster

Re: Long long, unsigned, C & C++ issues

 
0
  #3
Feb 24th, 2008
Is simply says "Wrong answer".
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Long long, unsigned, C & C++ issues

 
0
  #4
Feb 24th, 2008
the problem with the code using unsigned it is that nowhere in the c++ standard does it say
std::numeric_limits<unsigned int>::max() >= 2^32 - 1 or for that matter,
std::numeric_limits<unsigned int>::radix == 2 or
std::numeric_limits<unsigned int>::digits >= 32 .
so the code may fail for some input values on some implementations.

the code that was accepted is neither ISO C++ nor ANSI C. neither talks about a type long long.
the C99 standard introduced the long long int type ( to be an integral type with at least 64 bits) and it is a long-supported extension by almost all compilers. (C++0x adds this type as well as unsigned long long as fundamental types). by now, long long int (abbreviated as long long) has become a de facto standard (it is de jure only in C99). which is probably why it was accepted.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC