#10 Swift 的變數和常數練習( variable & constant)

1. Sum

You are given two variables a and b, compute their sum and store it in another variable named sum then print the result.

var a = 5
var b = 7
var sum = a + b
print(sum)

2. Seconds

Determine the number of seconds in a year and store the number in a variable named secondsInAYear.

var secondsInAYear = 60 * 60 * 24 * 365
print("365天的秒數:\(secondsInAYear)")
//一年為365天

secondsInAYear = 60 * 60 * 24 * 366
print("366天的秒數:\(secondsInAYear)")
//一年為366天

3. Pixels

Your are given the width and height of a screen in pixels. Calculate the total number of pixels on the screen and store the result in a variable named numberOfPixels.

var width = 1179
var height = 2556
var numberOfPixels = width * height
print(numberOfPixels)

4. Sum and Difference

You are given the sum and the difference of two numbers. Find out the values of the original numbers and store them in variables a and b.

var sum = 21 /*a+b*/
var difference = 3 /*a-b*/
var a = (sum + difference) / 2
var b = sum - a
print("a=\(a)")
print("b=\(b)")

5. L Area

You are given four variables width, height, x, y that describe the dimensions of a L-shape as shown in the image below. Determine the perimeter and area of the described L-shape. Store the value of the perimeter in a variable named perimeter, and the area in a variable named area.

//第一張圖
var x = 2
var y = 2
var height = 4
var width = 8
var perimeter = (width + height) * 2
var area = (x * height) + (y * (width - x))
print("pic1 perimeter=\(perimeter)")
print("pic1 area=\(area)")

//第二張圖
var x = 4
var y = 4
var height = 12
var width = 8
var perimeter = (width + height) * 2
var area = (x * height) + (y * (width - x))
print("pic2 perimeter=\(perimeter)")
print("pic2 area=\(area)")

6. Swap

Given two variable a and b, swap their values. That is the new value of a will become the old value of b and vice versa.

var a = 5
var b = 10
let sum = a + b
b = a
a = sum - b
print("a=\(a)")
print("b=\(b)")

7. Last digit

You are given a number a. Print the last digit of a.

var a = 329
var b = a % 10
print(b)

8. Dog Years

You are given Rocky’s age in dog years. Print Rocky’s age in human years. You know that 1 human year is 7 dog years.

var rockyDogAge = 30
var rockyHumanAge = rockyDogAge / 7
print(rockyHumanAge)

9. Brothers

Everyone hates solving word problems by hand so let’s make a program to solve them for us.
x years from now Alice will be y times older than her brother Bob. Bob is 12 years old. How many years does Alice have?

var x = 1
var y = 2
var bobAge = 12

//alice + x = y * (12 + x)
//alice = y * (12 + x) - x

var aliceAge = y * (12 + x) - x
print(aliceAge)

10. Apples and Oranges

You have x apples. Bob trades 3 oranges for 5 apples. He does not accept trades with cut fruit.
How many oranges can you get from Bob and how many apples will you have left?

The number of apples you will have left should be stored in a variable named apples. The number of oranges you will have after the trade should be stored in a variable named oranges.

var originalApples = 49
var originalOranges = 0

var myApples = originalApples % 3
var myOranges = originalApples / 5 * 3
print(myApples)
print(myOranges)

11. Boys and Girls

A class consists of numberOfBoys boys and numberOfGirls girls.
Print the percentage of boys in the class followed by the percentage of girls in the class. The percentage should be printed rounded down to the nearest integer. For example 33.333333333333 will be printed as 33.

var numberOfBoys = 27
var numberOfGirls = 38
var all = numberOfBoys + numberOfGirls

var percentageOfBoys = numberOfBoys * 100 / all
var percentageOfGirls = numberOfGirls * 100 / all
print("班上男生百分比:\(percentageOfBoys)%")
print("班上女生百分比:\(percentageOfGirls)%")

--

--