POJ Occurrence of Digits
2010-08-24 12:50
197 查看
Time Limit: 1000MS | Memory Limit: 65536K | |
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;
}
相关文章推荐
- poj 3720 Occurrence of Digits
- POJ 3720 Occurrence of Digits 可能会
- 【POJ】3720 Occurrence of Digits
- POJ 2739 Sum of Consecutive Prime Numbers
- poj 2151 Check the difficulty of problems(概率dp)
- POJ 1157 LITTLE SHOP OF FLOWERS(DP)
- POJ 1157 Little shop of flowers(DP,最优搭配)
- poj 3498 March of the Penguins(满足点容量限制的网络流)
- POJ 1351 Number of Locks (记忆化搜索 状态压缩)
- Period of an Infinite Binary Expansion+POJ+欧拉函数+好题
- POJ 1157 LITTLE SHOP OF FLOWERS 笔记
- poj 3083 Children of the Candy Corn
- Poj 2739 Sum of Consecutive Prime Numbers
- POJ 1236 Network of Schools
- Count the number of occurrences in a sorted array
- poj2151 Check the difficulty of problems
- POJ 2074 Line of Sight 直线交
- POJ 2139 Six Degrees of Cowvin Bacon (floyd)
- 【连通图|双连通+二分图判定】POJ-2942 Knights of the Round Table
- poj2771 Guardian of Decency