HOJ10184The Lowest k bits

Problem description
Give you two numbers a and b, you have to compute the product of them, and find the lowest k bits of the product.
Input
The first line of input will be a positive integer indicating how many data sets will be included (N). Each of the next N lines will contain the a, b and k .(0 <= a <= 2^32 - 1, 0 <= b <= 2^32 -1, 1<= k <= 32).
Output
For each dataset output a single number which is the last k bits of a * b.
Sample Input
2
2 4 3
20 29 5
Sample Output
0
4
Problem Source

HNU Contest 

#include <iostream>
#include <cstdio>
using namespace std;
#include <cstring>
#include <cmath>
int main()
{
    int k=0,a,b,t,q,d,j;
    int z[100];
    unsigned __int64 sum,p;
    cin >> t;
    while (t--)
    {
		memset(z,0,sizeof(z));
        q=0;	
        sum=0;
        scanf("%d%d%d",&a,&b,&k);
        sum=a*b;
        while (sum)
        {
            z[q++]=sum%2;
            sum/=2;
        }
		d=1;p=0;
        for(j=0;j<q,k>0;j++,k--)
		{
			p=d * z[j]+p;
			d=d*2;
		}
        printf("%I64u\n",p);                
    }
    return 0;
}
从这个题学到,在写程序中C和C++的输入输出不能混用,否则后果很严重。搞得我WA好几次,大家请记住哈!
 

你可能感兴趣的:(HOJ10184The Lowest k bits)