质数因子
来源:互联网 发布:知已唯美图片 编辑:程序博客网 时间:2024/05/20 00:50
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>#include <string.h>int JudgeDigit(long num);unsigned int Montgmery( int num, long index, long mod)//蒙哥马利快速幂算法 (num^index)%mod{ int tmp = 1;//保存奇数是剩下的那个数num %= mod; // 假设(2^7)%3 2%3while (index > 1){if (index & 1) //按位与 ,除最低位不变其他位置0 ,如果为1 说明是奇数,否则偶数{tmp = (tmp*num) % mod;// 假设(2^7)%3 第一次为 1%3=1 (1%3)(2%3)%3=2%3 第二次 为((2%3)*(4%3))%3=8%3}num = (num*num) % mod;//(2%3)(2%3)%3=4%3 (4%3)(4%3)%3=16%3index /= 2;}int result = (num*tmp) % mod; //(16%3)(8%3)%3=(2^7)%3return result;}int IsPrime( long num)//判断num是否是质数 ,如何是返回1,不是返回-1;{if (num < 2){return -1;}static int PrimeList[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};//质数表const int count = sizeof(PrimeList) / sizeof(unsigned int);//求出置数表中数的个数//printf("%d\n", count);int i = 0;for (i = 0; i < count; i++){if (num == PrimeList[i]){return 1;}//printf("%d\n", Montgmery(PrimeList[i], num - 1, num));if (Montgmery(PrimeList[i], num - 1, num) != 1)//按照表中的质素算,如果全都为1则可以大概说num是质数{return -1;}}return 1;}int IntToStr(int num, char *str) //int 型转换成 字符型{int ret = 0;if (str == NULL){ret = -1;return ret;}if (num == 0){*str = '0';}int count = JudgeDigit(num);int tmp = num;str[count] = ' ';str[count+1] = '\0';while (tmp){str[count - 1] = (tmp % 10) + 48;tmp /= 10;count--;}return ret;}int JudgeDigit(long num) // 判断位数{int count = 1;while (num/10){count++;num /= 10;}return count;}int GetResult( long uIDataInput, char *uIDataOutput) //转换成质数{int ret = 0;if (uIDataOutput == NULL){ret = -1;return ret;}if (IsPrime(uIDataInput) == 1){sprintf(uIDataOutput, "%d", uIDataInput);return ret;}int i = 0;char buf[1024] = { 0 }; // 用来存储每次转换的int型变量long tmp = uIDataInput;for (i = 2; i < tmp; i++){if (IsPrime(i) == -1){continue;}while(uIDataInput%i == 0){ memset(buf, 0, 1024); ret = IntToStr(i, buf); if (ret != 0) { printf("ERROR FUNC IntToStr() %d", ret); return ret; } strcat(uIDataOutput, buf); uIDataInput /= i;}if (uIDataInput == 1){break;}}return ret;}int main(){int ret = 0;long num;char *str = NULL;str = (char *)malloc(sizeof(char) * 100);if (str == NULL){printf("ERROR!!!");return 0;}while (scanf("%d", &num) != EOF){memset(str, 0, sizeof(char)*100);ret = GetResult(num, str);if (ret != 0){printf("ERROR !! FUNC GetResult() %d", ret);return 0;}int length = strlen(str);str[length - 1] = '\0';printf("%s\n", str);}free(str);return 0;}
题目描述
功能:输入一个正整数,按照从小到大的顺序输出它的所有质数的因子(如180的质数因子为2 2 3 3 5 )
详细描述:
函数接口说明:
public String getResult(long ulDataInput)
输入参数:
long ulDataInput:输入的正整数
返回值:
String
输入描述:
输入一个long型整数
输出描述:
按照从小到大的顺序输出它的所有质数的因子,以空格隔开
输入例子:
180
输出例子:
2 2 3 3 5
0 0
- 质数因子
- 质数因子
- 质数因子
- 质数因子
- 质数因子
- 质数因子
- 质数因子
- 质数因子
- 质数因子
- 质数因子
- 质数因子
- 质数因子
- 质数因子
- 质数因子
- 质数因子
- 质数因子
- 质数因子
- 质数因子
- 结构体进程描述进程
- 为什么UI线程是不安全线程
- 用IO流的知识将两个文件写到一个文件中(设置字符编码)
- SwipeRefreshLayout 源码
- acm_最短路
- 质数因子
- 处理下载文件时中文乱码
- 对于解析xls(excle表格数据 )
- servlet读书笔记
- Android 中获取当前的 Activity
- 统计一行字符中有多少个单词
- macbook win10下声音键和触摸板右键键盘灯无法使用解决方法
- 上海科目三要点
- 自定义Builder的构造