Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. // PitagorTrijniekv5.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. bool readNumbers(const char FileName[], int& givenNumber) {
  8. FILE* file;
  9. char t;
  10. fopen_s(&file, FileName, "rt");
  11. if (!file) return false;
  12. fscanf_s(file, "%c=%i", &t, 1, &givenNumber);
  13. fclose(file);
  14. return t == 'K';
  15. }
  16.  
  17. void writeNumber(const char FileName[], int a, int b, int c) {
  18. FILE* file;
  19. fopen_s(&file, FileName, "at");
  20. fprintf(file, "%i %i %i\n", a, b, c);
  21. fclose(file);
  22. }
  23.  
  24. void trueRoute(int givenNumber) {
  25. //cout << "check";
  26. int count = 0;
  27. for (int c = 1; c <= INT_MAX; c++) {
  28. for (int b = 1; b <= c; b++) {
  29. for (int a = 1; a <= b; a++) {
  30. if ((a * a) + (b * b) == (c * c)) {
  31. count += 1;
  32. writeNumber("output.txt", a, b, c);
  33. if (count >= givenNumber) { break; }
  34. }
  35. }
  36. if (count >= givenNumber) { break; }
  37. }
  38. if (count >= givenNumber) { break; }
  39. }
  40. }
  41.  
  42. void falseRoute(int givenNumber) {
  43. //cout << "check2";
  44. for (int c = 1; c <= givenNumber; c++) {
  45. for (int b = 1; b <= c; b++) {
  46. for (int a = 1; a <= b; a++) {
  47. if ((a * a) + (b * b) == (c * c)) {
  48. writeNumber("output.txt", a, b, c);
  49. //cout << a << "; " << b << "; " << c << endl;
  50. }
  51. }
  52. }
  53. }
  54. }
  55. int main()
  56. {
  57. int givenNumber;
  58. remove("output.txt");
  59. if (readNumbers("input.txt", givenNumber)) {
  60. trueRoute(givenNumber);
  61. }
  62. else {
  63. falseRoute(givenNumber);
  64. }
  65. return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement