啊哈磊_编程从这里起步

标题: 求约数,质因数,判断素数程序 [打印本页]

作者: 航空兔子    时间: 2013-10-12 21:12
标题: 求约数,质因数,判断素数程序
本帖最后由 航空兔子 于 2013-10-12 21:18 编辑

三位一体......
数学课正好在上这个,于是来了灵感
找约数什么的最烦了......
32位下载地址:
http://yunpan.cn/Qb6ULJR4z9Vpn
C++源码:
[code=Cpp width=740px]

#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
bool su=true;
unsigned long long x,i,s=0;
cout<<"输入数求约数、分解质因数、判断素数:";
cin>>x;
cout<<x<<"的约数:1,";
for(i=2;i<x;++i)
{
  if(x%i==0)
  {
   su=false;
   cout<<i<<",";
  }
}
cout<<x<<endl;
if(su){cout<<x<<"是素数。\n";}
else{cout<<x<<"是和数。\n";}
cout<<x<<"=";
bool frist=true;
if(su){cout<<"1x"<<x;}
else
{
  for(i=2;i<=x;++i)
  {
   if(x%i==0)
   {
    unsigned long long k;
    bool b=true;
    for(k=2;k<i;++k)
     if(i%k==0){b=false;}
    if(b)
    {
     if(frist){cout<<i;x=x/i;frist=false;}
     else{cout<<"x"<<i;x=x/i;}
    }
    while(x%i==0)
    {
     cout<<"x"<<i;
     x=x/i;
    }
   }
  }
}
cout<<endl;
system("pause");
return 0;
}
[/code]
作者: 超神级    时间: 2013-10-12 21:25
{:soso__16365716342404221339_7:}
赞个!
作者: BigRat    时间: 2013-10-12 21:32
路过点赞!
作者: rosynirvana    时间: 2013-10-12 22:36
这样做效率太可怜了,根本用不着用uint64
作者: start1980    时间: 2013-10-12 23:28
楼主几年级啊?
作者: start1980    时间: 2013-10-13 20:14
才6年级,有前途




欢迎光临 啊哈磊_编程从这里起步 (https://bbs.codeaha.com/) Powered by Discuz! X3.2