求对称数(C语言)

该博客主要介绍如何用C语言解决寻找大于给定数n的最小对称数问题。解题思路包括:输入对称数的个数t和数值n,将n的每一位存储到数组,然后根据n的奇偶性和中间数字的情况进行判断和更新,输出结果。在实现过程中,注意要检查中间数字及其对称位置的值,避免只考虑最中间的数为9而忽略其两侧的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

题目描述:

如果给定一个对称数n,请你求出大于n的最小对称数(即这个数从左向右读和从右向左读是完全一样的)。

输入:

第一行输入t(0<t<19999);
接下来的t行,每行输入一个n(0<n<10^9)。

输出:

与输入相对应,每行输出一个大于n的最小对称数m。

样例输入 :

6
1
11
99
789987
123454321
999999999

样例输出:

2
22
101
790097
123464321
1000000001

源代码:

#include<stdio.h>
int jiweishu(long long n);//判断n是几位数
int quanjiu(long long n);//全为9
in
在C语言中,字加密通常使用公开密钥体系(如RSA),其中涉及非对称加密。以下是使用RSA算法进行加解密的一个简单示例。注意这只是一个基础版本,并未包含完整的错误处理和安全实践: ```c #include <stdio.h> #include <stdlib.h> #include <openssl/pem.h> #include <openssl/rsa.h> // 加载公钥和私钥 RSA *load_key(const char *filename, const char *key_type) { FILE *file = fopen(filename, "r"); if (!file) return NULL; RSA *key; if (strcmp(key_type, "public") == 0) { key = PEM_read_RSAPublicKey(file, NULL, NULL, NULL); } else if (strcmp(key_type, "private") == 0) { key = PEM_read_RSAPrivateKey(file, NULL, NULL, NULL); } fclose(file); return key; } // 加密函 size_t encrypt(RSA *rsa, const unsigned char *plaintext, size_t plaintext_len, unsigned char **ciphertext) { unsigned int ciphertext_len; RSA_public_encrypt(plaintext_len, plaintext, ciphertext, rsa, RSA_PKCS1_OAEP_PADDING); ciphertext_len = SSL_get_bytes_from_cipher_block(*ciphertext); return ciphertext_len; } // 解密函 size_t decrypt(RSA *rsa, const unsigned char *ciphertext, size_t ciphertext_len, unsigned char **plaintext) { unsigned char decrypted_data[ciphertext_len]; size_t decrypted_len; RSA_private_decrypt(ciphertext_len, ciphertext, decrypted_data, rsa, RSA_PKCS1_OAEP_PADDING); decrypted_len = strlen((char *)decrypted_data); // 这里假设是明文,实际应用需解析解码 *plaintext = malloc(decrypted_len + 1); memcpy(*plaintext, decrypted_data, decrypted_len); (*plaintext)[decrypted_len] = '\0'; return decrypted_len; } int main() { // 读取公钥和私钥(这里仅做演示,实际操作需要妥善保存并加载) RSA *public_key = load_key("public.pem", "public"); RSA *private_key = load_key("private.pem", "private"); // 示例据 unsigned char plaintext[] = { 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd' }; size_t plaintext_len = sizeof(plaintext); // 加密 unsigned char *ciphertext = NULL; size_t ciphertext_len = encrypt(public_key, plaintext, plaintext_len, &ciphertext); printf("Encrypted message: %s\n", ciphertext); // 解密 unsigned char *decrypted_text = NULL; size_t decrypted_len = decrypt(private_key, ciphertext, ciphertext_len, &decrypted_text); printf("Decrypted message: %s\n", decrypted_text); free(ciphertext); free(decrypted_text); RSA_free(public_key); RSA_free(private_key); return 0; } ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值