20 Big data 1 The seventh classroom test of the class

The subject of this class test is more comprehensive , And it's a little difficult , So I hope the students can understand

1 Even numbers and prime numbers

Examination site ： How to judge a number to be prime
Ideas ： Using cycle and judgment to find two prime numbers that meet the conditions

Subject requirements 8

3 5

Complete code

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

int n,i,j,k;
scanf("%d",&n);
for(i=2; i<n; i++)// Use circulation from 2 Start judging whether they are prime numbers in turn
{

int x1=sqrt(i);// Judge i Prime or not
for(j=2; j<=x1; j++)
{

if(i%j==0)
break;
}
if(j>x1)// Find prime i after , Judge n-i Is it also a prime number
{

int x2=sqrt(n-i);
for(k=2; k<=x2; k++)
{

if((n-i)%k==0)
break;
}
if(k>x2)// If they are all prime numbers , Print these two primes
{

printf("%d %d",i,n-i);
return 0;
}
}
}
}

2 The greatest common divisor of multiple numbers

Examination site ： Judge whether a number is a common divisor
Ideas ： Because there are many numbers , So using arrays to store numbers , And then use the cycle and judgment conditions to find the greatest common divisor

Subject requirements 3
12 18 42

sample output

6

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

int n,i,j,a,flag=0;
scanf("%d",&n);
for(i=0; i<n; i++)
scanf("%d",&a[i]);// Using arrays to store numbers
int min=a;
for(i=1; i<n; i++)
{

if(a[i]<=min)
min=a[i];
}// Find the smallest of these numbers
for(i=min; i>=1; i--)// Decrease gradually from the minimum value , See if it's a common factor
{

for(j=0; j<n; j++)
{

if(a[j]%i!=0)// If there is a number that cannot be divisible i, be i It's not a common factor
{

flag=0;
break;
}
else if(a[j]%i==0)
flag=1;
}
if(flag)// if flag=1, Indicates that the greatest common factor has been found , Print and jump out of the loop
{

printf("%d",i);
break;
}
}
}

3 Z Type number matrix （ Correct the wrong questions ）

This problem requires students to base on the existing code , Clarify the cyclic conditions and the relationship between the number of rows and columns of a matrix , It's more difficult , Just try to understand , The answer is not to be explained , Please study by yourself , That's what makes the impression

Subject requirements 3

1 2 6
3 5 7
4 8 9

The corrected code

\$block1\$
if(m<n&&m%2==0)
x=m-j;
else if(m<n&&m%2!=0)
x=j;
else if(m>=n&&m%2==0)
x=m-i-j;
else if(m>=n&&m%2!=0)
x=m+j-n+1;
y=m-x;
a[x][y]=count++;
\$end1\$

Complete code

#include "stdio.h"
#include "math.h"
#include "stdlib.h"
int main()
{

int a, n, i, j, m=0, x, y, temp, count=1;
scanf("%d", &n);
for(i=-(n-1);i<=n-1;i++)
{

for(j=0;j<n-abs(i);j++)
{

/*******************Error**********************
if(m<n)
x=m-j;
else
x=n-1-j;
y=m-x;
a[x][y]=count++;
********************Error*********************/
// No explanation , Please understand by yourself
if(m<n&&m%2==0)
x=m-j;
else if(m<n&&m%2!=0)
x=j;
else if(m>=n&&m%2==0)
x=m-i-j;
else if(m>=n&&m%2!=0)
x=m+j-n+1;
y=m-x;
a[x][y]=count++;
}
m++;
}
for(i=0; i<n; i++)
{

for(j=0; j<n; j++)
printf("%3d", a[i][j]);
printf("\n");
}
}

4 The whole arrangement of numbers

The problem is very difficult , Summary method , The most important sort out ideas .

Subject requirements 3

1,2,3
1,3,2
2,1,3
2,3,1
3,1,2
3,2,1

Complete code

