hdoj 1405

The Last Practice

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12091 Accepted Submission(s): 2811

Problem Description
Tomorrow is contest day, Are you all ready?
We have been training for 45 days, and all guys must be tired.But , you are so lucky comparing with many excellent boys who have no chance to attend the Province-Final.

Now, your task is relaxing yourself and making the last practice. I guess that at least there are 2 problems which are easier than this problem.
what does this problem describe?
Give you a positive integer, please split it to some prime numbers, and you can got it through sample input and sample output.

Input
Input file contains multiple test case, each case consists of a positive integer n(1<n<65536), one per line. a negative terminates the input, and it should not to be processed.

Output
For each test case you should output its factor as sample output (prime factor must come forth ascending ), there is a blank line between outputs.

Sample Input
60
12
-1

Sample Output
Case 1.
2 2 3 1 5 1

Case 2.
2 2 3 1
Hint

60=22*31*5^1

瞎胡翻译:输入一个数,分解成乘积的形式,比如60可以分解成2个2 和 1个3 和 1个5相乘。以-1作为输入的结束。输出,先输出Case n. ,这里注意了,数字后面有个点,下一行输出分解的结果,数字在前,数字的个数在后,两个分解结果之间有一个空行,这个题好像很容易出现各式错误。
蜜汁思路:从1到n遍历,发现一个能被n整除的数i之后,就一直用n除以i,直到不能被整除,并记录i以及整除的次数。

看不懂思路就看代码,emmm

扫描二维码关注公众号,回复: 4456458 查看本文章
#include<stdio.h>
#include<string.h>
#define maxn 1100
int s[maxn];
int d[maxn];
int h;
void W(int n) {
    h=0;
    int m=n;
    for(int i=2; i<=n; i++) {
        int sum=0;
        if(n%i==0) {
            while(n%i==0) {
                n/=i;
                sum++;
            }
            s[h]=i;
            d[h++]=sum;
        }
        if(m==1)
            break;
    }
}
int main() {
    int n;
    int t=0;
    while(scanf("%d",&n)!=EOF&&n>=0) {
        memset(s,0,sizeof(s));
        memset(d,0,sizeof(d));
        t++;
        if(t>1)
            printf("\n");
        printf("Case %d.\n",t);
        int m=n,j=0;
        W(n);
        for(int i=0; i<h; i++)
            printf("%d %d ",s[i],d[i]);
        printf("\n");
    }
}

猜你喜欢

转载自blog.csdn.net/D_mengxin/article/details/84940764