面试编程题(swift版)

//1,有一对兔子从第三个月后每个月生一对兔子,小兔子长到第三个月后又生一对兔子,问每个月的兔子总数是多少


func rabbitSum(_num:NSInteger){

varf1 =1

varf2 =1

varm =0

ifnum <3{

print(f2)

return

}

for_in3...num {

m = f2

f2 += f1

f1 = m

}

print(f2)

}

//rabbitSum(5)

//2,判断101-200之间的素数

func primeNum(){

varn =false;

foriin101..<200{

forjin2..

ifi % j ==0{

n =true

break

}

}

ifn {

n =false

continue

}

print(i)

}

}

//primeNum()

//3,打印水仙花数

func flowerNum(){

foriin100..<1000{

leta = i /100

letb = i %100/10

letc = i %10

ifa*a*a+b*b*b+c*c*c == i {

print(i)

}

}

}

//flowerNum()

//4,输入字符统计字母数字空格和其他符号的个数

func getStrSum(str:String){

vars ="abc123ABC你Ky好z12漂tty45 亮;'"

varletter =0

varnumber =0

varchinese =0

varspace =0

varother =0

foruniins.unicodeScalars{

ifuni.value>=65&&uni.value<=90||uni.value>=97&&uni.value<=122{

letter +=1

}elseifuni.value>=48&&uni.value<=57{

number +=1

}elseifuni.value>=0x4e00&&uni.value<=0x9fbf{

chinese +=1

}elseifuni==" "{

space +=1

}else{

other +=1

}

}

print("字母:\(letter)")

print("数字:\(number)")

print("汉语:\(chinese)")

print("空格:\(space)")

print("其他:\(other)")

}

//getStrSum(str: "")

//5,小球从100米高空掉下,每次弹回是上一次的一半,第n次多高

func ballFallHeight(time:Int)->Double{

letheight =100.0

varn =1

for_in0..

n = n *2

}

letlastHeight = height /Double(n)

print("第",time,"弹回的高度",lastHeight)

returnlastHeight

}

funcball(time:Int)->Double{

iftime ==0{

return100

}else{

letm = time-1==0?0:ballFallHeight(time: time-1)*2

returnball(time: time-1)+m

}

}

print(ball(time:4))

你可能感兴趣的:(面试编程题(swift版))