这是我们学校进行新手ACM培训的题目..我做出来了现在趁热写一下博客复习一下。
尾数前移问题
Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 13 Accepted Submission(s) : 7
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
整数n的尾数是q,把尾数q移到其前面(成为最高位)后所得的数为原整数n的p倍,原整数n至少为多大?
约定:2≤p≤q≤9
约定:2≤p≤q≤9
Input
两个整数,分别是q和p
Output
一个整数n
Sample Input
9 3
Sample Output
3103448275862068965517241379
Author
这个问题的求解思路: 我们进行一位一位的计算,将所有的一位数一个一个储存在一个数组里面。注意每一位的进位运算。计算结束的条件就是前一位乘以倍数加上进位要等于所输入的数据q。最后再倒序输出这个数组即可。
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
int data[1010]= {0};
int q,p;
while(cin>>q>>p)
{
memset(data,0,sizeof(data));
data[0] =q;
int i,flag = 0;
for( i = 1;;)
{
if(data[i-1]*p + flag != q)
{
data[i] = (data[i-1]*p + flag) %10;
flag = (data[i-1]*p + flag) / 10;
i++;
}
else
break;
}
for(int j = i-1;j >=0;j--)
cout<<data[j];
cout<<endl;
}
return 0;
}