#include <stdio.h>
#define max 10
void sort(int a[],int n,int s,int r[],int m);// Define function first
int main()
{

int n,a[max],i;
int r[max];
scanf("%d",&n);
for(i=0; i<n; i++)
a[i]=i+1;
sort(a,n,0,r,n);
return 0;
}
void sort(int a[],int n,int s,int r[],int m)    // among m The number of arranged numbers , The whole recursive process does not change ;
{

// In fact, the condition is s yes r Index of the array ,r[s] Store the element that recursion takes out each time ;
int i,j;  // Start defining... In this line l=0; error ; That leads to the first one below for The loop retains the first value in the second loop , It must be cleared every time ;
int flag=0;
int b[max];
for(i=0; i<n; i++)    // This loop takes the second... In the array i Elements in the array r in ;
{

flag=1;     // When determining whether the range of the function reaches the output ;
r[s]=a[i];
int l=0;
for(j=0; j<n; j++)  // The purpose of this loop is to put the remaining elements into a new array , It is convenient to use recursion next time ;
{

if(i!=j)
{

b[l]=a[j];
l++;
}
}
sort(b,n-1,s+1,r,m);
}
if(flag==0)
{

int k;
for(k=0; k<m; k++)
{

if(k!=m-1)
printf("%d,",r[k]);
else
printf("%d",r[k]);
}
printf("\n");
}
}

5 Triangle rotation （ Correct the wrong questions ）

How to rotate a two-dimensional array clockwise
1. Elements in symmetric positions along the main diagonal are exchanged
2. For one-dimensional arrays , Put each line on i Elements and N-i-1 Exchange elements
A way to rotate a two-dimensional array counterclockwise
1. Elements in symmetric positions along the main diagonal are exchanged
2. Place each column in the i Elements and N-i-1 Exchange elements

Subject requirements 3
1
23
456
90

421
53
6

The modified code

\$block1\$
for(i = 0; i < n; i++)// First the a The array is copied to b
for(j = 0; j < n; j++)
b[i][j]=a[i][j];
for(i = 0; i < n; i++)// take b The elements of the array are swapped along the main diagonal
for(j = 0; j < i; j++)
{

t = b[i][j];
b[i][j] = b[j][i];
b[j][i] = t;
}
for(i=0; i<n; i++)// take b Every row of the array has i Elements and N-i-1 Exchange elements
for(j = n - 1; j >= n/ 2; j--)
{

t=b[i][j];
b[i][j]=b[i][n-1-j];
b[i][n-1-j]=t;
}
\$end1\$
\$block2\$
for(j=0; j<n; j++)
{

if(a[i][j]==0)// Determine if you need to print spaces
printf(" ");
else
printf("%c",a[i][j]);
}
\$end2\$

Complete code

#include <stdio.h>
#include <stdlib.h>

char a= {

0};
char b= {

0};

int main()
{

int i,j,n,w,t;
scanf("%d", &n);
for(i=0; i<n; i++)
scanf("%s", a[i]);
scanf("%d", &w);
w=(w%360)/90;
for(t=0; t<w; t++)
{

for(i = 0; i < n; i++)
for(j = 0; j < n; j++)
b[i][j]=a[i][j];
for(i = 0; i < n; i++)
for(j = 0; j < i; j++)
{

t = b[i][j];
b[i][j] = b[j][i];
b[j][i] = t;
}
for(i=0; i<n; i++)
for(j = n - 1; j >= n/ 2; j--)
{

t=b[i][j];
b[i][j]=b[i][n-1-j];
b[i][n-1-j]=t;
}
for(i = 0; i < n; i++)
for(j = 0; j < n; j++)
a[i][j] = b[i][j];
}
for(i=0; i<n; i++)
{

for(j=0; j<n; j++)
{

if(a[i][j]==0)
printf(" ");
else
printf("%c",a[i][j]);
}
printf("\n");
}
return 0;
}

6 Comprehensive experiment : Student achievement management

difficulty ：
1、 There's a lot of data , The data is complex
2、 The overall information of students should be sorted according to their grades
Their thinking ：
1、 See this type of topic , You should think of using structures to do , Because we have to treat a student as a whole , Sort , Although the two-dimensional array can also achieve this process , But it's not as natural as the structure .
2. After determining the structure , And then sort out the whole process , First, read in the data , Then calculate the total score of each student and the average score of each subject , Then rank the students according to the total score , Finally output data

