Closed vieyahn2017 closed 4 months ago
#!/usr/bin/python
# -*- coding: utf-8 -*-
import math
def is_prime(n):
if n <= 1:
return False
for i in xrange(2, int(math.sqrt(n)) + 1):
if n % i == 0:
return False
return True
def func(num):
s = 1 # (3,5)质数对单独处理,不再考虑
# 当n≥5时,如果一个n-1, n+1互为孪生质数,则n必定是6的倍数
for n in range(5, num + 1, 6): # 遍历5+6n数列
if n == 5: # (5,7)单独处理
s += 1
elif is_prime(n) and is_prime(n + 2): # 是否同时为素数
if n + 2 > num:
continue
else:
# print (n, n + 2)
s += 1
return s
if __name__ == "__main__":
num = int(input())
print(func(num))
if __name__ == "__main__2":
while True:
try:
num = int(input())
print(func(num))
except EOFError:
break
except ValueError:
break
import java.util.Scanner;
public class Main {
public static String fun(double dTestNo, int iBase) {
int[] iT = new int[10];
int iNo;
// System.out.printf("十进制正小数 %f 转换成 %d 进制数为: ", dTestNo, iBase);
for (iNo = 0; iNo < 10; iNo++) {
dTestNo *= iBase;
iT[iNo] = (int) dTestNo;
if (dTestNo >= 1.0) dTestNo -= iT[iNo];
}
StringBuffer sb = new StringBuffer();
sb.append("0.");
for (iNo = 0; iNo < 10; iNo++) {
sb.append(iT[iNo]);
}
return sb.toString();
}
public static void main(String[] args) {
double dTestNo = 0.795;
int iBase;
for (iBase = 2; iBase <= 9; iBase++) {
System.out.println(fun(dTestNo, iBase));
}
}
public static void main1(String[] args) {
Scanner sc = new Scanner(System.in);
double m = sc.nextDouble();
int n = sc.nextInt();
while (m != 0 && n != 0) {
System.out.println(fun(m, n));
m = sc.nextDouble();
n = sc.nextInt();
}
}
}
参考 https://blog.csdn.net/hanshileiai/article/details/8870550
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
public class Main {
public static void fun(int n) {
Set<Integer> res = new TreeSet<>();
for(int i = 1; i <= Math.sqrt(n); i++) {
if(n % i == 0) {
int k = n/i;
res.add(i);
res.add(k);
}
}
System.out.print(res.size());
for (Integer integer : res) {
System.out.print(" "+integer);
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
fun(n);
}
}
2012伦敦奥运会即将到来,大家都非常关注奖牌榜的情况,现在我们假设奖牌榜的排名规则如下:
1、首先gold medal数量多的排在前面; 2、其次silver medal数量多的排在前面; 3、然后bronze medal数量多的排在前面; 4、若以上三个条件仍无法区分名次,则以国家名称的字典序排定。 我们假设国家名称不超过20个字符、各种奖牌数不超过100,且大于等于0。
输入样例 1 5 China 32 28 34 England 12 34 22 France 23 33 2 Japan 12 34 25 Rusia 23 43 0
输出样例 1 China Rusia France Japan England
def func(contr_list):
"""
1.奖牌数是大到小排,名字是从小到大排。使用四个lambda函数的sort方法进行排序
2.先按照低优先级的条件进行排序,后面高优先级无法区分的就会已经呈现成低优先级条件的排序。
3.输入的都是str类型,进行比较的时候除了名字不用管,其他的要转换成int进行比较。
:param contr_list:
:return:
"""
contr_list.sort(key=lambda x: x[0])
contr_list.sort(key=lambda x: int(x[3]), reverse=True)
contr_list.sort(key=lambda x: int(x[2]), reverse=True)
contr_list.sort(key=lambda x: int(x[1]), reverse=True)
for one in contr_list:
print(one[0])
if __name__ == '__main__':
total = int(input())
contr_list = []
for i in range(total):
one_list = input().strip().split()
contr_list.append(one_list)
func(contr_list)
可信