POJ 1365 Prime Land(接收数据)

本文介绍了一种接收并处理质因数分解形式输入的方法,实现了对于输入数值减一后的质因数分解输出。文中提供了两种不同的数据接收思路,并附带了一个质数表用于简化处理流程。

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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值