HDU 3215 The first place of 2^n (数论-水题)

The first place of 2^n


Problem Description
LMY and YY are mathematics and number theory lovers. They like to find and solve interesting mathematic problems together. One day LMY calculates 2n one by one, n=0, 1, 2,… and writes the results on a sheet of paper: 1,2,4,8,16,32,64,128,256,512,1024,……

LMY discovers that for every consecutive 3 or 4 results, there must be one among them whose first digit is 1, and comes to the conclusion that the first digit of 2n isn’t evenly distributed between 1 and 9, and the number of 1s exceeds those of others. YY now intends to use statistics to prove LMY’s discovery.
 

Input
Input consists of one or more lines, each line describing one test case: an integer N, where 0≤N≤10000.

End of input is indicated by a line consisting of -1.
 

Output
For each test case, output a single line. Each line contains nine integers. The i th integer represents the number of js satisfying the condition that 2 j begins with i (0≤j≤N).
 

Sample Input
  
  
0 1 3 10 -1
 

Sample Output
  
  
1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 1 0 0 0 1 0 4 2 1 1 1 1 0 1 0
 

Source
 

Recommend
zhuweicong   |   We have carefully selected several similar problems for you:   3219  3217  3212  3218  3216 
 

题目大意“:

妈呀,这是我们大东华09年出的题啊,好厉害,其实是好水啊。

题目大意就是计算2^0到2^n这n个数首位为1的次数,2的次数,...9的次数。


解题思路:

我是不会告诉你log10一下就会找到你想要的东西的。


解题代码:

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

const int maxn=10010;
const double lg2=log10(2.0);
int a[maxn];

void ini(){
    a[0]=1,a[1]=2,a[2]=4,a[3]=8;
    for(int i=4;i<maxn;i++){
        double x=i*lg2-int(i*lg2+1e-7);
        a[i]=pow(10.0,x);
    }
    //for(int i=0;i<20;i++) cout<<"2^"<<i<<" :"<<a[i]<<endl;
}


int main(){
    ini();
    int n;
    while(scanf("%d",&n)!=EOF && n!=-1){
        int cnt[10]={0};
        for(int i=0;i<=n;i++){
            cnt[a[i]]++;
        }
        printf("%d",cnt[1]);
        for(int i=2;i<=9;i++){
            printf(" %d",cnt[i]);
        }
        printf("\n");
    }
    return 0;
}






  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

炒饭君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值