A. Introduction

We said in 1.5 that when writing a program, you start from an algorithm, which is the series of steps to solve your problem, and can be written in pseudo code. For instance, to solve the following problem:

You put $10,000 into a bank account that earns 5 percent interest per year. How many years does it take for the account balance to be double the original?

you can use this algorithm (written in pseudo code, of course there are other ways):

Start: year = 0, rate=0.05, balance = 10,000 Repeat steps below until balance reaches 20,000 Add 1 to the year value; Compute the interest as balance x 0.05 (i.e., 5 percent interest); Add the interest to the balance Print the final value of year

B. Try:

1. Suppose your cell phone carrier charges you $29.95 for up to 300 minutes of calls, and $0.45 for each additional minute, plus 12.5 percent taxes and fees. Give an algorithm to compute the monthly charge from a given number of minutes. What should replace "?" in the following pseudo code?

Start: set number of minutes to x; If x ? 300 then charge = 29.95 else charge = 29.95+(x-300)*0.45 set tax = ? * charge (i.e., 12.5% ) set total charge = ? Print out total charge

2. Consider the following pseudo code for finding the most attractive photo.

Line up all photos in a row; Pick the first photo and call it "the best so far"; For each photo in the row, repeat the following till reach the end of row: If it is more attractive than the "best so far": discard "the best so far" call this photo "the best so far" else do nothing (well, it's okay to omit this line) Move to the next photo The photo called "the best so far" is the most attractive photo

Is this an algorithm that will find the most attractive photo? Can you modify this algorithm to find the most ugly photo? Donald Trump's photo? the biggest number? the smallest number?

3. Write an algorithm to create a tile pattern composed of black and white tiles, with a fringe of black tiles all around and two or three black tiles in the center, equally spaced from the boundary.

The inputs to your algorithm are the total number of rows x and columns y. (x=7, y=9 is shown here. You can try x=6, y=8 or x=4, y=5, see how the pattern will change)

Number of rows = #rows

Number of Columns = #clms

Place #clms black tiles

Go down 1 line

Place 1 black tile

Place #clms - 2 white tiles

Place 1 black tile

Go down 1 line

Repeat the previous 4 steps x - 1 times

If #rows is even:

x = (#rows / 2) - 2

Else:

x = (#rows / 2) - 1.5

Place 1 black tile

Place x white tiles

Place #clms - (2 + 2x) black tiles

Place x white tiles

Place 1 black tile

Go down 1 line

If #rows is even:

repeat the previous 6 steps

Place 1 black tile

Place #clms - 2 white tiles

Place 1 black tile

Go down 1 line

Repeat the previous 4 steps x - 1 times

Place #clms black tiles

If first row or last row, fill all spaces with color

Fill in the first and last columns with color

Take the total number of rows and subtract 2 and divide that number by 2, if that number is a fraction round down.

Add that number to the 1st row and subtract that number form the last row. The remaining range will be the middle repeat this process for the columns and fill in the middle according to the ranges.

import java.util.Scanner;

class C621 {

public static void main(String[] args) {

Scanner in=new Scanner(

System.in);System.out.println("row");

int row=in.nextInt();

System.out.println ("column");

int column=in.nextInt();

System.out.println("the row is"+row);

System.out.println("the column is"+column);

for (int counter = 1; counter <= column; counter++)

{

System.out.print("R");

}

System.out.println();

for (int counterR = 1; counterR <= row-2; counterR++){

for (int counter = 1; counter <= column; counter++)

{

if(counter==1||counter==column)

{

System.out.print("R");

}

else{

System.out.print(" ");

}

}

System.out.println();

}

for (int counter = 1; counter <= column; counter++)

{

System.out.print("R");

if(row%2=0||column%2=0)

{

for (int counterC = 1; counterC<= column; counterC++)

{

if(counter=column/2||counter=column/2+1)

System.out.print("R");

}

else{

System.out.print(" ");

if(row%2>0||column%2=0)

{

for (int counterC = 1; counterC<= column; counterC++)

{

if(counter=column/2)

System.out.print("R");

}

}

但还有一些bug暂时没解决