POJ Occurrence of Digits

Time Limit: 1000MS Memory 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

题意:
每组输入数据包含两个正整数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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值