您的位置:首页 > 其它

POJ Occurrence of Digits

2010-08-24 12:50 197 查看
Time Limit: 1000MSMemory Limit: 65536K
Description

Every fraction can be converted to a repeatin decimal. For example 1/2 = .5, 1/3 = .(3) and 1/6 = .1(6). Given an integer n, Tom wants to know how many digit k occurs totally in the repeating decimal presentation of 1/2, 1/3 ... 1/n.

Input

The input consists of several test cases. Each test case is a line containing two integers n (2 ≤ n ≤ 100) and k (0 ≤ k ≤ 9).

Output

Output the total occurrence of the digit.

Sample Input
3 5
7 3
7 0

Sample Output
1
1
0

Source
POJ Monthly Contest – 2009.02.22, Lei Tao
题意:
每组输入数据包含两个正整数n和k,求数字k在分数序列1/2,1/3,1/4,……,1/n的小数形式中出现的次数。无限循环部分只计算一次。
我的解法:
题目给定n的范围为[2,100],可以使用暴力的方法将这个区间内的分数的小数形式的数字组成统计出来。对每组输入数据只需叠加计算结果就行。这可能出现循环小数,可以设置一个状态数组,在做除法的过程中,分母不变,如分子再次(第二次)出现则将出现循环小数。为此,做除的控制条件为:1.分子不为0;2,分子不重复重现。
我的代码:
#include <iostream>
#include <string>
int cnt[101][10];
bool flag[101]; // 标志该数做除过程中的某被除数数是否出现 以判别是否为无限循环小数
using namespace std;
int main()
{
int i;
int fenzi=1; // 分子
for( i=2; i<=100; i++ )
{
memset( flag , false, sizeof( flag ) );
fenzi=1;
while( fenzi && !flag[fenzi] )
{
flag[fenzi]=true; // 分子出现后,flag[fenzi]置为true
fenzi *= 10; //
int index = fenzi / i; // 做除求商
cnt[i][index] ++; // 计数器自加
fenzi %=i; // 做除求其余数
}
}
int n,k;
while( cin>>n>>k )
{
int total = 0;
for( i=2; i<=n; ++i )
total += cnt[i][k];
cout<<total<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: