zoukankan      html  css  js  c++  java
  • 判断1/N是否为无限小数

    给定一个正整数N,请判断1/N是否为无限小数,若是输出YES,若不是请输出NO。

    思路:

    只要被除数n可以转换成2的次幂或者2与5的组合即为有限小数,否则为无线小数

    代码如下:

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <iostream>
     4 #include <string>
     5 #include <math.h>
     6 #include <algorithm>
     7 #include <vector>
     8 #include <stack>
     9 #include <queue>
    10 #include <set>
    11 #include <map>
    12 #include <sstream>
    13 const int INF=0x3f3f3f3f;
    14 typedef long long LL;
    15 const int mod=1e9+7;
    16 //const double PI=acos(-1);
    17 #define Bug cout<<"---------------------"<<endl
    18 const int maxn=5e5+10;
    19 using namespace std;
    20 
    21 int judge(int n)
    22 {
    23     while(n)
    24     {
    25         if(n%2==0||n==1)
    26         {
    27             n/=2;
    28         }
    29         else
    30             break;
    31     }
    32     while(n)
    33     {
    34         if(n%5==0||n==1)
    35         {
    36             n/=5;
    37         }
    38         else
    39             break;
    40     }
    41     if(n==0)
    42         return 1;
    43     else
    44         return 0;
    45 }
    46 
    47 int main()
    48 {
    49     int n;
    50     scanf("%d",&n);
    51     if(judge(n))
    52         printf("NO
    ");
    53     else
    54         printf("YES
    ");
    55     return 0;
    56 }

     递归写法:

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <iostream>
     4 #include <string>
     5 #include <math.h>
     6 #include <algorithm>
     7 #include <vector>
     8 #include <stack>
     9 #include <queue>
    10 #include <set>
    11 #include <map>
    12 #include <sstream>
    13 const int INF=0x3f3f3f3f;
    14 typedef long long LL;
    15 const int mod=1e9+7;
    16 //const double PI=acos(-1);
    17 #define Bug cout<<"---------------------"<<endl
    18 const int maxn=5e5+10;
    19 using namespace std;
    20 
    21 int Solve(int n)
    22 {
    23     if(n==1)
    24         return 1;
    25     else if(n%2==0)
    26         return Solve(n/2);
    27     else if(n%5==0)
    28         return Solve(n/5);
    29     else
    30         return 0;
    31 }
    32 
    33 int main()
    34 {
    35     int n;
    36     scanf("%d",&n);
    37     if(Solve(n))
    38         printf("NO
    ");
    39     else
    40         printf("YES
    ");
    41     return 0;
    42 }
  • 相关阅读:
    Kotlin泛型与协变及逆变原理剖析
    struts2中action的class属性值意义
    重新设置Eclipse的workspace路径
    windows下将mysql加入环境变量
    Eclipse插件安装4种方法
    Maven常用命令
    IntelliJ IDEA光标变粗 backspace无法删除内容解决方法
    Weblogic Exception in AppMerge flows' progression
    Oracle的dual
    lgp20151222 解决-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment variable and mvn script match.
  • 原文地址:https://www.cnblogs.com/jiamian/p/11717107.html
Copyright © 2011-2022 走看看