• UVa 202 Repeating Decimals【模拟】


    题意:输入整数a和b,输出a/b的循环小数以及循环节的长度

    学习的这一篇

    http://blog.csdn.net/mobius_strip/article/details/39870555

    因为n%m的余数只可能是0到m-1中的一个,根据抽屉原理,当计算m+1次时至少存在一个余数相同

    发现看了题解理解起来也好困难啊,

    后来手动画了一下5/7的竖式除法的式子,,理解一些了

     1 #include<iostream>  
     2 #include<cstdio>  
     3 #include<cstring> 
     4 #include <cmath> 
     5 #include<stack>
     6 #include<vector>
     7 #include<map> 
     8 #include<set>
     9 #include<queue> 
    10 #include<algorithm>  
    11 using namespace std;
    12 
    13 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
    14 
    15 typedef long long LL;
    16 const int INF = (1<<30)-1;
    17 const int mod=1000000007;
    18 const int maxn=100005;
    19 
    20 int vis[maxn],r[maxn],s[maxn];
    21 
    22 int main(){
    23     int n,m;
    24     while(cin>>n>>m){
    25         memset(vis,0,sizeof(vis));
    26         memset(r,0,sizeof(r));
    27         memset(s,0,sizeof(s));
    28         
    29         int cnt=0;
    30         int t=n;
    31         r[cnt++]=n/m;
    32         n=n%m;
    33         while(!vis[n]&&n){
    34             vis[n]=cnt;
    35             s[cnt]=n;//s数组存的是对应的余数 
    36             r[cnt++]=n*10/m;//r数组存的是对应的商 
    37             n=n*10%m;
    38         }
    39         
    40         printf("%d/%d = %d.",t,m,r[0]);
    41         
    42         for(int i=1;i<cnt&i<=50;i++){
    43             if(s[i]==n&&n) printf("(");
    44             printf("%d",r[i]);
    45         }
    46         if (!n) printf("(0");  
    47         if (cnt > 50) printf("...");  
    48         printf(")
    ");  
    49         printf("   %d = number of digits in repeating cycle
    
    ",!n?1:cnt-vis[n]);  
    50     }
    51     return 0;
    52 }
    View Code

    还得再多画一画----好晕--------------

    写模拟写得好难受------------

    goooooooooooooo----------

  • 相关阅读:
    Windows上安装PyV8
    Windows鼠标右键菜单添加SublimeText打开选项
    Windows使用Python虚拟环境
    Windows同时安装Python2和Python3
    Windows使用Cmder
    Visual Studio Code配置
    Windows 10使用Tesseract-OCR出现WindowsError: [Error 2]
    用pymysql代替MySQLdb
    使用python来搞定redis的订阅功能
    来写一个最基本的装饰器吧!
  • 原文地址:https://www.cnblogs.com/wuyuewoniu/p/4477256.html
Copyright © 2020-2023  润新知