原文:

给定一个整数 n ,任务是打印所有合适的分数,使分母小于或等于 n

适当分数:如果分子小于分母,则称分数为适当分数。

示例:

输入:n = 3 t3】输出: 1/2,1/3,2/3

输入:n = 4 t3】输出: 1/2、1/3、1/4、2/3、3/4

方法: 遍历【1,n-1】上的所有记数器,对于每个记数器,遍历【分子 1,n】范围内的所有分母,检查分子和分母是否互质。如果发现是互质,那么打印分数。

下面是上述方法的实现:

c 14

// c   program to implement the
// above approach
#include 
using namespace std;
// function to print all
// proper fractions
void printfractions(int n)
{
    for (int i = 1; i < n; i  ) {
        for (int j = i   1; j <= n; j  ) {
            // if the numerator and the
            // denominator are coprime
            if (__gcd(i, j) == 1) {
                string a = to_string(i);
                string b = to_string(j);
                cout << a   "/"   b << ", ";
            }
        }
    }
}
// driver code
int main()
{
    int n = 3;
    printfractions(n);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java program to implement the
// above approach
class gfg{
// function to print all
// proper fractions
static void printfractions(int n)
{
    for(int i = 1; i < n; i  )
    {
        for(int j = i   1; j <= n; j  )
        {
            // if the numerator and the
            // denominator are coprime
            if (__gcd(i, j) == 1)
            {
                string a = string.valueof(i);
                string b = string.valueof(j);
                system.out.print(a   "/"  
                                 b   ", ");
            }
        }
    }
}
static int __gcd(int a, int b)
{
    return b == 0 ? a : __gcd(b, a % b);    
}
// driver code
public static void main(string[] args)
{
    int n = 3;
    printfractions(n);
}
}
// this code is contributed by 29ajaykumar

python 3

# python3 program for the
# above approach
# function to print
# all proper functions
def printfractions(n):
  for i in range(1, n):
    for j in range(i   1, n   1):
      # if the numerator and
      # denominator are coprime
      if __gcd(i, j) == 1:
        a = str(i)
        b = str(j)
        print(a   '/'   b, end = ", ")
def __gcd(a, b):
  if b == 0:
    return a
  else:
    return __gcd(b, a % b)
# driver code
if __name__=='__main__':
  n = 3
  printfractions(n)
# this code is contributed by virusbuddah_

c

// c# program to implement the
// above approach
using system;
class gfg{
// function to print all
// proper fractions
static void printfractions(int n)
{
    for(int i = 1; i < n; i  )
    {
        for(int j = i   1; j <= n; j  )
        {
            // if the numerator and the
            // denominator are coprime
            if (__gcd(i, j) == 1)
            {
                string a = i.tostring();
                string b = j.tostring();
                console.write(a   "/"  
                              b   ", ");
            }
        }
    }
}
static int __gcd(int a, int b)
{
    return b == 0 ? a : __gcd(b, a % b);    
}
// driver code
public static void main(string[] args)
{
    int n = 3;
    printfractions(n);
}
}
// this code is contributed by rutvik_56

java 描述语言


output: 

1/2, 1/3, 2/3,

时间复杂度:o(n2log n) 辅助空间: o(1)