Description
将x分解质因数其中pi按降序排列,ei>0。
现在给你一个数num的分解质因数的形式,输出num-1的分解质因数的形式。
如Sample2表示10=5^1*2^1,那么10-1=3^2。
Input
The input consists of lines (at least one) each of which except the last contains prime base representation of just one positive integer greater than 2 and less or equal 32767. All numbers in the line are separated by one space. The last line contains
number 0.
Output
The output contains one line for each but the last line of the input. If x is a positive integer contained in a line of the input, the line in the output will contain x - 1 in prime base representation. All numbers in the line are separated by one space. There is no line in the output corresponding to the last ``null'' line of the input.
Sample Input
17 1 5 1 2 1 509 1 59 1 0
Sample Output
2 4 3 2 13 1 11 1 7 1 5 1 3 1 2 1
值得思考的地方是如何接收这样的数据。网上很多题解是把一行数据作为字符串接收,然后把字符串中的数字提取出来。个人认为这样比较麻烦就没有尝试。开始的思路是一次接收两个数据(一个底数和一个指数),然后通过判断是否遇到回车来判断是否一组数据录入完毕。
while(~scanf("%d%d",&p,&e)){
for(int i=1; i<=e; i++) num*=p;
char c=getchar();
if(c=='\n')
{
num--;
fun(num);
num=1;
}
}
另一种思路是一次接收一个数据(底数)
while(~scanf("%d",&p)){
if(p==0) break;
scanf("%d",&e);
for(int i=1; i<=e; i++) num*=p;
char c=getchar();
if(c=='\n')
{
num--;
fun(num);
num=1;
}
}
思路很简单,为方便打了个32767以内的质数表prime[ ]。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cctype>
using namespace std;
int prime[]={};/*3512个,自行打印*/
void fun(int num){
int i;
for(i=0;i<3512;i++){
if(num<=prime[i]) break;
}
if(num==prime[i]) {
cout<<num<<" 1"<<endl;
return;
}
else {
i--;
int flag_cnt=1;
for(;i>=0;i--){
int cnt=0,flag=0;
while(num%prime[i]==0){
flag=1;
cnt++;
num/=prime[i];
}
if(flag)
if(flag_cnt){
flag_cnt=0;
cout<<prime[i]<<' '<<cnt;
}
else
cout<<' '<<prime[i]<<' '<<cnt;
if(num==1) {
putchar(10);
return;
}
}
}
}
int main(){
int p,e,num=1;
while(~scanf("%d",&p)){
if(p==0) break;
scanf("%d",&e);
for(int i=1; i<=e; i++) num*=p;
char c=getchar();
if(c=='\n')
{
num--;
fun(num);
num=1;
}
}
return 0;
}