字符串分割与质数问题

•连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组;
•长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
连续输入字符串(输入2次,每个字符串长度小于100)

输出描述:
输出到长度为8的新字符串数组

输入例子:
abc
123456789

输出例子:
abc00000
12345678
90000000
substr

#include<iostream>
#include<string>

using namespace std;

void Fun(string s)
    {
     if(s=="")
         {
         return;
     }
    if(s.size()%8 !=0)
        {
        s=s+"00000000";
        for(int i=0;i<int(s.size()/8);i++)
            {
            cout<<s.substr(i*8,8)<<endl;;
        }
    }
    else
        {
        for(int i=0;i<int(s.size()/8);i++)
            {
                cout<<s.substr(i*8,8)<<endl;;
            }
    }
}


int main()
    {
    string s1,s2;
    cin>>s1>>s2;
    Fun(s1);
    Fun(s2);
    return 0;
}

求一个数的质数问题

#include<string>
#include<math.h>
#include<iostream>
#include<string>

using namespace std;
void getResult(long ulDatainput)
    {
    //string result;
    while(ulDatainput%2==0)
        {
        ulDatainput=ulDatainput/2;
        cout<<2<<" " ;
    }
    for(int i=3;i<=sqrt(ulDatainput);i++)
        {
        while(ulDatainput%i==0)
            {
            ulDatainput=ulDatainput/i;
            cout<<i<<" ";
        }
    }
    if(ulDatainput>2)
        {
        cout<<ulDatainput<<" ";
    }
    cout<<endl;
    //return result;
}

int main()
    {
    long a;
    while(cin>>a)
        {
        getResult(a);
    }


    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用C语言实现RSA加解密的示例代码,可以对分割后的字符串进行加解密: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <math.h> #define MAXLEN 10000 #define BLOCKSIZE 3 /* 辗转相除法求最大公因数 */ int gcd(int a, int b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } /* 扩展欧几里得算法求乘法逆元 */ int exgcd(int a, int b, int *x, int *y) { if (b == 0) { *x = 1; *y = 0; return a; } else { int r = exgcd(b, a % b, x, y); int t = *x; *x = *y; *y = t - a / b * (*y); return r; } } /* 判断一个数是否为素数 */ int isprime(int n) { int i; if (n < 2) { return 0; } for (i = 2; i <= sqrt(n); i++) { if (n % i == 0) { return 0; } } return 1; } /* 生成一个指定位数的素数 */ int generate_prime(int bits) { int p; do { p = rand() % (int)pow(10, bits) + (int)pow(10, bits - 1); } while (!isprime(p)); return p; } /* 生成RSA公私钥对 */ void generate_key(int bits, int *p, int *q, int *n, int *e, int *d) { int phi, x, y; do { *p = generate_prime(bits / 2); *q = generate_prime(bits / 2); *n = *p * *q; phi = (*p - 1) * (*q - 1); *e = rand() % phi; } while (gcd(phi, *e) != 1); exgcd(*e, phi, &x, &y); *d = (x % phi + phi) % phi; } /* 加密一个字符 */ int encrypt_char(int c, int e, int n) { int i, r = 1; for (i = 0; i < e; i++) { r = (r * c) % n; } return r; } /* 解密一个字符 */ int decrypt_char(int c, int d, int n) { int i, r = 1; for (i = 0; i < d; i++) { r = (r * c) % n; } return r; } /* 加密一个字符串 */ void encrypt(char *msg, int len, int e, int n, int *ciphertext, int *ciphertext_len) { int i, j, c; *ciphertext_len = 0; for (i = 0; i < len; i += BLOCKSIZE) { c = 0; for (j = 0; j < BLOCKSIZE; j++) { if (i + j < len) { c = c * 256 + msg[i + j]; } else { c = c * 256; } } ciphertext[(*ciphertext_len)++] = encrypt_char(c, e, n); } } /* 解密一个字符串 */ void decrypt(int *ciphertext, int ciphertext_len, int d, int n, char *msg, int *msg_len) { int i, j, c; *msg_len = 0; for (i = 0; i < ciphertext_len; i++) { c = decrypt_char(ciphertext[i], d, n); for (j = BLOCKSIZE - 1; j >= 0; j--) { if (*msg_len < MAXLEN) { msg[(*msg_len)++] = (c >> (8 * j)) & 0xFF; } } } } int main() { srand((unsigned)time(NULL)); int bits = 512; int p, q, n, e, d; generate_key(bits, &p, &q, &n, &e, &d); printf("p = %d\nq = %d\nn = %d\ne = %d\nd = %d\n", p, q, n, e, d); char msg[MAXLEN], msg2[MAXLEN]; int ciphertext[MAXLEN], ciphertext_len, msg_len; printf("Enter message: "); gets(msg); int len = strlen(msg); encrypt(msg, len, e, n, ciphertext, &ciphertext_len); decrypt(ciphertext, ciphertext_len, d, n, msg2, &msg_len); msg2[msg_len] = '\0'; printf("Ciphertext: "); int i; for (i = 0; i < ciphertext_len; i++) { printf("%d ", ciphertext[i]); } printf("\n"); printf("Decrypted message: %s\n", msg2); return 0; } ``` 该程序会首先生成一个512位的RSA公私钥对,然后等待用户输入要加密的字符串,将字符串分成若干个3个字符一组的块,对每个块进行加密,最终输出加密后的密文和解密后的明文。注意:这个程序只是演示了如何使用RSA加密和解密一个字符串,实际应用需要考虑安全性等问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值