開発環境
- OS X Mavericks - Apple(OS)
- Xcode 6.0 Beta 6
- Swift (プログラミング言語)
Practical Programming: An Introduction to Computer Science Using Python 3 (Pragmatic Programmers) (Paul Gries (著)、Jennifer Campbell (著)、Jason Montojo (著)、Lynn Beighley (編集)、Pragmatic Bookshelf)のChapter 9(Repeating Code Using Loops)、9.10(Exercises) 16.をSwiftで考えてみる。
9.10(Exercises) 16.
コード(Xcode)
main.swift
//
// main.swift
// sample16
//
// Created by kamimura on 9/21/14.
// Copyright (c) 2014 kamimura. All rights reserved.
//
import Foundation
var rat_1_weight:Double = 1
var rat_2_weight:Double = 1
let rat_1_rate:Int = 4
println("a.")
var weeks:Int = 0
var temp:Double = rat_1_weight
let rat_1_inc:Double = 1.0 + Double(rat_1_rate) / 100
while temp < rat_1_weight * 1.25 {
temp *= rat_1_inc
weeks += 1
}
println(weeks)
println("b.")
let rat_2_rate:Int = rat_1_rate - 1
let rat_2_inc:Double = 1 + Double(rat_2_rate) / 100
weeks = 0
while rat_1_weight < rat_2_weight * 1.1 {
rat_1_weight *= rat_1_inc
rat_2_weight *= rat_2_inc
weeks += 1
}
println("rat_1_weight:\(rat_1_weight), rat_2_weight:\(rat_2_weight), weeks:\(weeks)")
入出力結果(Console Output)
a. 6 b. rat_1_weight:1.48024428491834, rat_2_weight:1.34391637934412, weeks:10 Program ended with exit code: 0
0 コメント:
コメントを投稿