设定包含十个元素{1,5,3,1,2,3,7,16,3,9}的集合。从键盘输入0到9之间的任意一个数字,查找该数字在集合中出现的次数。

设定包含十个元素{1,5,3,1,2,3,7,16,3,9}的集合。从键盘输入0到9之间的任意一个数字,查找该数字在集合中出现的次数。

在python中,集合的中的元素是不重复的,而列表可以重复,所以如果要计算数字的出现次数,可以将其存入列表,然后遍历列表,再计算某个数字出现的次数即可。

代码如下:

参考链接:


5. 数据结构 — Python 3.11.4 文档 本章深入讲解之前学过的一些内容,同时,还增加了新的知识点。 列表详解: 列表数据类型支持很多方法,列表对象的所有方法所示如下: 多数列表方法示例: You might have noticed that methods like insert, remove or sort that only modify the list have no return value printed -- t... https://docs.python.org/zh-cn/3/tutorial/datastructures.html

python列表中的元素可以重复吗_python列表是否允许重复_weixin_39974409的博客-CSDN博客 Python列表允许重复,下面我们就来介绍几种判断列表是否重复的方法:一、用set方法去重后与原列表长度比较lst=[1,3,5,3,4,4,2,9,6,7]set_lst=set(lst)#set会生成一个元素无序且不重复的可迭代对象,也就是我们常说的去重iflen(set_lst)==len(lst):print('列表里的元素互不重复!')else:print('列表里有重复的元素!')二..._列表中的元素可以重复吗 https://blog.csdn.net/weixin_39974409/article/details/110783801

python 里面的 整除(// )、除(/)和取余%_python 整除_中南自动化学院“智能控制与优化决策“至渝的博客-CSDN博客 Python里面的/不再和c/c++一样整数除整数返回一个整数(int = int / int)。//才是这个功能,而/变成浮点数除法了。%还是取余不变,具体如下“ // ” 表示整数除法,返回整数 比如 6//2 结果为3,7//2结果也是3“ / ” 表示浮点数除法,返回浮点数 (即小数) 比如 6/2 结果为3.0 ,7/2=3.5“ %” 表示取余数 比如7/4 结果为3这三个运算..._python 整除 https://blog.csdn.net/qq_43657442/article/details/103576353



# https://blog.csdn.net/weixin_28871743/article/details/113723690
set1={1,5,3,1,2,3,7,16,3,9}
# print(set1)

# https://docs.python.org/zh-cn/3/tutorial/datastructures.html
# https://blog.csdn.net/weixin_39974409/article/details/110783801
# 将10个数字存入列表
list1=[1,5,3,1,2,3,7,16,3,9]
# print(list1)
num=int(input())  # 从输入获取一个数字字符串,然后转为int类型
times=0 # 该数字出现的次数
for i in list1: # 遍历列表

    temp= i
    # 判断当前的数字中,存在几个指定的数num
    while temp!=0: 
        t = temp%10      
        if t == num:  # 如果列表当前元素和输入的数字相同,则次数+1
            times=times+1
        # https://blog.csdn.net/qq_43657442/article/details/103576353    
        temp//=10
    if num==0 and i==0: #  判断列表元素为0的情况
        times=times+1

# 打印结果
print("数字"+str(num)+"在列表中出现的次数为:"+str(times))



img


#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;


int main()
{
        vector<int> nums{ 1,5,3,1,2,3,7,16,3,9 };
        unordered_map<int,int> map;
        for(int& x:nums){
                map[x]++;
        }
        for(auto& p:map){
                cout<<p.first<<":"<<p.second<<endl;
        }
}


  • 帮你找了个相似的问题, 你可以看下: https://ask.csdn.net/questions/7442477
  • 你也可以参考下这篇文章:蓝桥杯真题 19省3-数列求值 给定数列 1, 1, 1, 3, 5, 9, 17, …,从第 4 项开始,每项都是前 3 项的和。求第 20190324 项的最后 4 位数字。
  • 除此之外, 这篇博客: 给一维数组输入6个整数,假设为5,7,4,8,9,1中的 方法一 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • #include<stdio.h>
    int main()
    {
    	int i,j,k,a[6];
    	//输入
    	for(int i=0;i<6;i++){
            scanf("%d",&a[i]);
        }
    	for(i=0;i<6;i++)
    	{
    		for(j=0;j<6;j++)
    		{
    			k=(j-i)<0?j-i+6:j-i;
    			printf("%d ",a[k]);
    		}
    		printf("\n");
    	}
    	return 0;
    }
    
  • 以下回答由chatgpt基于相关博客总结生成:

    根据问题的描述,我们需要从键盘输入一个数字,并查找该数字在集合中出现的次数。具体的步骤如下:

    1. 首先,我们可以定义一个int型的数组a,用来存储给定的十个元素{1,5,3,1,2,3,7,16,3,9}。

    c int a[10] = {1,5,3,1,2,3,7,16,3,9};

    1. 接下来,从键盘输入一个数字num,取值范围为0到9。读取用户输入的数字可以使用scanf函数。

    c int num; printf("请输入一个数字(取值范围为0到9):"); scanf("%d", &num);

    1. 然后,我们可以使用一个循环遍历数组a,统计数字num在数组中出现的次数。

    c int count = 0; for (int i = 0; i < 10; i++) { if (a[i] == num) { count++; } }

    1. 最后,打印数字num在集合中出现的次数。

    c printf("数字 %d 在集合中出现了 %d 次。\n", num, count);

    完整代码如下:

    #include <stdio.h>
    
    int main() {
        int a[10] = {1,5,3,1,2,3,7,16,3,9};
        int num;
        printf("请输入一个数字(取值范围为0到9):");
        scanf("%d", &num);
    
        int count = 0;
        for (int i = 0; i < 10; i++) {
            if (a[i] == num) {
                count++;
            }
        }
    
        printf("数字 %d 在集合中出现了 %d 次。\n", num, count);
    
        return 0;
    }
    

    运行该代码,输入一个数字,即可查找该数字在集合中出现的次数。