教学服务系统

 找回密码
 立即注册
搜索
查看: 538|回复: 2

信息计算2019级1班14号钱星一

[复制链接]

10

主题

23

帖子

114

积分

注册会员

Rank: 2

积分
114
发表于 2022-5-4 10:29:41 | 显示全部楼层 |阅读模式
5月3日网课截图

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

10

主题

23

帖子

114

积分

注册会员

Rank: 2

积分
114
 楼主| 发表于 2022-5-4 10:32:28 | 显示全部楼层
[code]#include<stdio.h>
#include<stdlib.h>

int long_n(int n);
int shuru(char* arr, int k, char* wei, int is_first);
void jiami(char* arr, int k, int e, int n);

int shuru(char* arr, int k, char* wei, int is_first)
{
        int i;
        char ch;
     
        if (is_first == 1)
                ch = getchar();
        else
                ch = *wei;
        for (i = 0; (i < k) && (ch != '\n'); i++) 
        {
                arr[i] = ch;
                ch = getchar();
        }
        *wei = ch; 
        for (i = i; i < k; i++)
                arr[i] = 'a';  
        if (ch == '\n')  
                return 0;
        else
                return 1;
}


void jiami(char* arr, int k, int e, int n)
{
        int m = 0, c = 1, i, j, t = 0, shu, temp, num = 0;
        int* array;
       
        for (i = 0; i < k; i++)
        {
                temp = 1;
                for (j = 0; j < (k - i - 1) * 2; j++)
                        temp = temp * 10;
                shu = (int)arr[i] - 97;
                m = m + temp * shu;
        }
        temp = e;
     
        do {
                temp = temp / 2;
                num++;
        } while (temp != 0);
        array = (int*)malloc(sizeof(int) * k); 
        temp = e;
       
        for (i = 0; i < num; i++)
        {
                array[i] = temp % 2;
                temp = temp / 2;
        }
       
        for (i = num - 1; i >= 0; i--)
        {
                t = t * 2;
                temp = c * c;
                if (temp > n)
                {
                        for (j = 0; temp - n * j >= 0; j++);
                        j--;
                        c = temp - n * j;
                }
                else
                        c = temp;
                if (array[i] == 1)
                {
                        t = t + 1;
                        temp = c * m;
                        if (temp > n)
                        {
                                for (j = 0; temp - n * j >= 0; j++);
                                j--;
                                c = temp - n * j;
                        }
                        else
                                c = temp;
                }

                e = e / 2;
        }
        temp = c;
        i = 0;
       
        do {
                temp = temp / 10;
                i++;
        } while (temp != 0);
        for (i; i < num; i++)
                printf("0");
        printf("%d", c);
}

int long_n(int n)
{
        int temp, i, j, k, shi, comp = 0;
        temp = n;

        for (i = 1; temp / 10 != 0; i++)
        {
                temp = temp / 10;
        }
        temp = i;
   
        if (i % 2 != 0)
        {
                i = i - 1;
                return i;
        }
   
        else
        {
                for (j = 0; j < i / 2; j++)
                {
                        shi = 1;
                        for (k = 0; k < temp - 2; k++)
                                shi = shi * 10;
                        comp = comp + shi * 25;
                        temp = temp - 2;
                }
                if (comp <= n)
                        return i;
                else
                {
                        i = i - 2;
                        return i;
                }
        }
}


int main()
{
        int p, q, e, d, n, fai_n, k, i, is_first = 1;
        char ch, * arr, wei = 'a';
        printf("请输入p、q、e值,用空格间隔开\n");
        scanf_s("%d%d%d", &p, &q, &e); 
        n = p * q;
        fai_n = (p - 1) * (q - 1);   //Φ(n)
        for (k = 0; (k * n + 1) % e != 0; k++);
        if ((k * n + 1) % e == 0)
                d = (k * n + 1) / e;  //d * e ≡ 1 (mod Φ(n))
        k = long_n(n);
        k = k / 2;  
        ch = getchar();
        arr = (char*)malloc(sizeof(char) * k);  
        printf("请输入明文\n");
        while (1)
        {
                i = shuru(arr, k, &wei, is_first);  
                is_first = 0;  
                jiami(arr, k, e, n);  
                if (i == 0)  
                        break;
        }
        printf("\n");
        return 0;
}
回复

使用道具 举报

10

主题

23

帖子

114

积分

注册会员

Rank: 2

积分
114
 楼主| 发表于 2022-5-4 10:34:27 | 显示全部楼层
运行截图结果

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

教学服务系统

GMT+8, 2025-4-30 11:44 , Processed in 0.014585 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表