列表求和,从键盘上输入一个列表舍弃列表中能被3整除的数值
时间: 2024-03-10 18:17:22 浏览: 38
可以使用 Python 编程语言来实现这个功能,具体代码如下:
```python
nums = input("请输入一个列表,用逗号隔开:")
nums_list = nums.split(",") # 将输入的字符串转换为列表
sum = 0
for num in nums_list:
if int(num) % 3 != 0:
sum += int(num)
print("列表中能被3整除的数值已经被舍弃,求和结果为:", sum)
```
以上代码中,首先从键盘上输入一个包含数值的列表,然后使用 `split()` 方法将其转换为列表类型。接着使用 `for` 循环遍历列表中的每一个数值,如果该数值不能被3整除,则将其加到累加器 `sum` 中。最后输出求和结果。
相关问题
列表求和舍弃被三整除的数
可以使用以下代码来实现:
```python
def sum_without_multiples_of_three(lst):
total = 0
for num in lst:
if num % 3 != 0:
total += num
return total
```
这个函数会遍历列表中的每个数字,如果这个数字不能被 3 整除,则将其加入总和中。最后返回总和即可。
c语言从键盘上输入两个2*3的矩阵求和
以下是从键盘上输入两个2*3的矩阵并求和的C语言代码示例:
```
#include <stdio.h>
int main() {
int A[2][3], B[2][3], C[2][3]; // 定义两个2*3矩阵和它们的和矩阵C
// 输入第一个矩阵A
printf("Enter the elements of matrix A:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
scanf("%d", &A[i][j]);
}
}
// 输入第二个矩阵B
printf("Enter the elements of matrix B:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
scanf("%d", &B[i][j]);
}
}
// 计算和矩阵C
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
C[i][j] = A[i][j] + B[i][j];
}
}
// 输出和矩阵C
printf("The sum of the two matrices is:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", C[i][j]);
}
printf("\n");
}
return 0;
}
```
运行示例:
```
Enter the elements of matrix A:
1 2 3
4 5 6
Enter the elements of matrix B:
6 5 4
3 2 1
The sum of the two matrices is:
7 7 7
7 7 7
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)