Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // PitagorTrijniekv5.cpp : This file contains the 'main' function. Program execution begins and ends there.
- //
- #include <iostream>
- using namespace std;
- bool readNumbers(const char FileName[], int& givenNumber) {
- FILE* file;
- char t;
- fopen_s(&file, FileName, "rt");
- if (!file) return false;
- fscanf_s(file, "%c=%i", &t, 1, &givenNumber);
- fclose(file);
- return t == 'K';
- }
- void writeNumber(const char FileName[], int a, int b, int c) {
- FILE* file;
- fopen_s(&file, FileName, "at");
- fprintf(file, "%i %i %i\n", a, b, c);
- fclose(file);
- }
- void trueRoute(int givenNumber) {
- //cout << "check";
- int count = 0;
- for (int c = 1; c <= INT_MAX; c++) {
- for (int b = 1; b <= c; b++) {
- for (int a = 1; a <= b; a++) {
- if ((a * a) + (b * b) == (c * c)) {
- count += 1;
- writeNumber("output.txt", a, b, c);
- if (count >= givenNumber) { break; }
- }
- }
- if (count >= givenNumber) { break; }
- }
- if (count >= givenNumber) { break; }
- }
- }
- void falseRoute(int givenNumber) {
- //cout << "check2";
- for (int c = 1; c <= givenNumber; c++) {
- for (int b = 1; b <= c; b++) {
- for (int a = 1; a <= b; a++) {
- if ((a * a) + (b * b) == (c * c)) {
- writeNumber("output.txt", a, b, c);
- //cout << a << "; " << b << "; " << c << endl;
- }
- }
- }
- }
- }
- int main()
- {
- int givenNumber;
- remove("output.txt");
- if (readNumbers("input.txt", givenNumber)) {
- trueRoute(givenNumber);
- }
- else {
- falseRoute(givenNumber);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement