c语言分解因数错误代码,【原创】质因数分解 C语言代码

#include

#include

const unsigned __int64 prime[2] = {2,3};

const int primeNumTblSize = sizeof(prime) / sizeof(prime[0]);

void PrintFactorization( unsigned __int64 srcNum, unsigned __int64 baseNum )

{

unsigned __int64 yinzi[64] = {0};

int mi[64] = {0};

int numOfYinzi = 0;

if (srcNum == 1 || srcNum == 0)

printf("is neither a prime number nor a composite number\n**************************************************\n", srcNum);

else if (srcNum < 4)

printf("is a prime number.\n**************************************************\n");

else

{

while (srcNum > 1)

{

for (int i = 0; i < primeNumTblSize; i++)

{

if (srcNum % prime[i] == 0)

{

if (numOfYinzi == 0)

{

yinzi[0] = prime[i];

mi[0]++;

numOfYinzi++;

} else

{

if (yinzi[numOfYinzi-1] == prime[i])

mi[numOfYinzi-1]++;

else

{

yinzi[numOfYinzi] = prime[i];

mi[numOfYinzi]++;

numOfYinzi++;

}

}

srcNum /= prime[i];

break;

}

}

if (i >= primeNumTblSize)

{

for (baseNum; baseNum * baseNum <= srcNum; baseNum += 6)

{

if (srcNum % baseNum == 0)

{

if (numOfYinzi == 0)

{

yinzi[0] = baseNum;

mi[0]++;

numOfYinzi++;

} else

{

if (yinzi[numOfYinzi-1] == baseNum)

mi[numOfYinzi-1]++;

else

{

yinzi[numOfYinzi] = baseNum;

mi[numOfYinzi]++;

numOfYinzi++;

}

}

srcNum /= baseNum;

break;

} else if (srcNum % (baseNum + 2) == 0)

{

if (numOfYinzi == 0)

{

yinzi[0] = (baseNum + 2);

mi[0]++;

numOfYinzi++;

} else

{

if (yinzi[numOfYinzi-1] == (baseNum + 2))

mi[numOfYinzi-1]++;

else

{

yinzi[numOfYinzi] = (baseNum + 2);

mi[numOfYinzi]++;

numOfYinzi++;

}

}

srcNum /= (baseNum + 2);

break;

}

}

if (baseNum * baseNum > srcNum)

{

if (numOfYinzi == 0)

{

yinzi[0] = srcNum;

mi[0]++;

numOfYinzi++;

} else

{

if (yinzi[numOfYinzi-1] == srcNum)

mi[numOfYinzi-1]++;

else

{

yinzi[numOfYinzi] = srcNum;

mi[numOfYinzi]++;

numOfYinzi++;

}

}

srcNum = 1;

}

}

}

if (numOfYinzi == 1 && mi[0] == 1)

printf("is a prime number.\n**************************************************\n");

else

{

printf("= ");

for (int ww = 0; ww < numOfYinzi-1; ww++)

{

if (yinzi[ww] != 0)

mi[ww] == 1 ? printf("%I64u x ", yinzi[ww]) : printf("%I64u^%d x ", yinzi[ww], mi[ww]);

}

mi[numOfYinzi-1] == 1 ? printf("%I64u\n", yinzi[numOfYinzi-1]) : printf("%I64u^%d\n", yinzi[numOfYinzi-1], mi[numOfYinzi-1]);

printf("**************************************************\n");

}

}

}

int main(int argc,char *argv[])

{

system("title Factorization v1.1 -- DevilHand Presents");

unsigned __int64 srcNum = 0;

unsigned __int64 baseNum = 5;

int scanfRet = 0;

int suanfa = 2;

printf("**************************************************\n");

printf(" * Factorization v1.1\n");

printf(" * DevilHand Presents 2011-03-18\n");

printf(" * Email: DevilHand@126.com\n");

printf("**************************************************\n");

while (1)

{

srcNum = 0;

do {

fflush(stdin);

printf("Input a number: ");

scanfRet = scanf("%I64u", &srcNum);

} while (EOF == scanfRet || 0 == scanfRet);

printf("Result: %I64u ", srcNum);

PrintFactorization(srcNum, baseNum);

}

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值