zoukankan      html  css  js  c++  java
  • 编程小工具

    说明

    1、语言:C语言

    2、环境:VS2019

    随机素数(指定范围)生成器

    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <time.h>
    #define randomInt(a,b) (rand()%(b-a)+a)
    int prime(int n)
    {
    	int i;
    	if (n < 2) {
    		return -1;
    	}
    	else {
    		for (i = 2; i < n; i++) {//判断n在2~n-1中有没有因数
    			if (n % i == 0)//如果用可以除尽的数,则非素数
    				break;
    		}
    		if (i < n) {//存在2~n-1之间有因数
    			return -1;
    		}
    		else
    			return 0;
    	}
    	return 0;
    }
    
    int main()
    {
    	int  k,res,a,b;
    	printf("-------------素数生成器--------------
    ");
    	printf("           输入范围:[a,b)
    ");
    	printf("-------------------------------------
    ");
    	int ntime = 20;
    	while (ntime > 0)
    	{
    		scanf("%d %d", &a, &b);
    		srand((unsigned)time(NULL));
    		do
    		{
    			res = randomInt(a, b);
    			k = prime(res);
    		} while (k == -1);
    		printf("素数:%d
    ", res);
    		ntime--;
    		printf("还有%d次!
    ",ntime - 1);
    	}
    	system("pause");
    	return 0;
    }

    求逆元

    基于扩展欧几里得算法

    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <stdlib.h>
    
    int exgcd(int a, int b, int& x, int& y)//扩展欧几里得算法
    {
        if (b == 0)
        {
            x = 1, y = 0;
            return a;
        }
        int ret = exgcd(b, a % b, y, x);
        y -= a / b * x;
        return ret;
    }
    int getInv(int a, int mod)//求a在mod下的逆元,不存在逆元返回-1
    {
        int x, y;
        int d = exgcd(a, mod, x, y);
        return d == 1 ? (x % mod + mod) % mod : -1;
    }
    int main() {
        int m, n;
        puts("          扩展欧几里得求逆元
    ");
        puts("       对m * x = 1 mod n,求x
    ");
        printf("请输入m=");
        scanf("%d", &m);
        printf("请输入n=");
        scanf("%d", &n);
        printf("x=%d
    ", getInv(m, n));
        system("pause");
        return 0;
    }
    

    基于费马定理算法

    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
    	int m, n, x;
        puts("          基于费马定理求逆元
    ");
        puts("       对m * x = 1 mod n,求x
    ");
        printf("请输入m=");
        scanf("%d", &m);
        printf("请输入n=");
        scanf("%d", &n);
    	x = (int)pow(m, n - 2) % n;
    	printf("x=%d
    ", x);
    	system("pause");
    	return 0;
    }
    

    计算时间差

    引入头文件:
    
    #include <time.h>
    
    给出定义变量:
    
    time_t begin, end;
    
    前后分别写:
    
    begin = clock();
    
    ....
    
    end = clock();
    
    printf("
    			加密时间: %f seconds
    ", (double)(end - begin) / CLOCKS_PER_SEC);
    

    作者: Pam

    出处: https://www.cnblogs.com/pam-sh/>

    关于作者:网安在读

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 原文链接 如有问题, 可邮件(mir_soh@163.com)咨询.

  • 相关阅读:
    [POJ2752]Seek the Name, Seek the Fame
    [HDOJ1247]Hat’s Words
    [POJ2001]Shortest Prefixes
    不用代码,10分钟打造属于自己的第一款小程序
    不用代码,10分钟打造属于自己的第一款小程序
    不用代码,10分钟打造属于自己的第一款小程序
    移动端iPhone系列适配问题的一些坑
    移动端iPhone系列适配问题的一些坑
    【前端统计图】echarts多条折线图和横柱状图实现
    【前端统计图】echarts多条折线图和横柱状图实现
  • 原文地址:https://www.cnblogs.com/pam-sh/p/15037403.html
Copyright © 2011-2022 走看看