C language knowledge

A. C language knowledge Mind Mapping

II. Write your own understanding of the following syntax

1.If else:
If(语句1){
    语句2;
} else {
    语句3;
}
认识: 判断语句1,如果不成立的话做else后面语句3,成立的话就走语句2,可以有许多if else语句组合在一起,进行更复杂更多的判断,用这种语句要注意判断关系符号==<>等等,还有语句1是等式的话会有一个值,0或1,直接语句1为数字也是于是否等于0如果不等于就成立,等于就不成立,走else语句。

2.switch case:

Switch(ch){
   Case a:语句1;break;
   Default:语句;break;
}
认识:在switch()里得到一个字符或数字,在case里找到相应的地方执行语句,判断ch与a,成立就走后面语句,不成立就接下去找,分有break和无break的情况,有break就执行完语句就退出,如果执行语句后没有break,就接下去执行接下去不管case成不成立,执行语句一直到有break语句,没有break就一直到结束,还有default就是找不到对应的case就执行default的语句。提醒要注意是否有break,有和没有运行结果不一样。

3.for:

For(语句1;语句2;语句3){
}
认识:语句1进行需要的一开始变量赋值,执行一次,在接下去的循环当中就不会再用到了,如果不需要可以不写,但是后面的;号是一定要的,然后语句2是循环判断的条件每一次要进行循环要先进行语句2的判断,然后后面也要有;号,然后语句3是循环内我们写的语句做完后加在后面一定要进行的语句,通常是改变循环条件值得一个式子,后面就不用;号了,通常错误是有时会写错;或没写,通常是知道循环次数会使用for循环,比较好用。

5.While:

While(语句){
    复合语句;
} 
认识:语句里写的是判断条件,成立就执行大括号内的复合语句,不成立就退出while循环,还是一样要注意判断关系符,在后面的大括号里要写出可以改变判断值,使循环条件是在改变的,不然会一直循环还不知道怎么了,当遇到这种情况时,会没有答案输出,这时候就可以想到有可能是循环没结束。

6.do while:

Do{
   复合语句;
}while();
认识: 这个循环是不管怎么样都会进去循环一次,然后再进去while后面()的循环条件进行判断,注意while()后是有一个;符号的,这种循环就是比其他的循环来说,会不管怎么样的进行一次循环,需要特殊使用。

7.Break和continue:

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

}

for(语句1; 语句2; 语句3)
{
    语句1;
     continue;
    语句2;
        
}
认识:在通常使用在循环当中,在你某个条件后,在条件后加一个break他就会跳出整个循环体,不会再进行后面多余的循环,使得效率提高,是个很好用的词,在switch中,也是一样,找到对应的语句执行完,遇到break,就跳出switch;continue,当在普通代码顺序是,在语句1后面时,当做到在它之前语句完后,执行continue,会跳过语句2,做下一个语句,如果是在for循环当中,就会跳过语句2如果还有语句也会一直跳过,直接进行下一次循环。在for循环内,会进行语句3后,在判断循环条件,进行下一次循环。

Three. Pta fraction Screenshot

Four. Pta subject analysis

Topic one:

First: 1 see this topic first see the title will know it's a cycle of problems, and find the first n items. 2. To enter a variable to see to know the format of input conditions. 3. know to see output format output standard and precision behind, we know to use float or double the number of bits required to have a defined, will think of how to express.
Finally: then use their own knowledge to analyze how to do this subject. I am thinking of adding to the front from the back, with n input variables, with a variable m = n * 2-1, and when n is two is the value of m = 2, so that bit n can know the denominator, then the denominator minus 2, up to 1, a first cycle, with variable double the sum calculated values, because the output to double, so I can start the other variables defined as double. Here is my code. (By reference)

#include <stdio.h>

int main()
{
    double n,m;
    double sum=0;
    
    scanf("%lf",&n);
    
    m=2*n-1;
    
    while (m>0) 
    {
        s+=1/m;
        m-=2;   
    } 

    printf("sum = %.6f",s);
    
    return 0;
}

Topic two:

First of all: to see the topics mentioned three integers 1, it will be three int variables and then compared the size, my mind would think of if else to compare. 2. then go to the input format and output format output needed -> this symbol is to remember, and use is a simple three values than the size of the if else.
Finally: analysis of their own how to write code to compare a and b, big words in comparison b and c, big words, it is cba, and then format output, small words would no longer determine the a and c, big words bca, small the word is bac, another is true enough. Here is my code. (By reference)

#include <stdio.h>
int main()
{
    int a;
    int b;
    int c;
    
    scanf ("%d %d %d",&a,&b,&c);
    
if (a>b){
    if(b>c){
        printf("%d->%d->%d",c,b,a);
    }else {
        if(a>c){
        printf("%d->%d->%d",b,c,a);
        }else {
        printf("%d->%d->%d",b,a,c);
        }
    }
}else {
    if(a>c){
        printf("%d->%d->%d",c,a,b);
    }else {
        if(b>c){
        printf("%d->%d->%d",a,c,b);
        }else {
        printf("%d->%d->%d",a,b,c);
        }
    }   
}
    return 0;   
}

Topic three:

First of all: 1. look at the subject and seeking odd, then think of how to determine whether it is an odd number. 2. Look at the input format, and 0 to negative end of write cycles it is determined whether the read data is odd and judges, and add. 3. Look output format, no points, and then you think well how to write in my mind.
Finally: in my mind like a good how to write. 1. Define a variable used to read the data one by one, and the odd sum to count, to define a start sum = 0, otherwise the sum will be indeterminate value assigned system will not answer. 2. The data is then read into a first and into the circulation is determined whether 0 or negative, it is determined whether or not an odd number, it is added to the sum, not on a read data into, determination again, the final result obtained. Here is my code. (By reference)

#include <stdio.h>
int main()
{
    int a;
    int sum=0;

    scanf ("%d",&a);

    while (a>0){
        if (a%2>0){
            sum+=a;
        }
        scanf ("%d",&a);        
    }

    printf ("%d",sum);
    
    return 0;
        
}

Guess you like

Origin www.cnblogs.com/jiuweihong/p/11657092.html