[COCI2006-2007#1] Modulo

题目链接   https://www.luogu.org/problemnew/show/P4325

题目描述

Given two integers A and B, A modulo B is the remainder when dividing A by B. For example, the numbers 7, 14, 27 and 38 become 1, 2, 0 and 2, modulo 3. Write a program that accepts 10 numbers as input and outputs the number of distinct numbers in the input, if the numbers are considered modulo 42.

给出10个整数,问这些整数%42后有多少个不同的余数。 输入

输入包含10个小于1000的非负整数,每行一个。 输出

输出它们%42后,有多少个不同的余数。 说明

第一个样例的十个结果是1,2,3,4,5,6,7,8,9,10,有10个不同的结果;第二个样例结果都是0,只有一个不同的结果;第三个样例余数是39,40,41,0,1,2,40,41,0,1,有0,1,2,39,40,41这六个不同的结果。

输入输出格式

输入格式:

The input will contain 10 non-negative integers, each smaller than 1000, one per line.

输出格式:

Output the number of distinct values when considered modulo 42 on a single line.

输入输出样例

输入样例#1:

1
2
3
4
5
6
7
8
9
10

输出样例#1:

10

输入样例#2:

42
84
252
420
840
126
42
84
420
126

输出样例#2:

1

输入样例#3:

39
40
41
42
43
44
82
83
84
85

输出样例#3:

6

既然是要不同的取模结果,那就可以先得出所有余数,放到列表num中,然后整理这个列表:使用set()去重(chóng)

参考文章https://www.cnblogs.com/chjbbs/p/5729540.html

虽然set()后顺序改变,但本题对顺序并无要求

所以输出最终列表的长度即为余数个数

# -*- coding: utf-8 -*-
num = []
for i in range(0, 10):
    n = input()       #输入整数
    m = int(n)%42     #直接对输入的整数取余
    num.append(m)     #将所得余数加入列表num中

num = list(set(num))  #使用set()除去重复
ans = len(num)        #所得结果(不同的余数)即为去重后列表的长度
print(ans)
发布了58 篇原创文章 · 获赞 40 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/baidu_41248654/article/details/94035039