Simplified Fractions(C++最简分数)

解题思路:

(1)依次遍历从2-n作为分母

class Solution {
public:
    
    vector<string> simplifiedFractions(int n) {
        vector<string> s;
        int i=2;
        while(i<=n) {
            int j=1;
            while(j<i) {
                int a = __gcd(j,i);
                string temp=to_string(j/a)+"/"+to_string(i/a);
                if(find(s.begin(),s.end(),temp)==s.end()) s.push_back(temp);
                j++;
            }
            i++;
        }
        return s;
    }
};

猜你喜欢

转载自blog.csdn.net/coolsunxu/article/details/114968073