XDOJ1017--Duan's problem

Description

Duan was a naulty boy. One day, he made a mistake again. His teacher was very angry, and want to punish him. The teacher wanted him to solve a problem. If he successfully solved it, he could go home.

Here is the problem. Duan is given two number a and b. As everybody knows, a number is made of 0-9. The teacher wanted him to count how many 0-9 in a*b, and calculate which one appears more than others.

For example, if a is 100, b is 4, then a*b is 400.
Number 0 1 2 3 4 5 6 7 8 9
Frequency 2 0 0 0 1 0 0 0 0 0
so duan should answer 0.
If a is 110,b is 10,then a*b is 1100
Number 0 1 2 3 4 5 6 7 8 9
Frequency 2 2 0 0 0 0 0 0 0 0
so duan should answer 0 and 1.

Can you help him?

Input

There are multiple test cases.Each case contains two integers a and b (0 <= a < 10^10000, 0<=b<10^9). Input file ends of EOF.

Output

On a single line, print the numbers of largest frequency in incremental order separated by one space.

Sample Input

100 4
110 10

Sample Output

0
0 1

 

解题思路:

题意非常好理解,就是统计两个数a,b的乘积a×b中0~9出现的次数,主要考察的就是大整数的乘法运算。大整数的乘法是这样处理的:

如果采用int数组秋存储数组,那么数组中的每一位最多可以存大整的的9位。两个大整数相乘的法则与我们在进行10进制的乘法是一样的,不过此时的进行为10^9罢了。

其实在编程的过程中最让我头疼的是细节问题,比如:

(1)0 0 的情况

(2)如果结果是1-000000001-111111111这样的数,对于第二段中000000001中的0的统计

(3)两个int相乘,结果要用long long存储

……

果然是细节决定成败!

#include<iostream>
#include<string>
using namespace std;

const int INF=100000000;
const int M = 8;//the count of 10 in INF
const int D = 1300;//1300*8>10000+9
const int N = 2;
long long c[D],a[D],b[2];
int len ;
struct FQ
{
    long long freq;
    int id;
} numFreq[10];
void mul(long long *m,long long *n)
{
    long  long t;
    for(int i=0;i<D;++i)
        c[i] = 0;
    for(int i=D-1;i>=0;--i)
    {
        t = 0;
        for(int j=N-1;j>=0;--j)
        {
            t = m[i]*n[j];
            int k = i-(N-1-j);
            while(t!=0&&k>=0)
            {
                c[k] += t%INF;
                t /=INF;
                --k;
            }
        }
    }
    for(int i=0;i<D;++i)
        m[i] = c[i];
}
void insertSort(FQ *A)
{
    for(int j=1;j<10;++j)
    {
        FQ key = A[j];
        int i = j-1;
        while(i>=0&&A[i].freq<key.freq)
        {
            A[i+1] = A[i];
            --i;
        }
        A[i+1] = key;
    }
}
void strToArr(string str,long long *A)
{
    int i = len-1;
    int k = 1;
    int factor = 1;
    long long t = 0;
    while(i>=0)
    {
        t = 0;
        for(int j=i;j>i-M&&j>=0;--j)
        {
            t += (str[j]-'0')*factor;
            factor *= 10;
        }
        A[D-k] = t;
        ++k;
        i -= M;
        factor = 1;
    }
}
int main()
{
    long long numCount[10];
    string num1;
    int num2;
    while(cin>>num1>>num2)
    {
       len = num1.length();
       for(int i=0;i<D;++i)
            a[i] = 0;
       for(int i=0;i<10;++i)
            numCount[i] = 0;
        strToArr(num1,a);
        b[0] = 0;
        b[1] = num2;
        mul(a,b);
        int i=0;
        while(i<D&&a[i]==0)++i;
        int w = M*(D-i);
        if(i==D)
        {
            cout<<0<<endl;
            continue;
        }

        long long t = a[i];
        w -= M;
        while(t!=0)
        {
            int id = t%10;
            t /= 10;
            numCount[id]++;
        }
        ++i;

        for(;i<D;++i)
        {
             t = a[i];
            while(t!=0)
            {
                int id = t%10;
                t /= 10;
                numCount[id]++;
                --w;
            }
        }
        numCount[0] += w;

        for(int i=0;i<10;++i)
        {
            numFreq[i].freq = numCount[i];
            numFreq[i].id = i;
        }
        insertSort(numFreq);
        cout<<numFreq[0].id;
        for(int i=1;i<10;++i)
        {
            if(numFreq[i-1].freq!=numFreq[i].freq)
                break;
            else
                cout<<" "<<numFreq[i].id;
        }
        cout<<endl;
    }
    return 0;
}

 

最后欢迎大家访问我的个人网站: 1024s

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值