c_cpp 我在参与最近的编码挑战时写的C ++程序。对象是在文本文件中读取并遵循指定的逻辑。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 我在参与最近的编码挑战时写的C ++程序。对象是在文本文件中读取并遵循指定的逻辑。相关的知识,希望对你有一定的参考价值。


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <fstream>

using namespace std;

struct Legit {
    string city;
    string color;
    int legitScore;
};

int main() {

    ifstream infile("/Users/eroth/Dropbox/googlegames/google1/sample.txt");
    
    int numTestCases; // Total number of test cases per file
    int numPantsToCompare; // Number of pants per case
    int legitTime = 0; // Keeps track of number of legit time per case
    bool legitInSameCity = false; // Keeps track of whether we already have a legit match in the same city
    bool legitSoFar = false; // Running counter of whether we're legit so far; i.e., if we have a unique case
    
    infile >> numTestCases;

    // Execute loop numTestCases amount of times
    for (int x = 0; x < numTestCases; x++) {
        infile >> numPantsToCompare;
        
        // Create array of Legit objects equal in size to numPantsToCompare for each test case
        Legit *test[numPantsToCompare];
        
        // Create an instance of a new legit object for each index, read a line from the file into it, and store it in our array
        for (int i = 0; i < numPantsToCompare; i++) {
            Legit *legit  = new Legit();
            infile >> legit->city >> legit->color >> legit->legitScore;
            test[i] = legit;
        }
        
        // Comparison algorithm
        for (int i = 0; i < numPantsToCompare; i++) {
            for (int k = 0; k < numPantsToCompare; k++) {
                // We don't want to compare a case to itself, so if the indices are equal increment k, except if k = numPantsToCompare
                if (i == k) {
                    k++;
                    if (k == numPantsToCompare) break;
                }
                
                # pragma mark debug
                // Outputs comparison cases
                cout << "comparing i:" << i << " " << test[i]->city << " " << test[i]->color << " " << test[i]->legitScore << endl;
                cout << "to k:" << k << " " << test[k]->city << " " << test[k]->color << " " << test[k]->legitScore << endl;
                
                if (test[i]->color == test[k]->color) {
                    if ((test[i]->city != test[k]->city) && (test[i]->legitScore == test[k]->legitScore)) {
                        // If the cities aren't equal it means we won't have any legitTime, so set it to false and break
                        legitSoFar = false;
                        break;
                    }
                    else if ((test[i]->city == test[k]->city) && (test[i]->legitScore == test[k]->legitScore) && !legitInSameCity) {
                        // Addresses case we have a legit match in the same city
                        legitInSameCity = true;
                        break;
                    }
                }
                else {
                    // If we've come this far, we're at the end of a comparison loop and we're legit
                    legitSoFar = true;
                }
            }
            if (legitSoFar && !legitInSameCity) {
                // If we're legitSoFar and we don't have a match in the same city already, increment our legitTime
                legitTime++;
                legitSoFar = false;
            }
            legitInSameCity = false;

        }
        // Output our legit time for this case
        cout << "Case #" << x+1 << ": " << legitTime << endl;
        
        // Reset everything for the next loop
        legitInSameCity = false;
        legitTime = 0;
    }

    
    return 0;
}

以上是关于c_cpp 我在参与最近的编码挑战时写的C ++程序。对象是在文本文件中读取并遵循指定的逻辑。的主要内容,如果未能解决你的问题,请参考以下文章

c语言在哪里运行,怎么保存,后缀名是啥。

C语言的宏定义问题

C语言的宏定义问题

android学习路线图

JavapythonC/C++程序的执行过程

c++20 协程本质