| | |
Long long, unsigned, C & C++ issues
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2008
Posts: 9
Reputation:
Solved Threads: 0
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:
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:
... 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?
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:
C++ Syntax (Toggle Plain Text)
#include<stdio.h> #include<math.h> int main(void) { long long int n; while(scanf("%lld", &n)==1) { if(n==0) break; else if( (long long int)sqrt((double)n) * (long long int)sqrt((double)n) == n) printf("yes\n"); else printf("no\n"); } return 0; }
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:
C++ Syntax (Toggle Plain Text)
#include <stdio.h> #include <math.h> int main() { unsigned int n; while (scanf("%d", &n) == 1) { if (n == 0) break; else { unsigned int root = (unsigned int)sqrt((double)n); if (root*root != n) { printf("no\n"); } else { printf("yes\n"); } } } return 0; }
... 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?
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
the problem with the code using unsigned it is that nowhere in the c++ standard does it say
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.
std::numeric_limits<unsigned int>::max() >= 2^32 - 1 or for that matter,std::numeric_limits<unsigned int>::radix == 2 orstd::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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: How to read integers from an array in a text file and display them on the screen
- Next Thread: Opening a file whilst running program?
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






