fork download
  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. #define ep 1e-10
  5.  
  6. void printWithSpace(int a, int nos) {
  7. char s[100];
  8. int size = (int)(log(a) / log(10)) + 1;
  9. int i = size - 1;
  10. s[size] = '\0';
  11. while (i >= 0) {
  12. s[i--] = a % 10 + '0';
  13. a /= 10;
  14. }
  15. nos--;
  16. for (i = 0; i < size; i++) {
  17. std::cout << s[i];
  18. if (nos) {
  19. std::cout << ' ';
  20. nos--;
  21. }
  22. }
  23. std::cout << std::endl;
  24. }
  25.  
  26. void printWithSpace(double f, int nos) {
  27. int a = (int)f;
  28. double b = f - a;
  29. char s[100];
  30. int i = (int)(log(a) / log(10));
  31. s[i + 1] = '.';
  32. int j = i + 2;
  33. while (i >= 0) {
  34. s[i--] = a % 10 + '0';
  35. a /= 10;
  36. }
  37. while (b > ep) {
  38. b *= 10.0;
  39. s[j++] = (int)b + '0';
  40. b -= (double)(int)b;
  41. }
  42. s[j] = '\0';
  43. nos--;
  44. for (i = 0; i < j; i++) {
  45. std::cout << s[i];
  46. if (nos) {
  47. std::cout << ' ';
  48. nos--;
  49. }
  50. }
  51. std::cout << std::endl;
  52. }
  53.  
  54. int main() {
  55. printWithSpace(67514, 4);
  56. printWithSpace(123.456, 4);
  57. return 0;
  58. }
  59.  
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
6 7 5 14
1 2 3 .456