检验素数
python
from math import sqrt
a = int(input("请输入一个数:"))
for i in range(2,int(sqrt(a))):
if a%i == 0:
print("该数不是素数")
break
else:
print("该数是素数")
# # 1既不是素数也不是合数
# #可以用flag做标志位
# b = int(input("请输入一个数:"))
# Flag = False
# for i in range(2,b):
# if b%i == 0:
# Flag = True
# if Flag:
# print("是合数")
# else:
# print("是素数")
cpp
#include <iostream>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv) {
int a = 0;
printf("请输入一个大于1的自然数:");
scanf("%d", &a);
int flag = 0;
for (int i = 2; i < a; i++)
{
if (a % i == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
{
printf("输入的数是一个素数");
}
else
{
printf("输入的数不是一个素数");
}
return 0;
}
逐字检查法
自解
python
解法1
a = "python"
b = "typhon"
list1 = list(b)
print(list1)
def judge(a,b):
for i in range(len(a)):
for j in range(len(a)):
if a[i] == b[j]:
list1[j] = None
break
for i in range(len(a)):
list1[i] == None
if list1[i] != None:
return False
return True
if judge(a,b) == True:
print("是变位词")
elif judge(a,b) == False:
print("不是变位词")
python
解法2
a = "python"
b = "typhon"
def judge(a,b):
list1 = list(a)
list2 = list(b)
j = len(list2)
list1.sort()
list2.sort()
for i in range(len(list1)):
if list1[i] == list2[i]:
pass
else:
return False
return True
if judge(a,b) == True:
print("是变位词")
elif judge(a,b) == False:
print("不是变位词")
他解
鹏哥C语言
、