Blog park job 1

First, FIG Mind

img

Second, understanding the syntax

(1)if-else

1. Structure

if (条件) 
   语句一;
else 
   语句二;

2. Usage: solving the expression, if the condition is true, the output a statement, the statement is false output two

3. Question: containing multiple if-else statements, which sometimes judge else if errors belong

4. Solution: listening to the teacher to explain and re-look at their own analysis of the structure

(2)switch

1. Structure

switch (表达式){
     case 常量表达式1:语句段1; break;
     case 常量表达式2:语句段2;break;
                  ...
     case 常量表达式n:语句段n; break;
     default:         语句段n+1;break;
}

2. Method: evaluating an expression, when the value of the expression is equal to a constant expression, the expression of the corresponding measurement performed paragraph statement, the statement after the segment, if any default is not equal to a, is performed, and finally execution break out of the switch

3. Question: No

4. Solution: No

(3)for

1. Structure

for(表达式1;表达式2;表达式3)
    循环体语句

2. Method: first calculation expression 1, expression 2 then determines, if the value is true, statement executes first loop, and then evaluate the expression 3, if the value is false, then the loop ends

3. Question: The order of expression 23 sometimes confused, when executed i ++ i is the value or the value of the loop on the cycles sometimes confused

4. Resolution: shown in the block diagram on rough paper

(4)while

1. Structure

while(表达式)
      循环体语句;

2. Usage: When the value of an expression of true, the loop executes until expression evaluates to false, the loop terminates and execution while the next statement

3. Question: did not notice the loop body is only a statement

4. Solution: change a single statement

(5)do-while

1. Structure

do{
   循环体语句
} while (表达式);

2. Method: first into the loop, the first loop execution statement, and then check the condition of the control cycle, i.e. calculation expression, if the value is true, the cycle continues until the value is false, executing the next statement

3. Question: conditions when determining whether to continue in error

4. Solution: more practice

(6) break statement and continue statement

1. Structure

switch (表达式){
     case 常量表达式1:语句段1; break;
     case 常量表达式2:语句段2;break;
                  ...
     case 常量表达式n:语句段n; break;
     default:         语句段n+1;break;
}
for(表达式1;表达式2;表达式3)
{
     语句1;
     continue;
     语句2;
}

2. Method: break when a plurality of loop condition loop structure appears to be jointly controlled by a loop statement and break statement expression

continue skip the next loop in the next cycle continues with the statement continue

3. Question: No

4. Solution: No

Three, pta score screenshots

2.3, 2.4 jobs

Chapter 3 jobs

4.1 Job

Four, pta code analysis

2.3, 2.4 jobs

7-2 before seeking odd-number sequence and the N items (15 minutes)

This problem requires programming, calculates the sequence 1 + 1/3 + 1/5 + ... and the first N.

Input formats:

In a given input row positive integer N.

Output formats:

And output portion of the value S in accordance with the "sum = S" format in a row, six decimal place. Title ensure that the calculation result does not exceed the double precision.

Sample input:

23

Sample output:

sum = 2.549541
#include<stdio.h>
int main()
{
    int n,i,zi=1,mu=1;   
    double sum=0;        
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        sum+=zi*1.0/mu;
        mu+=2;
    }
    printf("sum = %lf\n",sum);
}

Thinking: 1.0 ratio with the input character obtained fraction, then accumulate

Cause: The last answer format flawed, pay attention to the title accuracy required

Harvest: Note that the answer given accuracy, do not forget to multiply 1.0, note that every law between

Chapter 3 jobs

7-10 student achievement statistics (15 points)

This question requires the preparation of program reads N percentile scores of students, the statistical distribution of a five-point performance. Percentile scores to the results of a five-point conversion rules:

  • A greater than or equal to 90 divided;
  • Less than 90 and greater than or equal to B 80;
  • Greater than or equal to 80 and less than 70 are C;
  • Less than 70 and greater than or equal to 60 D;
  • 60 is less than E.

Input formats:

In the first line input is given a positive integer N (≤1000), i.e., the number of students; given in the second row of the N th percentile score students, separated by a space therebetween.

Output formats:

The number of output A, B, C, D, E corresponding to the five-point distribution results in a row, separated by a space between the numbers, end of line can not have extra space.

Sample input:

7
77 54 92 73 60 65 69

Sample output:

1 0 2 3 1
#include<stdio.h>
int main()
{
    int N;
    int i,x;
    int a=0,b=0,c=0,d=0,e=0;
    while(scanf("%d",&N)!=EOF){
        
        for(i=0;i<N;i++){
            scanf("%d",&x);
        
        if(x>=90){
            a++;
        }else if(x>=80){
            b++;
        }else if(x>=70){
            c++;
        }else if(x>=60){
            d++;
        }else{
            e++;,
        }
    }
        printf("%d %d %d %d %d\n",a,b,c,d,e);
        
    } 
    return 0;
}

Thinking: After input characters, if-else by a plurality of cycles, different fractions separated segments, the output level

Error: five levels, the last five results have

Harvest: Note that all output results

4.1 Job

7-8 guessing game (15 points)

Guessing game consoles is to make a positive integer randomly generated within a 100, the user enters a number of them guessing, you need to write a program to automatically compare them to guess the number is randomly generated, and prompts big ( "Too big "), or small (" Too small "), equal representation guessed. If you guessed, the program ends. Also requires that the number of statistical guess, if you guess the number 1, suggesting that "Bingo!"; If less than three times the number of guessed, the prompt "Lucky You!"; But if more than three times in N (> 3) times less (including the N-th) guess the number, the prompt "Good guess!"; if more than N times without guessed, the prompt "Game over", and the program ends. If before reaching the N times, the user enters a negative number, and output "Game Over", and the program ends.

Input formats:

Two positive integer not exceeding 100, random numbers are generated by the gaming machine, and the maximum number of guesses input of the first row given N. Finally, each line of a given user's input until a negative number appears.

Output formats:

The output of each guess in a row corresponding results until the results of the output guess or "Game Over" is ended.

Sample input:

58 4
70
50
56
58
60
-2

Sample output:

Too big
Too small
Too small
Good Guess!
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int num, times;
    scanf("%d %d", &num, &times);
    int i = 0, guess;
    while (i < times) {

        scanf("%d", &guess);
        if (guess < 0) {
            printf("Game Over\n");
            break;
        }
        if (guess > num) {
            printf("Too big\n");
        }
        if (guess < num) {
            printf("Too small\n");
        }
        if (guess == num) {
            if (i == 0) {
                printf("Bingo!\n");
            } else if (i < 3) {
                printf("Lucky You!\n");
            }
            if (i >= 3) {
                printf("Good Guess!\n");
            }
                break;
        }
        i++;
    }
    if (i >= times) {
        printf("Game Over\n");
    }
    return EXIT_SUCCESS;
}

Ideas: Multiple use if-else statement to compare the size of the output guess and num

Cause: The confusion of different combinations of if-else

Harvest: Note else with the nearest if a group of

Guess you like

Origin www.cnblogs.com/lim-M/p/11682162.html