C language Job 1

A. Mind FIG.

II. Syntax know

1.if—else.

if(条件1)
{
  语句1;
}
else
{
  语句2;
}

Problems encountered: for if and else paired off understanding is not enough, resulting in abnormal program to determine the structure.
Solution: keep in mind else and if matching criteria: its nearest else, does not match the other else if ever match. And multilayer indented writing format when editing the code, the structure is determined so structured.
Note: When else do not need to edit the conditions for the editor to develop good habits, braces if-else statements fight it.

2.switch.

switch(整形表达式)
{ 
    case 常量表达式1:  语句1;break;
    case 常量表达式2:  语句2;break;
    … 
    case 常量表达式n:  语句n;break;
    default:  语句n+1;

}

Challenges: too dependent switch, sometimes leading to edit out bad code quality, not easy inspection and maintenance procedures later.
Resolution: The obvious use in solving problems only in the advantages of a switch statement switch statement, caution, wisdom with the switch statement.
Note: switch statement break statement after each case statement is not a must have, this mechanism allows the use of the switch statement is greatly increased, so to arrange the location of the break in the switch statement wisdom.

3.for.

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

Challenges: In some problems, often control the number of cycles is not good for the statement, leading to the calculation result dislocation.
Solution: Before you use the for statement, write the first program to simulate the drawing on paper, the calculation is correct and then hit the code.
Note: Expressions for statement 1 is only responsible for providing a starting point at the beginning, the operation only once. Expression 2 is again whether the determination condition of the execution cycle. 3 to provide variable expression cycles, avoid the program into an infinite loop.

4.while.

while(循环条件)
{
    循环体语句
}

Note: while the statement is a loop statement, the other loop to write code that almost every statement into a while, so in their own cycle can not read a code, it may well be a way into the while statement, one can see things in a different way, and secondly to enhance their own degree of understanding of this code.

5.do-while.

do
{
    循环体语句
}while(循环条件);

Challenges: This statement is circulating loop of the first two ideas are not the same, the same conditions in this cycle statement is likely to output different results.
Solution: understanding the principles of this statement and the cycle of circulation special order, pay more training, when he saw a problem you can determine whether a do-while statement, it means have a more accurate understanding of the do-while.
Note: It should be noted that the order of do-while loop statement with the first two are not the same loop, then determine the order of the first cycle determines its cycle at least once properties that may in the settlement as "a calculated number It seems handy when the number of bits "of this problem, we should make good use of this feature, but remember to add the final statement of the semicolon.

6.break.

switch(整形表达式)
{ 
    case 常量表达式1:  语句1;break;
    … 
    case 常量表达式n:  语句n;break;
    default:  语句n+1;

}

for(表达式1; 表达式2; 表达式3)
{
    语句1;
     continue;
    语句2;
        
}

Note: break statement may appear in the selection structure and loop structure in, but continue statement can only be used in the loop statement, continue statement after use, this cycle is immediately terminated, after the statement of no longer continue to perform, directly next round cycle. The break statement is currently executing statement out of line, both have their own characteristics, each have their own role.

pta Score Screenshot

2.3, 2.4 points

Chapter 3 scores

Score 4.1

Four .pta code analysis

1.7-8 N factorial sequences before and (15 minutes)

本题要求编写程序,计算序列 1!+2!+3!+⋯ 的前N项之和。
输入格式:
输入在一行中给出一个不超过12的正整数N。
输出格式:
在一行中输出整数结果。
输入样例:
5
输出样例:
153
#include<stdio.h>
int fac(int n)    /*函数声明*/
{
    int mul = 1;
    for (int i = n; i > 0; i--)
    {
        mul = mul*i;  
    }
    return mul;    /*将结果返回主函数*/
}
int main()   
{
    int n = 0;
    scanf("%d", &n);
    int sum = 0;
    int i = 1;
    while (i <= n)
    {
        sum = sum + fac(i);/*调用函数*/
        i++;
    }
    printf("%d", sum);
    system("pause");
    return 0;
}

Problem: The main program can not think of how to write, there is the function does not know how to define;

2.7-5 query fruit prices (15 points)

给定四种水果,分别是苹果(apple)、梨(pear)、桔子(orange)、葡萄(grape),单价分别对应为3.00元/公斤、2.50元/公斤、4.10元/公斤、10.20元/公斤。
首先在屏幕上显示以下菜单:
[1] apple
[2] pear
[3] orange
[4] grape
[0] exit
用户可以输入编号1~4查询对应水果的单价。当连续查询次数超过5次时,程序应自动退出查询;不到5次而用户输入0即退出;输入其他编号,显示价格为0。
输入格式:
输入在一行中给出用户连续输入的若干个编号。
输出格式:
首先在屏幕上显示菜单。然后对应用户的每个输入,在一行中按格式“price = 价格”输出查询结果,其中价格保留两位小数。当用户连续查询次数超过5次、或主动输入0时,程序结束。
#include<stdio.h>

int main()
{
    printf("[1] apple\n[2] pear\n[3] orange\n[4] grape\n[0] exit\n");/*首先输出水果种类及序号*/

    int i,num;

    for(i=0;i<=4;i++){    /*for循环*/

        scanf("%d",&num);

        if(num==0){
            break;
        }

        switch(num){    /*switch语句*/

            case 1:printf("price = 3.00\n");
            break;

            case 2:printf("price = 2.50\n");
            break;

            case 3:printf("price = 4.10\n");
            break;

            case 4:printf("price = 10.20\n");
            break;

            default:printf("price = 0.00\n");
            break;
        }
    }

    return 0;
}

Problem: Not understanding "the user can enter numbers 1 to 4 correspond to the query unit price of fruit when continuous queries over five times, the program should automatically exit query; less than five times the user input 0 to exit; enter another number, price display. 0. "this sentence means

Solution: carefully examine the topic, and with the resolve to complete online.

3.7-1 seeking odd and (15 minutes)

本题要求计算给定的一系列正整数中奇数的和。
输入格式:
输入在一行中给出一系列正整数,其间以空格分隔。当读到零或负整数时,表示输入结束,该数字不要处理。
输出格式:
在一行中输出正整数序列中奇数的和。
输入样例:
8 7 4 3 70 5 6 101 -1
输出样例:
116
#include <stdio.h>
int main(){
    int sum=0, n=-1;
    while(1){    /*小技巧,while(1)永远为真*/ 
        if(n<=0){    /*小于零直接跳出循环*/ 
            break;
        }
        if(n%2==1){    /*判断奇数*/
            sum += n;    /*求奇数项和*/
        }
    }
    printf("%d\n", sum);

    return 0;
}

Question: I did not expect how continuous output a series of numbers.

Solution: Familiar while and conditional statements true or false.

Guess you like

Origin www.cnblogs.com/254916-cn/p/11666333.html