Description
In the planet Pandora, Jake found an old encryption algorithm. The plaintext, key and ciphertext are all four decimal numbers and all greater than 0. The way to get the ciphertext from the plaintext and the key is very simple: the ciphertext is the last four digits of the product of the plaintext and the key (for example: the plaintext is 2010 and the key is 4024, and then the product is 8088240. So the ciphertext is 8240).
Note that the plaintext and the key don’t have leading 0, while the ciphertext might have. Now given the plaintext and the key, you are asked to figure out the ciphertext for Jake quickly.
Input
The first line is an integer T, which presents the number of test cases. Then there are T lines, each line has two integers, the first integer is the plaintext and the second is the key.
Output
For each test case, output the ciphertext.
Sample Input
2
2010 4024
1234 1111
Sample Output
8240
0974
HINT
#include<iostream>
#include<cstring>
using namespace std;
char *reverse(char *s)
{
char temp;
char *p = s; //p指向s的头部
char *q = s; //q指向s的尾部
while(*q)
++q;
q--;
//交换移动指针,直到p和q交叉
while(q > p)
{
temp = *p;
*p++ = *q;
*q-- = temp;
}
return s;
}
/*
23. * 功能:整数转换为字符串
24. * char s[] 的作用是存储整数的每一位
*/
char *my_itoa(int n)
{
int i = 0,isNegative = 0;
static char s[100]; //必须为static变量,或者是全局变量
if((isNegative = n) < 0) //如果是负数,先转为正数
{
n = -n;
}
do //从各位开始变为字符,直到最高位,最后应该反转
{
s[i++] = n%10 + '0';
n = n/10;
}while(n > 0);
if(isNegative < 0) //如果是负数,补上负号
{
s[i++] = '-';
}
s[i] = '\0'; //最后加上字符串结束符
return reverse(s);
}
main()
{
int n;
scanf("%d",&n);
while(n--){
int a,b;
scanf("%d%d",&a,&b);
int c;
c=a*b;
char d[100];
memset(d ,'a', sizeof(d));
// char l=my_itoa(c);
//int tail;
string g=my_itoa(c);
int nn=g.length()-1;
cout<<g[nn-3]<<g[nn-2]<<g[nn-1]<<g[nn]<<endl;
//printf("%s",my_itoa(c));
/* for(int i=0;i<100;i++){
if(d[i]=='a') {tail=i;break;}
}
printf("%c%c%c%c",d[tail-5],d[tail-4],d[tail-3],d[tail-2]);*/
}
return 0;
}
代码比较乱,中间有些注释的部分是我在做题过程中实验所用,无视就行(实在没心情挨个删除了),这破题花了我将近两个小时,开始想用课设中使用的两个大数相乘的原理,结果不知哪里出了问题总是算不对,无奈选择将int型转为char类型这一思路,本来c语言有一个非标准库中包含这个函数,即itoa函数,可是oj系统的c/c++都没有这个库,无奈又从网上找了这个函数的算法,虽然我并没有看,直接复制过来。。这才终于解决了这一问题,啊啊啊啊啊 我的时间都让这个破题浪费了!!!