# Second assignment

2020-11-08 10:05:54

# 0. Exhibition PTA Total score  # 1. Summary of this chapter

## 1.1 Summary of this chapter and 1.2 Summary of programming skills

1. Variable name ： Little hump nomenclature example ：aB
Function name ： The name of the great hump example ：Ab
2. c The identifier in a language consists of letters Numbers Underline composition , The first character must be Letters or underscores .
3. int（ integer ） char( Character ） float( Single precision floating point ） double( Double precision floating point ）typedef( Custom type ）
4. / written words / Comment lines
//： Note a single line
5. （1） Two integers are divided , The result of the operation is an integer .
example ：10/4=2 1/3=0
（2） The remainder
example ：5%6=5 9%4=1 100%4=0（ It means that it can be divisible ）
（3） To convert to floating point , You need to convert one of the numbers into floating-point numbers
example ：10.0/4 10.0/4.0
6. （1） Format input function scanf()
int Type data %d float Type data %f doouble Type data %lf
example ：scanf("%lf",&x); （ Pay attention to the type of data entered , Control the character with the corresponding format ,& Don't miss it. ）
（2） Format output function printf()
int Type data %d fioat and double Type data are available %f,double The model can also be used %lf
%nf Indicates that the output data is reserved after the decimal point n position
%f The decimal point is reserved by default 6 position
example ：printf("%d %f %.3f",a,b,c);
7. Generate random number
example ：
``````#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int number;             // A variable that defines a random number
srand(time(0));         // Produce seeds
number = rand() % 100;  // The range of generating random numbers is 0-100
printf("%d", number);   // Output random number
return 0;
}
``````
If the range of random numbers generated is 10-100, The code for the scope is changed to ：number=10+rand()%90;
8. Relational operator
== equal ！= Unequal
9. Mathematical functions
Call I / O function , To add #include <stdio.h> Call mathematical function , To add #include <math.h>
Square root function ： sqrt(x) example ： sqrt(4.0)=2.0
Absolute value function ： fabs(x) example ： fabs(-5.0)=5.0
power function ： pow(x,n) example ： pow(4,2)=16
Exponential function exp(x) example ： exp(2.3) The value of is e Of 2.3 Power
10. Control the number of outputs
%md If the number of digits is less than m, Will automatically fill in the space from the left
%-md If the number of digits is less than m, Will automatically fill in the space from the right
%m.nf m Including the sign place and the decimal point ,n Express reservation n Decimal place
example ：
``````#include <stdio.h>
int main()
{
int number1,number2;
double number3;
scanf("%d %d %lf", &number1,&number2,&number3);
printf("%5d%-5d%5.3f", number1,number2,number3);
return 0;
}
``````
When the input 30 50 5.5 when , The output is ： Three spaces +3050 Three spaces +5.500
11. Make the tail without spaces
example ：
``````#include <stdio.h>
int main()
{
int n = 3;
int i;
int flag = 1;
int number;
int a;
for (i = 1; i <= 3; i++)
{
scanf_s("%d", &a);
number = a;
if (flag == 1)                    // Find the first number , And change the output mode of the following numbers
{
flag = 0;
printf("%d", number);
}
else
{
printf("a%d", number);
}
}
return 0;
}
``````
When the input 30 and 40 and 50 when , The output is 30a40a50, Can realize tail without space
12. Character data
The data of character type variable is used char To define , And add ''
Commonly used decimal character type constant ASCII code ：'a':97,'A':65,'0':48
Character input and output functions ：
（1）getchar(): Only one character can be read in ,（） There are no parameters in it
（2）putchar(): Output a character , Format ：putchar( Output parameters ）
example ：
``````#include <stdio.h>
int main()
{
char ch;
ch = 97;
printf("%c %d", ch, ch);
return 0;
}
``````
The output is ：ch 97
13. Logical operations
！： Not ,&&： And ,||： or
&& and || Has a lower priority than the relational operator
14. Flexible use switch Statement break
example ： Show part of the code
``````case n: case m: case f: ......case z:
expression ;
break;
``````
above case Common expressions
15. Pseudo code descriptors
example ：
(1）while Conditions
expression
end while
(2) if Conditions
expression
end if
16. n-- And --n A kind of distinction
example ：
``````#include <stdio.h>
int main()
{
int n;
n = 8;
printf("%d ", n--);
}
``````
The output is 8
``````#include <stdio.h>
int main()
{
int n;
n = 8;
printf("%d ", --n);
}
``````
The output is 7
17. Take a number from a certain bit
example ：
number It means a number ,n A number in one of its bits
bits ：n=number%10
ten ：n=number/10%10
Hundred bit ：n=number/100%10
And so on
18. Input 3 The number of outputs is maximum or minimum or middle or arranged in order of size
Example ： The code you started with ：
``````#include <stdio.h>

int main()
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
if(a>b)
{
if(b>c)
{
printf("x=%d,y=%d,z=%d",c,b,a);
}
else if(c>a)
{
printf("x=%d,y=%d,z=%d",b,a,c);
}else
{
printf("x=%d,y=%d,z=%d",b,c,a);
}
}else
{
if(c>b)
{
printf("x=%d,y=%d,z=%d",a,b,c);
}
else if(a>c)
{
printf("x=%d,y=%d,z=%d",c,a,b);
}else
{
printf("x=%d,y=%d,z=%d",a,c,b);
}
}
return 0;
}
``````
What happened ： A lot of code , And the idea is complicated , It's not good for getting things done quickly .
Learn to refer to the code replaced by intermediate quantity ：
``````#include <stdio.h>
int main()
{
int number1,number2,number3, temp;
scanf("%d %d %d", &number1, &number2, &number3);
if (number1 > number2)
{
temp = number1;
number1 = number2;
number2 = temp;
}
if (number1 > number3)
{
temp = number1;
number1 = number3;
number3 = temp;
}
if (number2 > number3)
{
temp = number2;
number2 = number3;
number3 = temp;
}
printf("x=%d,y=%d,z=%d", number1, number2, number3);
return 0;
}
``````
Easy to operate , And the idea is clear .
19. Calculation 1-1/3+1/5-... A kind of question type （ introduce flag)
Example ： ``````#include <stdio.h>

int main()
{
int i;
int n;
int flag;
double sum;
scanf("%d", &n);
flag = 1;
sum = 0;
for (i = 1; i <= n; i++)
{
sum = sum + flag*i * 1.0 / (2 * i - 1);
flag = -flag;
}
printf("%.3f", sum);
return 0;
}
``````
20. Count the number of digits of an integer
example ：
``````#include <stdio.h>

int main()
{
int count, number;
count = 0;
scanf_s("%d",&number);
while (number != 0)
{
count++;
number = number / 10;
}
printf("%d", count);
return 0;
}
``````

## 1.3 Learning experience of this chapter

Through these two weeks of study and review c Language related content , Because during the summer vacation, I have a little understanding of the previous content , So I can keep up with the class content , There is no problem with the structure and content of those statements , But some of the sentences are not very well understood , The basic reason is that there is no clear understanding of the basic content of the sentence , I don't know enough about the details , Including the preview of the textbook is not complete . After that, the class progress is a little fast ⊙ω⊙, Just after the preview, we should preview the next section immediately , A little too late to consolidate their own foundation , In the last two weeks, we have written the branch structure of the topic, and have written the experimental problem set published in class , And preview some topics in the courseware .

# 2.PTA Experiment assignment

## 2.1 The day before yesterday

### 2.1.1 Data processing

Variable ：

• year（ year ）( integer ）： The day before yesterday, it might be another year
• month（ month ）（ integer ）： The day before yesterday may involve a month change
• day( Days ）（ integer ）： It must involve the change of days
• Febary( The number of days in February ）： There are different February days between leap year and non leap year
• mark（ month /1）（ integer ）： Need to use switch sentence

### 2.1.2 Code screenshot

A little longer , Separate cut graphs    ### 2.1.3 PTA Submit list and description Q1: Looking for a long time exactly where programming errors or missing some letters and symbols
A1: The results are moving , Turned out to be case There's no number after it , Add the expression directly .....

## Height difference of the best couple ### 2.2.1 Data processing

Variable ：

• sex（ Gender ）( Character ）： Determine gender
• n( The number of ）( plastic ）： Judge how many people
• i( plastic ): Loop statements use
• height( height ）( integer ）： Subject requirements
• number( Suitable for height ）( integer ）： The optimum height after conversion

### 2.2.2 Code screenshot ### 2.2.3 PTA Submit list and description The teacher has explained the input related question in class and typed the code , When I write by myself, I first write in vs Debug and correct, so the commit is correct

## Step by step debugging ( Tortoise and the hare ) ### 2.3.1 One step debug screenshot ### 2.3.2 Code screenshot  ### 2.3.3 PTA Submit list and description Q1： Only partially correct when submitted , Several parts of the judgment are missing
A1: Forget to judge whether the rabbit runs faster than the tortoise , Split the time directly every ten minutes , This leads to a partial lack of judgment .