惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
博客园 - Franky
GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
博客园_首页
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
C
CERT Recently Published Vulnerability Notes
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
L
LangChain Blog
L
LINUX DO - 最新话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hacker News: Front Page
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
AI
AI
T
Troy Hunt's Blog
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Security @ Cisco Blogs
Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
Attack and Defense Labs
Attack and Defense Labs

博客园 - mahope

在ASP.NET web 站点中使用log4net (1.2.9) 在.NET 中实现 AOP 解决Web Service中传递子类实例时,序列化的问题。 Q & A:Does ASP.NET support one-way Web Service operations? openwave:Malformed server response web 项目的 csproj 文件要有对应的.webinfo文件才能在vs里面打开 - mahope 软件需求规范(SRS)指南 写需求文档的一般原则 删除everyone对c:的 访问权限后,运行asp.net出现DirectoryNotFoundException未找到路径“C:\”的一部分 - mahope - 博客园 NHibernate Mapping文件中如何指定类的字节数组属性 NHibernate.ADOException : Unable to perform find 对于事件不能调用BeginInvoke,可改用另外一层包装 IBM面试题试解(关于50条狗、50个人、病狗) Einstein's Riddle 爱因斯坦出的智力题? Artificial intelligence: Solving problems for the real world 一些面向对象的设计法则 重构、分支语句、虚函数、抽象函数与多态--《重构:改善既有代码设计》之读书心得 NHibernate 执行内嵌类(Nested Class)查询 为内嵌类(Nested Class)配置NHibernate的O/R Mapping文件
一个算法题解
mahope · 2007-11-04 · via 博客园 - mahope

写一个函数,接受两个参数N,D进行分数运算N/D;N表示分子,D表示分母。
输入带小数的结果;对于循环小数,用()把循环部分括起来,比如1/3 表示为0.(3)。
限制输出小数点后100位数字,并仅考虑循环在100前结束。

现在给一个算法,基本思路是按照小学所学的除法运算,检查商和余数是否有重复来判断循环小数。此解花费约20分钟完成编码,30分钟调试完成。

#include <stdio.h>
#include 
<memory.h>
#include 
<stdlib.h>
#include 
<string.h>

void fraction(int N, int D, int size, char* out)
{
    
char result[102= {0};

    
int a = N / D;

    
int aa = a;

    
int b = N - a*D;

    
int check_a[102];
    memset(check_a,
0,102*sizeof(int));
    
int check_b[102];
    memset(check_b,
0,102*sizeof(int));

    
int i = 0;
    
    
int c =  10 * b;

    
while(b!=0 && i < 100)
    
{
        a 
= c / D; 
        b 
= c - a*D;

        check_a[i] 
= a;
        check_b[i] 
= b;

        c 
= 10*b;

        
bool loop = false;
        
int loopBegin = 0;

        
for (int j=0;j<i;j++)
        
{
            
if (check_a[j] == a && check_b[j] == b)
            
{
                loop 
= true;
                loopBegin 
= j;
                
break;
            }

        }



        
if (loop)
        
{
            memmove(result
+loopBegin+1, result+loopBegin, i-j);
            result[loopBegin] 
= '(';
            result[loopBegin
+i-j+1= ')';
            
break;
        }

        
else
        
{
            sprintf(result,
"%s%d",result,a);
        }


        i
++;
    }



    
char* big = out;
    memset(big,
0,size);
    itoa(aa,big,
10);

    strcat(big,
".");
    strcat(big,result);
}


int main(int argc,char** argv)
{
    
if (argc!=3)
    
{
        printf(
"input N D\n");

        
return -1;
    }


    
int N = atoi(argv[1]);
    
int D = atoi(argv[2]);

    
char result[120= {0};

    fraction(N,D,
sizeof(result),result);

    printf(result);

    
return 0;
}