重写了PKU2897“Dramatic Multiplications”,AC了,重写后思路很清晰,果然不一样!

来源:互联网 发布:淘宝网足球 编辑:程序博客网 时间:2024/04/30 00:16

最近几天比较少更新Blog,原因是因为我连QQ都无法连上。昨天和前天加起来一共做了14道题,都是在zju的。因为我有zju的离线文件。可是最后在PKU上只AC了三道,加上我重写的这一道,一共AC了4道。我数了下,我还有8道提交了没AC。

我的程序代码:

/*
Dramatic Multiplications
Time Limit:1000MS  Memory Limit:65536K
Total Submit:714 Accepted:216

Description
Hassan, helping with his younger brother's homework, found out that when you multiply 102564 by 4, its right-most digit moves to the left, and the other digits move one position to the right; i.e. 4 * 102564 = 410256. We call a number that has this property when multiplied by n, an n-dramatic number. For instance, 102564 and 128205 are both 4-dramatic. Given two one-digit numbers n and k, the goal is to find the smallest n-dramatic number that its rightmost digit is k.

Input
On the first line of the input, there is an integer t, which is the number of cases that follow. Each test case, is on a line by itself, and contains two integers n and k, where 1 <= n <= 9, and 1 <= k <= 9.

Output
For each test case, output a single integer on a line by itself, which is the smallest n-dramatic number that its rightmost digit is k. If no such number exists, output 0 instead.

Sample Input


2
4 5
2 1

Sample Output


128205
0

Source
Tehran 2005
*/
#include "iostream"

using namespace std;

int main()
{
 int n,i,k,t,flag,j;
 char str1[100],str2[100],str3[100];
 cin>>t;
 while(t)
 {
  memset(str1,'0',sizeof(str1));
  memset(str2,'0',sizeof(str2));
  memset(str3,'0',sizeof(str3));
  i=0;
  flag=0;
  cin>>n>>k;
  if(n<1||n>9||k<1||k>9)
   return 0;
  str1[i]=k+'0';
  i++;
  while(i<100)
  {
   str2[i-1]+=str3[i-1]-'0';
   str2[i-1]+=((str1[i-1]-'0')*n)%10;
   str3[i]+=((str1[i-1]-'0')*n)/10;
   if(str2[i-1]>'9')
   {
    str2[i-1]-=10;
    str3[i]+=1;
   }
   str1[i]=str2[i-1];
   i++;
  }
  for(i=0;i+1<100;i++)
  {
   if(str3[i+1]=='0'&&str1[0]==str2[i]&&str1[i]!='0')
   {
    flag=1;
    break;
   }
  }
  if(flag)
  {
   for(j=i;j>=0;j--)
    cout<<str1[j];
   cout<<endl;
  }
  else
   cout<<"0"<<endl;
  t--;
 }
 return 0;

原创粉丝点击