• ----:)欢迎访问源码网(:----
    • 首页
    • 博客
    • 学院
    • 下载
    • 论坛
    • 影视
    • 发布源码
    • RSS
    • ITPig
    • 笑话网
    • 百家姓
    • 繁體中文

源码网 - 中国第一源码门户
选择镜像:网通镜像 - 电信主站
  • 首 页
  • 新闻动态
  • 网站运营
  • 网页制作
  • WEB开发
  • 编程开发
  • 图像媒体
  • 操作系统
  • 数据库
  • 服务器
热门搜索 优化 SEO 故事 cms IIS7 MySQL 个人 AdSense 主题推广 | 文章搜索: 高级搜索
会员登录/控制面版您的位置: 学院首页 >> WEB开发 >> .NET 开发 >> 详细内容
 

推荐文章

 
 

热点文章

  • FckEditor远程图片下载插件
  • TFS(Team Foundation Server)使用经验
  • IIS过滤器实现.NET程序不破解DLL替换字符串一法
  • 为ASP.NET封装的SQL数据库访问类
  • ASP.NET2.0中文验证码的实现
  • Url地址重写,利用HttpHander手工编译页面并按需生成静..
  • ASP.NET学习笔记一——ASP和ASP.NET比较
  • 使用HtmlInputHidden 控件在本页面保持状态和跨页面传..
  • ASP.Net发邮件
  • Silverlight 2.0中文学习资源集萃
  • WinForm中使用XtraGrid控件,实现在界面中动态修改列显..
  • 解析ASP.NET木马文件操作
 
 

相关文章

  • 快算24的算法
  • ACM UVa算法题209 Triangular Vertices的解法
  • ACM UVa 算法题 #110 - Meta-Loopless Sort 的解法
  • ACM UVa 算法题 #108 - Maximum Sum的解法
  • ACM UVa 算法题 #507 - Jill Rides Again的解法
  • ACM UVa 算法题 #201 - Squares解法
  • ACM UVa 算法题 #200 - Rare Order的解法
  • ACM UVa 116 - Undirectional TSP的解法
  • ACM UVa 算法题100, 101, 103, 104, 112, 10405解法
  • 百度面试题(算法)
  • 张劲松:蚂蚁算法与SNS猜想
  • 获取 Google PR 代码Checksum 算法
 
 

百度搜索

 
 

ACM UVa 算法题 #202 - Repeating Decimals的解法

  • 阅览次数:
  • 文章来源: http://blog.csdn.net/ATField/
  • 原文作者: ATField
  • 整理日期: 2008-07-20
  • 发表评论
  • 字体大小:
  • 小
  • 中
  • 大

题目的Link:ACM UVa #202 - Repeating Decimals

很简单,属于大家都可以做的题目,也就是送分题 :)

当逐步作除法的时候如果出现了重复的商,那么cycle必然在这个两个重复的商之间,道理我就不多啰嗦了,小学数学嘛

一般的方法是纪录所有商,每算出一个新的便顺序查找看该商是否出现过,复杂度为O(n^2)。为了提高速度,注意到题目中限制除数和被除数<3000,也就是说商必然<3000。那么用一数组便可以纪录某个商是否出现过,可以将速度提高一个数量级,为O(n)。

代码如下:

// 
// ACM UVa Problem #202
// http://acm.uva.es/p/v2/202.html
//
// Author:  ATField
// Email:   atfield_zhang@hotmail.com
//

#include 
"stdafx.h"
#include 
<iostream>
#include 
<algorithm>

using namespace std;

#define MAX 5000
#define DISPLAY_LIMIT 50
#define MAX_INT 3000

int main(int argc, char *argv[])
...{
    
int digits[MAX + 1];
    
int reminder_exist[MAX_INT];
    
int reminder_pos[MAX_INT];

    
while(1)
    
...{
        
int numerator;
        
int original_numerator;
        
int denominator;

        cin 
>> numerator;
        
if( cin.eof() )
            
return 0;

        original_numerator 
= numerator;

        cin 
>> denominator;

        
int quotient, reminder;

        memset( reminder_exist, 
0, sizeof(reminder_exist) );
        memset( reminder_pos, 
0, sizeof(reminder_pos) );

        quotient 
= numerator / denominator;
        reminder 
= numerator - quotient * denominator;

        
int integer = quotient;

        
int n = 0;
        
bool found_cycle = false;
        
int cycle_pos = MAX;
        
int cycle_len = 0;
        
while( n <= MAX && !found_cycle /**//* && reminder > 0 */ )
        
...{
            
if( reminder_exist[reminder] )
            
...{
                cycle_pos 
= reminder_pos[reminder];
                cycle_len 
= n - cycle_pos;
                found_cycle 
= true;
            }

            
else
            
...{
                reminder_exist[reminder] 
= 1;
                reminder_pos[reminder] 
= n;
            }


            numerator 
= reminder * 10;

            quotient 
= numerator / denominator;
            reminder 
= numerator % denominator;
            digits[n] 
= quotient;

            n
++;
        }


        cout 
<< original_numerator << "/" << denominator << " = " << integer << ".";
        
int limit = min(cycle_pos, DISPLAY_LIMIT);
        
for( int i = 0; i < limit; ++i )
            cout 
<< digits[i];

        
if( cycle_pos < DISPLAY_LIMIT )
        
...{
            cout 
<< "(";
            
int limit = min(n - 1, DISPLAY_LIMIT);
            
for( int i = cycle_pos; i < limit; ++i )
                cout 
<< digits[i];
            
if( n > DISPLAY_LIMIT )
                cout 
<< "...";
            cout 
<< ")";
        }


        cout 
<< " ";
        cout 
<< "   " << cycle_len << " = number of digits in repeating cycle ";
    }



    
return 0;
}

上一篇:PHP使用zlib扩展实现页面GZIP压缩输出
下一篇:构建支持Master/Slave读写分离的数据库操作类
  • 网友评论:
  • 查看所有评论
  • 我要发表评论
您的网名:
留言主题:
你要发表的内容:

 

关于本站 | 广告联系 | 版权声明 | 网站地图 | 发布软件 | 帮助中心 | 源码论坛

Copyright © 2005-2007 CodePub.Com  程序支持:木翼  滇ICP备05005971号