zoukankan      html  css  js  c++  java
  • 【C语言程序】法雷数列

    对任意给定的一个自然数n,将分母小于等于n的不可约的真分数按升序排列,并且在第一个分数之前加上0/1,在最后一个分数之后加上1/1,这个序列称为n级法雷数列,以Fn表示。如F5为:0/1,1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5,1/1.其元素个数为11.

    现在给出n让你求其n级法雷数列中元素的排列和个数。

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 int n,s; 
     4 void pre(int a,int b,int c,int d){//利用二分法输出法雷数列 
     5     if(b+d>n) return ;
     6     pre(a,b,a+c,b+d);
     7     printf("%d/%d,",a+c,b+d);
     8     pre(a+c,b+d,c,d);
     9 }
    10     int eular(int n){//利用欧拉函数求与n互素的数的个数 
    11     double sum=n;
    12     int i;
    13     for(i=2;i<=n;i++){
    14         if(n%i==0){
    15             sum*=(1-1.0/i);
    16             while(n%i==0){    
    17                 n=n/i;
    18             }
    19         }
    20     }
    21     return (int)sum;
    22 }
    23 int SUM(int n)//欧拉函数求和 
    24 {
    25     int j,sum=0;
    26     for(j=2;j<=n;j++)
    27     sum+=eular(j);
    28 return sum;
    29 }
    30 
    31 int main ()
    32 {  
    33     scanf("%d",&n);
    34         printf("0/1,");
    35         pre(0,1,1,1);
    36         printf("1/1
    ");
    37     s=SUM(n);
    38       printf("%d",s+2);
    39       
    40       return 0;
    41 }
  • 相关阅读:
    2017-3-7 leetcode 66 119 121
    2017-3-6 leetcode 118 169 189
    2017-3-5 leetcode 442 531 533
    c++ std
    2017-3-4 leetcode 414 485 495
    2017-3-3 leetcod 1 35 448
    想做手游
    编程规范
    1165: 零起点学算法72——首字母变大写
    1164: 零起点学算法71——C语言合法标识符(存在问题)
  • 原文地址:https://www.cnblogs.com/levelstrcpy/p/7830901.html
Copyright © 2011-2022 走看看