在python中Given a list of integers. Determine how many distinct numbers there are.
时间: 2024-09-20 20:04:08 浏览: 48
在Python中,如果你有一个包含整数的列表,可以使用集合(set)数据结构来确定列表中有多少个唯一的数字。集合是一种无序、不重复的元素序列。以下是实现这一功能的一个简单例子:
```python
def count_distinct_numbers(numbers_list):
# 使用 set 数据结构去除重复值,再获取长度即为唯一数字的数量
distinct_numbers = len(set(numbers_list))
return distinct_numbers
# 示例用法
numbers = [1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9]
distinct_count = count_distinct_numbers(numbers)
print(f"Number of distinct numbers: {distinct_count}")
```
在这段代码中,`set(numbers_list)`会创建一个新的集合,其中只包含列表中的不同整数。最后,`len(set(numbers_list))`返回集合中元素的数量,即为列表中独特数字的数量。
相关问题
The task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and output the positions of the input numbers. The hash function is defined to be H(key)=key%TSize where TSize is the maximum size of the hash table. Quadratic probing (with positive increments only) is used to solve the collisions. Note that the table size is better to be prime. If the maximum size given by the user is not prime, you must re-define the table size to be the smallest prime number which is larger than the size given by the user. Input Specification: Each input file contains one test case. For each case, the first line contains two positive numbers: MSize (≤10 4 ) and N (≤MSize) which are the user-defined table size and the number of input numbers, respectively. Then N distinct positive integers are given in the next line. All the numbers in a line are separated by a space. Output Specification: For each test case, print the corresponding positions (index starts from 0) of the input numbers in one line. All the numbers in a line are separated by a space, and there must be no extra space at the end of the line. In case it is impossible to insert the number, print "-" instead.
这道题的任务很简单:将一串不同的正整数插入哈希表中,并输出输入数字的位置。哈希函数的定义为 H(key)=key%TSize,其中 TSize 是哈希表的最大大小。使用二次探测(只增加正数)来解决碰撞。注意,表格大小最好为质数。如果用户给出的最大大小不是质数,则必须重新定义表格大小为大于用户给出大小的最小质数。
输入格式:每个输入文件包含一个测试用例。对于每种情况,第一行包含两个正数:MSize(≤10 4)和N(≤MSize),它们是用户定义的表大小和输入数字的数量。然后,在下一行中给出N个不同的正整数。一行中的所有数字由空格分隔。
输出格式:对于每个测试用例,在一行中打印输入数字的相应位置(索引从0开始)。一行中的所有数字由空格分隔,行末不能有多余的空格。如果无法插入数字,则打印“-”。
You are given a map of a building made up of n×m squares, and each square is either a floor or a wall. Find how many rooms are in the building. A room is defined by a group of any number of adjacently connected floor squares, that is surrounded by walls. Input The first input line has two integers n and m: the height and width of the map. Then there are n lines each containing m characters describing the map. Each character is either . (floor) or # (wall). Output Print one integer, the number of rooms. Constraints 1≤n,m≤1000 Sample Input: 5 8 ######## #..#...# ####.#.# #..#...# ######## Output: 3
Sure! Here's a C++ code implementation that solves the problem:
cpp
#include <iostream>
#include <vector>
using namespace std;
int n, m;
vector<vector<char>> building;
vector<vector<bool>> visited;
// Define possible movements: up, down, left, right
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
// Function to perform DFS to find rooms
void explore(int x, int y) {
visited[x][y] = true;
// Explore all possible neighboring cells
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
// Check if neighboring cell is within bounds and unvisited
if (nx >= 0 && nx < n && ny >= 0 && ny < m && building[nx][ny] == '.' && !visited[nx][ny]) {
explore(nx, ny);
}
}
}
int countRooms() {
int roomCount = 0;
// Initialize visited matrix
visited.assign(n, vector<bool>(m, false));
// Traverse through the building and count rooms
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (building[i][j] == '.' && !visited[i][j]) {
roomCount++;
explore(i, j);
}
}
}
return roomCount;
}
int main() {
cin >> n >> m;
// Read the building map
building.assign(n, vector<char>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> building[i][j];
}
}
int roomCount = countRooms();
cout << roomCount << endl;
return 0;
}
```
You can compile and run this code to get the desired output. Let me know if you have any further questions!
阅读全文