- Jul 03 Sun 2011 19:39
-
C++ 質數
- Jun 11 Sat 2011 22:35
-
C++ 質因數分解 2

原理與我之前的C++ 質因數分解是一樣的,不過這一個增加了次方的顯示,方便觀看XD~
#include<iostream>
using namespace std;
int main()
{
int in,sum;
while(cin>>in)
{
for(int x=2;x<=in;x++)
{
while(in%x==0)
{
if(in%(x*x)==0)
{
sum=0;
do{
sum++;
in/=x;
}while(in%x==0);
cout<<x<<"^"<<sum;
}
else
{
cout<<x;
in/=x;
}
if(in>1)
{
cout<<" * ";
}
}
}
cout<<"\n";
}
return 0;
}
- May 23 Mon 2011 22:05
-
C++ 質因數分解

這是簡易版的質因數分解,使用的想法類似於短除法,程式碼不長,給大家做做參考吧~
看看你能找到多大的質數
#include<iostream>
using namespace std;
int main()
{
int in; //儲存輸入
while(true)
{
cout<<"Number:";
cin>>in;
for(unsigned int x=2;x<=in;x++)
{
while(in%x==0)
{
cout<<x<<"*";
in/=x;
}
}
cout<<"\b \n";
}
system("pause");
return 0;
}
1


