Purple Book - Chapter 1 Introduction to Programming

The first chapter, program design entry

1.1 arithmetic expression

1, 2 + 1 calculated output

int main()
{
    printf("%d\n",1+2)
    return 0;
}

2, calculate the output 8/5, to one decimal place

#include<stdio.h>
int main()
{
    printf("%lf\n",8.0/5.0);
    return 0;
}
  • Output integer value with% d,% f with floating output

Integer / Integer = integers, floats / float float =

3, complex arithmetic header files

#include<stdio.h>
#include<math.h>
int main()

1.2 input variables and their

  • Scanf correspondence data type and the placeholder variable, and the variable is incremented before each "&" symbol

  • storing an integer value int, double double precision floating point value is stored

  • Do not use headers conio.h, including getch (), clrscr () function, etc.

  • Format requirements: ① Each output carriage return line (including the last row)

    ② start of each line without spaces

    ③ every two or string, and outputs separated by a single space

    Example: input and high cylinder bottom radius r h, the output of cylinder surface area

    #include<stdio.h>
    #include<math.h>
    int main()
    {
        const double pi=acos(-1.0);
        double r,h,s1,s2,s;
        scanf("%lf%lf",&r,&h);
        s1=pirr;
        s2=2pir*h;
        s=s1*2.0+s2;
        printf("Area=%.3f\n",s)
        return 0;
    }

    How to understand s1 = pi * r * r?

    Assignment: Assignment is action, first calculate the value on the right, and then assigned to the variable on the left, covering its original value

1.3 Design of the sequence structure of the program

1, three-digit number, separated one hundred ten bits, the inverted output

Analysis: one hundred equal to n / 100 (commercially herein take the integer part)

Ten equal to n / 10% 1 (% of remainder)

Bits equal to n% 10

(This question has a fuzzy detail, i.e., bits 0, for example input 520, output 025 or 25?)

#include<stdio.h>
int main()
{    
    int n;
    scanf("%d",&n);
    printf("%d%d%d\n",n%10,n/10%10,n/100);
    return 0;
}

More than 025 output

#include<stdio.h>
int main()
{
    int n,m;
    scanf("%d",&n);
    m=(n%d*100+(n/10%10)*10+(n/100))
    printf("%d\n",m);
    return 0;
}

Output more than 25

2, the input two integers a and B, the exchange value of the two, and then output

#include<stdio.h>
int main()
{
    int a,b,t;
    scanf("%d%d",&a,&b);
    t=a;
    a=b;
    b=t;
    printf("%d %d\n",a,b);
    return 0;
}

1.4 branch structure programming

1, the known total number of chickens and rabbits is n, the total number of legs is m. The number of inputs n, and rabbit and m, are sequentially output chicken, without outputting No answer solution

Analysis: Chicken provided a rabbit b, then a + b = n, 2a + 4b = m, solve for a = (4n-m) / 2, b = na

To this problem has a solution, i.e. a, b are non-negative integers

#include<stdio.h>
int main()
{
    int a,b,n,m;
    scanf("%d%d",&n,&m);
    a=(4*n-m)/2;
    b=n-a;
    if(m%2==1||a<0||b<0)
        printf("No answer\n");
    else
        printf("%d %d\n",a,b);
    return 0;
}

2, 3 integer input, the output from small to large

The a, b, c of the three variables into a ≤ b ≤ c form, if a> b, then the switch and a b, c and next checks a, b and c, final inspection

#include<stdio.h>
int main()
{
    int a,b,c,t;
    scanf("%d%d%d",&a,&b,&c);
    if(a>b){t=a;a=b;b=t;}//执行完毕后a≤b
    if(a>c){t=a;a=c;c=t;}//执行完毕后a≤c,且a≤b仍成立
    if(b>c){t=b;b=c;c=t;}
    printf("%d %d %d\n",a,b,c);
    return 0;
}

1.5 Notes and exercises

Guess you like

Origin www.cnblogs.com/mandala/p/11536504.html