用C语言编写一个程序输入n输入一个n行n列的矩阵输出所有靠边元素之和
时间: 2023-05-20 10:04:31 浏览: 79
以下是用C语言编写的程序,可以输入一个n行n列的矩阵,并输出所有靠边元素之和:
```
#include <stdio.h>
int main() {
int n, i, j, sum = 0;
printf("请输入矩阵的行数和列数:");
scanf("%d", &n);
int matrix[n][n];
printf("请输入矩阵的元素:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
if (i == 0 || i == n - 1 || j == 0 || j == n - 1) {
sum += matrix[i][j];
}
}
}
printf("所有靠边元素之和为:%d\n", sum);
return 0;
}
```
关于lua closure factory 完整代码的问题,以下是一个简单的示例:
```
function make_counter()
local count = 0
return function()
count = count + 1
return count
end
end
counter1 = make_counter()
print(counter1()) -- 输出 1
print(counter1()) -- 输出 2
counter2 = make_counter()
print(counter2()) -- 输出 1
```
这个示例中,make_counter() 函数返回了一个闭包,该闭包包含一个局部变量 count 和一个匿名函数,该匿名函数可以访问并修改 count 变量的值。每次调用 make_counter() 函数都会创建一个新的闭包,因此可以创建多个计数器,它们之间互不干扰。
关于中文加密的问题,可以使用一些加密算法,例如简单替换密码、凯撒密码、栅栏密码等。以下是一个简单的简单替换密码的加密和解密示例:
```
#include <stdio.h>
#include <string.h>
char* encrypt(char* plaintext, char* key) {
int i, len = strlen(plaintext);
char* ciphertext = (char*)malloc(len + 1);
for (i = 0; i < len; i++) {
if (plaintext[i] >= 'a' && plaintext[i] <= 'z') {
ciphertext[i] = key[plaintext[i] - 'a'];
} else if (plaintext[i] >= 'A' && plaintext[i] <= 'Z') {
ciphertext[i] = key[plaintext[i] - 'A'] - 32;
} else {
ciphertext[i] = plaintext[i];
}
}
ciphertext[len] = '\0';
return ciphertext;
}
char* decrypt(char* ciphertext, char* key) {
int i, len = strlen(ciphertext);
char* plaintext = (char*)malloc(len + 1);
for (i = 0; i < len; i++) {
if (ciphertext[i] >= 'a' && ciphertext[i] <= 'z') {
plaintext[i] = 'a' + strchr(key, ciphertext[i]) - key;
} else if (ciphertext[i] >= 'A' && ciphertext[i] <= 'Z') {
plaintext[i] = 'A' + strchr(key, ciphertext[i] + 32) - key;
} else {
plaintext[i] = ciphertext[i];
}
}
plaintext[len] = '\0';
return plaintext;
}
int main() {
char plaintext[] = "这是一段中文明文";
char key[] = "qwertyuiopasdfghjklzxcvbnm";
char* ciphertext = encrypt(plaintext, key);
printf("加密后的密文:%s\n", ciphertext);
char* decryptedtext = decrypt(ciphertext, key);
printf("解密后的明文:%s\n", decryptedtext);
free(ciphertext);
free(decryptedtext);
return 0;
}
```
该程序使用简单替换密码算法对中文明文进行加密和解密,其中 key 是一个由 26 个小写字母组成的密钥,plaintext 是待加密的明文,ciphertext 是加密后的密文,decryptedtext 是解密后的明文。加密和解密的过程都是通过遍历明文或密文的每个字符,并根据密钥进行替换来实现的。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)