首页 > 代码库 > HDU 1405 The Last Practice(数学题,变态的格式)

HDU 1405 The Last Practice(数学题,变态的格式)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1405


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=2^2*3^1*5^1


题意很明了!但是格式确实把我坑了一把!注意了最后一个案例是没有空格的情况!但是没想到的是每个案例输出的最后一个数字后面还有空格(过于小心,特意去掉了最后一个空格,结果一直PE,后来才知道最后还有一个空格,图样图森破)!



代码如下:

#include <cstdio>
int a[1017], b[1017];
int K;
int cas = 0, f = 0;
void slove(int n)
{
    int tt = 2;
    K = 0;
    int c = 0, flag = 0;
    while(n)
    {
        if(n%tt == 0)
        {
            flag = 1;
            n/=tt;
            c++;
        }
        else if(n%tt!=0 && flag)
        {
            a[K] = tt;
            b[K] = c;
            K++;
            c = 0;
            tt++;
            flag = 0;
            if(n == 1)
                break;
        }
        else
            tt++;
    }
}
void prit()
{
    printf("Case %d.\n",++cas);
    for(int i = 0; i < K; i++)
    {
        //if(i == 0)
        //    printf("%d %d",a[0],b[0]);
        //else
        //   printf(" %d %d ",a[i],b[i]);
        printf("%d %d ",a[i],b[i]);
    }
    printf("\n");
    f = 1;
}
int main()
{
    int n;
    while(scanf("%d",&n) && n >= 0)
    {
        if(f)
            printf("\n");
        slove(n);
        prit();
    }
    return 0;
}


HDU 1405 The Last Practice(数学题,变态的格式)