1,题目:判断101-200之间有多少个素数,并输出所有素数。
for (var i=101; i<200; i++){
var m=2
for( m; m<i; m++){
if(i%m==0 ){
break
}
}
if(m>=i){
print(i)
}
}
2,题目:假设有一个学习成绩的数组,利用条件运算符的嵌套来完成此题:学习成绩> =90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
var chengji=[92,78,56]
for (index, value) in chengji.enumerate(){
if(value<60){
print("C")
}else{
if(value>90){
print("A")
}else{
print("B")
}
}
}
以上是数组表示成绩,下面是字典
var chengji2=[String:String]()
var chengji=["tom":92,"suda":78,"penny":56]
for (name,score) in chengji{
if (score<60){
chengji2[name] = "C"
}else{
if(score>90){
chengji2[name] = "A"
}else{
chengji2[name] = "B"
}
}
print("(name):(score)")
}
print(chengji2)
3,题目:输入某年某月某日,判断这一天是这一年的第几天?
var year = 2015
var month = 6
var day = 23
var Feb:Int
var days:Int
if(year%4 == 0||(year%4==0 && year%100 != 0)){
Feb = 29
}else{
Feb = 28
}
switch month {
case 1:
days = 0
print("(year)年(month)月(day)日是(year)年的第(day+days)天")
case 2:
days = 31
print("(year)年(month)月(day)日是(year)年的第(day+days)天")
case 3:
days = 31+Feb
print("(year)年(month)月(day)日是(year)年的第(day+days)天")
case 4:
days = 62+Feb
print("(year)年(month)月(day)日是(year)年的第(day+days)天")
case 5:
days = 92+Feb
print("(year)年(month)月(day)日是(year)年的第(day+days)天")
case 6:
days = 123+Feb
print("(year)年(month)月(day)日是(year)年的第(day+days)天")
case 7:
days = 153+Feb
print("(year)年(month)月(day)日是(year)年的第(day+days)天")
case 8:
days = 184+Feb
print("(year)年(month)月(day)日是(year)年的第(day+days)天")
case 9:
days = 215+Feb
print("(year)年(month)月(day)日是(year)年的第(day+days)天")
case 10:
days = 245+Feb
print("(year)年(month)月(day)日是(year)年的第(day+days)天")
case 11:
days = 276+Feb
print("(year)年(month)月(day)日是(year)年的第(day+days)天")
case 12:
days = 306+Feb
print("(year)年(month)月(day)日是(year)年的第(day+days)天")
default:
print("输入有误")
}