Subject requirements 5 3
90 70 75
89 67 78
87 65 98
97 86 77
88 96 82

88 96 82
97 86 77
87 65 98
90 70 75
89 67 78
90.2 76.8 82

Complete code

#include <stdio.h>
#include <stdlib.h>

struct student
{

int screo;
};
typedef struct student DEG;

int main()
{

DEG stu,t;
int i,j,n,m,sum,t1;
double ave;
scanf("%d%d",&n,&m);
for (i=0;i<n;i++)
{

for(j=0;j<m;j++)                    // Read in the data
{

scanf("%d",&stu[i].screo[j]);   // Pay attention to the use of structures
sum[i]+=stu[i].screo[j];
}
}
for (i=0;i<m;i++)                       // Be careful m,n Use
{

for (j=0;j<n;j++)
{

ave[i]+=stu[j].screo[i];
}
ave[i]/=n;                          // Work out the average
}

for (i=0;i<n-1;i++)                     // Bubble sort
{

for (j=0;j<n-1-i;j++)
{

if (sum[j]<sum[j+1])            // Exchange students' information and total scores at the same time
{

t=stu[j];
stu[j]=stu[j+1];
stu[j+1]=t;

t1=sum[j];
sum[j]=sum[j+1];
sum[j+1]=t1;
}
}
}
for (i=0;i<n;i++)                       // Output data
{

for (j=0;j<m;j++)
{

printf("%d ",stu[i].screo[j]);
}
printf("\n");
}
for (i=0;i<m;i++)
printf("%.1lf ",ave[i]);
return 0;
}

7 Zero in the factorial

The topic of this class is , You need to know the rules to do it
Here is a blog about how to calculate the number of zeros after factorial
https://blog.csdn.net/Z_sea/article/details/80160098
Students can go to study

Subject requirements 6
3
60
100
1024
23456
8735373

0
14
24
253
5861
2183837

Complete code

#include <stdio.h>
#include <stdlib.h>
int F(int x)
{

int j,y,count=0;
for(y=1;y<=x;y++)
{

for(j=5;j<=y;j=j*5)
{

if(y%j==0)
count++;
}
}
return count;
}
int main()
{

int a,i;
scanf("%d",&a);
int b[a];
for(i=0;i<a;i++)
scanf("%d",&b[i]);
for(i=0;i<a;i++)
printf("%d\n",F(b[i]));
return 0;
}

8 Lucky numbers

Ideas ： Start from the bottom to judge whether each person is 4 perhaps 8, If it is 8, Then continue to judge the next one , If it is 4, You don't have to judge anymore , Jump straight out of the loop

Subject requirements Complete code

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

int i,flag1=0,flag2=1,a,t;//flag1 To determine if there is 8,flag2 Judge whether it exists 4
for(i=8;i<=998;i++)
{

flag1=0;
flag2=1;
t=i;
while(t!=0)
{

a=t%10;
if(a==8)
flag1=1;
if(a==4)
{

flag2=0;// If exist 4,flag2=0, And out of the loop
break;
}
t=t/10;
}
if(flag1==1&&flag2==1)
printf("%d\n",i);
}

}

The questions of the test list are relatively simple , It's all basic knowledge in books , As long as you understand the structure of the linked list , You can do it . But maybe most of the students haven't understood what the linked list is , To understand first , Use it again , Practice makes perfect .
To understand linked lists, you can refer to and understand arrays , The difference is
1、 storage ： An array is a continuous space in memory , And linked lists are stored in the form of nodes .
2、 Read mode ： Array subscript flexibly read variables in the array , But the linked list can only read the content of the node in one direction .3、 How it was created ： Creating an array just requires “ Array name 【】” In the form of , But to create a linked list, you need to define a structure , Create a function , One by one, it will open up memory space and read data into . But the advantage of linked lists is that they can store a number of quantities and types of data , And it can save memory space .
That's what linked lists are all about , If it's not easy to understand , Just type more code , Compare the examples in the textbook , Imitate first , Repeat it a few times , Only a few operations are nothing more than adding, deleting, checking and modifying .“ dedicated , Only hand ripe ”.

