fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <math.h>
  4.  
  5. using namespace std;
  6.  
  7. string fractionToDecimal(int numerator, int denominator) {
  8. double res = (double)numerator/denominator;
  9. string res_s = to_string(res);
  10. unordered_map<char,int> hashtable;
  11. int cur = 0;
  12. while(res_s[cur]!='.'){
  13. cur++;
  14. }
  15. if(cur == res_s.size()) return res_s;
  16. cur++;
  17. while(cur < res_s.size()){
  18. if(hashtable.find(res_s[cur]) == hashtable.end()){
  19. hashtable[res_s[cur]] = cur;
  20. }
  21. else{
  22. res_s.insert(res_s[cur],"(");
  23. res_s.insert(cur+1,")");
  24. res_s.erase(res_s.begin()+cur+2,res_s.end());
  25. break;
  26. }
  27. cur++;
  28. }
  29. return res_s;
  30. }
  31.  
  32. int main() {
  33. cout << fractionToDecimal(1,5) << endl;
  34.  
  35. // your code goes here
  36. return 0;
  37. }
Compilation error #stdin compilation error #stdout 0s 3432KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘std::string fractionToDecimal(int, int)’:
prog.cpp:9:37: error: ‘to_string’ was not declared in this scope
         string res_s = to_string(res);
                                     ^
prog.cpp:10:9: error: ‘unordered_map’ was not declared in this scope
         unordered_map<char,int> hashtable;
         ^
prog.cpp:10:23: error: expected primary-expression before ‘char’
         unordered_map<char,int> hashtable;
                       ^
prog.cpp:10:23: error: expected ‘;’ before ‘char’
prog.cpp:15:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if(cur == res_s.size()) return res_s;
                              ^
prog.cpp:17:32: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         while(cur < res_s.size()){
                                ^
prog.cpp:18:16: error: ‘hashtable’ was not declared in this scope
             if(hashtable.find(res_s[cur]) == hashtable.end()){
                ^
stdout
Standard output is empty