挑战!!!
时间:2014-04-27
来源:互联网
1,3
1,2,4
1,5
1,2,3,6
1,7
1,2,4,8
1,3,9
1,2,5,10
1,11
1,2,3,4,6,12
1,13
1,2,3,4,6,8,12,16,24,48
1,59
1,2,3,4,6,8,12,16,24,32,48,96
#include <map>
#include <set>
#include <iterator>
using namespace std;
uintmax_t GCD(uintmax_t a, uintmax_t b)
{
uintmax_t t;
while(b)
{
t = b;
b = a % t;
a = t;
}
return a;
}
const std::map<uintmax_t, uintmax_t> Decompose(uintmax_t n)
{
static std::map<uintmax_t, std::map<uintmax_t, uintmax_t>> _factors;
if(n == 0 || n == 1) throw;
std::map<uintmax_t, uintmax_t> &result = _factors[n];
//find the exists...
if(!result.empty())
return result;
if(n == 2)
{
result[2] = 1;
return result;
}
auto _decompose = [&](uintmax_t n, uintmax_t a, std::map<uintmax_t, uintmax_t> &result)
{
uintmax_t gcd = GCD(n, a);
if(gcd != 1)
{
for(auto q : Decompose(gcd))
{
if(result.find(q.first) == result.end())
result[q.first] = q.second;
else
result[q.first] += q.second;
}
for(auto q : Decompose(n / gcd))
{
if(result.find(q.first) == result.end())
result[q.first] = q.second;
else
result[q.first] += q.second;
}
return true;
}
return false;
};
for(auto i = _factors.begin(); i != _factors.end() && i->first != n; ++i)
{
if(_decompose(n, i->first, result)) return result;
}
if(_decompose(n, 2, result)) return result;
for(uintmax_t i = 3; i * i <= n; i += 2)
{
if(_decompose(n, i, result)) return result;
}
//write prime...
result[n] = 1;
return result;
}
const std::set<uintmax_t> Factor(uintmax_t n)
{
std::set<uintmax_t> result{ 1 };
if (n == 0) throw;
if (n == 1) return result;
auto _factor = *Decompose(n).begin();
for (uintmax_t i = 0, k = _factor.first; i < _factor.second; ++i, k *= _factor.first)
{
result.insert(k);
auto temp = Factor(n / k);
result.insert(temp.begin(), temp.end());
for (auto _p : temp) result.insert(_p * k);
}
return result;
}
int main()
{
// your code goes here
uintmax_t a;
while(std::cin >> a)
{
auto _f = Factor(a);
for(auto _p : _f)
{
std::cout << _p;
if(_p == a)
std::cout << endl;
else
std::cout << ",";
}
}
return 0;
}
作者: Susan﹏汪汪 发布时间: 2014-04-27

作者: me888 发布时间: 2014-04-27
相信无咩几个系楼主嘅对手啦,C++ 本人唔识, 是否楼主在做紧特别既算法呢?其实可以好简单化架,以input和output既数字来睇,两个for loop加比较是否可整数除,再加个line feed即可唔系吗

h ...
作者: Susan﹏汪汪 发布时间: 2014-04-27

作者: Susan﹏汪汪 发布时间: 2014-04-27

作者: me888 发布时间: 2014-04-27
无所谓效率快慢啦,今时今日电脑既计算速度,根本系微不足道啦, bubble sort(新机) 可以快过quick sort (用旧机)

作者: Susan﹏汪汪 发布时间: 2014-04-27
无所谓效率快慢啦,今时今日电脑既计算速度,根本系微不足道啦, bubble sort(新机) 可以快过quick sort (用旧机)

作者: a8d7e8 发布时间: 2014-04-27

作者: me888 发布时间: 2014-04-27
<head>
</head>
<body>
<script language="JavaScript">
var rt = new Array(2,3,4,5,6,7,8,9,10,11,12,13,48,59,96);
for (var i=0;i<15;i++)
{
for (var j=1;j<=rt[i];j++)
{
if ( rt[i] % j==0 )
{ document.write((j));
if (j<rt[i]) { document.write(",");}
}
}
document.write("<br />");
}
</script>
</body>
</html>
作者: me888 发布时间: 2014-04-27
c + cpu1 * O(n^2) < c2 + cpu2 * O(nlogn)
I thought that N was veeeeeeery BIG
var rt = new Array(2,3,4,5,6,7,8,9,10,11,12,13,48,59,96);
for (var i=0;i
作者: a8d7e8 发布时间: 2014-04-27
{
if (n == 0) throw;
std::set<uintmax_t> result{ 1, n };
for (uintmax_t i = 2; i * i < n; ++i)
{
if (n % i == 0)
{
result.insert(i);
result.insert(n / i);
}
}
return result;
}
作者: Susan﹏汪汪 发布时间: 2014-04-27
factor s = let n = read s :: Int
in show $ filter (\x -> n `mod` x == 0) [1..n]
factorAll input = unlines $ map factor $ words input
作者: fitcat07 发布时间: 2014-04-27
>>> l = [2,3,4,5,6,7,8,9,10,11,12,13,48,59,96]
>>> for i in range(len(l)):
... fi = filter(l[i]%_==0, range(1, l[i]+1))
... print (','.join(map(str, list(fi))))
...
1,2
1,3
1,2,4
1,5
1,2,3,6
1,7
1,2,4,8
1,3,9
1,2,5,10
1,11
1,2,3,4,6,12
1,13
1,2,3,4,6,8,12,16,24,48
1,59
1,2,3,4,6,8,12,16,24,32,48,96
>>>
作者: form5 发布时间: 2014-04-27
试下 fn.py>>> from fn import _
>>> l = [2,3,4,5,6,7,8,9,10,11,12,13,48,59,96]
>>> for i in range(len(l)):
... fi = filter(l%_==0, range(1, l+1))
... print (','.join(map(str, list(fi))))
. ...




作者: bluenemo 发布时间: 2014-04-27
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28