9 [ Completion ] Insert at the specified position of the linked list node

1、 After seeing the topic , It's a fill in question , This topic is very friendly , Creation of linked list 、 The output has been programmed for us , Look at the main function , The process is ： Create a linked list —— Print created list —— Enter the node information to be inserted 、 Insertion position —— Insert node —— Print a new linked list .
2、 All we have to do is insert nodes , The train of thought is ： Traverse the list to the insertion position n, The node to be inserted points to the original n The location of the node ,n The node before the position points to the node to be inserted

Subject requirements

The following procedure , First create a linked list , Then create a new node stu, And call putinto function , Insert the new node into the second m Before the node （ Be careful , If m<=1 Insert in the header ;m> The number of nodes in a linked list indicates that it is inserted at the end of the list ）, but putinto Hang in the air , Please complete .

3
1
98
7
99
5
87
6
78
2

1      98
7      99
5      87
1      98
6      78
7      99
5      87

Complete code

#include "stdio.h"
#include "malloc.h"
#define LEN sizeof(struct student)

struct student
{

long num;
int score;
struct student *next;
};

struct student *creat(int n)
{

int i;
for(i=1;i<=n;i++)
{

p1=(struct student *)malloc(LEN);
scanf("%ld",&p1->num);
scanf("%d",&p1->score);
p1->next=NULL;
else p2->next=p1;
p2=p1;
}
}

{

struct student *p;
while(p!=NULL)
{

printf("%8ld%8d",p->num,p->score);
p=p->next;
printf("\n");
}
}
// Where the title is required to be filled in
struct student *putinto (struct student *head, struct student *stu , int n)
{

struct student *p;
int i;
for (i=1;i<(n-1)&&p!=NULL;i++)			// until n The previous node of
{

p=p->next;
}
stu->next=p->next;
p->next=stu;
}
//
main()
{

int n, m;
scanf("%d",&n);
stu=(struct student *)malloc(LEN);
scanf("%ld",&stu->num);
scanf("%d",&stu->score);
stu->next = NULL;
scanf("%d", &m);
}

This program cannot be inserted into the first node , however OJ The system went through , The topic does not consider the case of inserting the first node .

10 [ Fill in the blanks ] Deletion of linked list nodes

Subject requirements ： Find the node and delete
Their thinking ：
1、 The topic is more friendly , Same as the last question , The main part has been given , Just write your own function to delete the node .
2、 Look at the main function , Try to sort it out by yourself .
Delete node ideas ： Let the previous node of the node to be deleted point to the next node of the node to be deleted , But consider deleting the first node .

3
1
98
4
99
5
87
4

1       98
4       99
5       87
1       98
5       87

Complete code

#include "stdio.h"
#define LEN sizeof(struct student)

struct student
{

long num;
int score;
struct student *next;
};

struct student *creat(int n)
{

int i;
for(i=1;i<=n;i++)
{

p1=(struct student *)malloc(LEN);
scanf("%ld",&p1->num);
scanf("%d",&p1->score);
p1->next=NULL;
else p2->next=p1;
p2=p1;
}
}

{

struct student *p;
while(p!=NULL)
{

printf("%8ld%8d",p->num,p->score);
p=p->next;
printf("\n");
}
}
// Where the title is required to be filled in
struct student *del(struct student *head,long num)
{

struct student *p,*pre;
while (p!=NULL&&p->num!=num) // Note that the criterion here is p->!=num, That is to say p->num=num Stop at
{

pre=p;                  // Record the previous node
p=p->next;
}
if (p==NULL)                // Exclude the case that the linked list is empty
{

}
else if (p==head)           // When the node to be deleted is the first one
{

}
else
{

pre->next=p->next;      // The node before the node to be deleted points to the node after the node to be deleted
}
free(p);                    // Release space
}
//
main()
{

struct student *creat(int n);
struct student *delete(struct student *head,long num);
int n;
long del_num;
scanf("%d",&n);
scanf("%ld",&del_num);