/*
* Copyright (c) 2014, 烟台大学计算机学院
* All rights reserved.
* 文件名称:test.cpp
* 作 者:李晓凯
* 完成日期:2015年 5 月 27 日
* 版 本 号:v1.0
*
* 问题描述:
* 输入描述:
* 程序输出:
*/
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.
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
思路:将一个数分解成几个质数的几次方的形式。
有点错误的代码:
#include <iostream>
using namespace std;
int main()
{
int i,n,m,num=1;
while(cin>>n&&n>0)
{
int j=0,a[65526]= {};
cout<<"Case "<<num++<<"."<<endl;
for(i=2; i<=n; ++i)
{
m=0;
if(n%i==0)
{
a[j]=i;
j++;
}
while(n%i==0)
{
m++;
n/=i;
}
if(m>0)
a[j++]=m;
}
for(i=0; i<j; ++i)
{
cout<<a[i]<<" ";
}
cout<<endl<<endl;
}
return 0;
}
这个代码,改了好多次,提交的时候老是提示“Presentation Error”,这应该是格式错误,但是就是没找出来哪不对,跪求大神指出!
AC代码:
#include <iostream>
using namespace std;
int main()
{
int n,m,num= 1;
while (cin>>n&& n > 0)
{
if (num!=1) cout<<endl;
cout<<"Case "<<num++<<"."<<endl;
for (int i=2;i<=n;++i)
{
m=0;
if (n%i==0)
{
cout<<i<<" ";
}
while (n%i==0)
{
m++;
n/=i;
}
if (m>0) cout<<m<<" ";
}
cout<<endl;
}
return 0;
}