重寫了PKU2897“Dramatic Multiplications”,AC了,重寫後思路很清晰,果然不一樣!

最近幾天比較少更新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;

發佈了43 篇原創文章 · 獲贊 0 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章