Jiangsu Province C language level II preparation (8 / 20)

catalogue

Theoretical questions

1. Modern information technology

2.Power processor: used for supercomputers, servers and workstations

three   8-bit binary representation of the integer range

Non theoretical questions

1. The loop body of the do while statement will be executed at least once

2. The strcat function (s1, s2) combines two char characters s1+s2

Code question

1. Correction question: several classic mistakes (simple but easy to make mistakes)

2. Write it yourself: simulate Joseph Ring problem

Theoretical questions

1. Modern information technology

withElectronic technology, especially microelectronic technologyBased on
withDigital technology (computer)As the core
withcommunications technologyAs a pillar

   China is vigorously promoting the synchronous development of new industrialization, informatization, urbanization and agricultural modernization

2.Power processor: used for supercomputers, servers and workstations

CPU series products

Power processor

Intel processor
ARM processor
AMD processors

three   8-bit binary representation of the integer range

Signed

-128  ,127

Unsigned0  ,255

Non theoretical questions

1. The loop body of the do while statement will be executed at least once

Because it executes the loop body first, and then judges the condition

Some judgments on circular body
The loop body of a do while statement is executed at least once
The loop body of the while statement is executed at least once×
The loop body of the for statement is executed at least once×
Can only be implemented with do, do while, or for statements×

2. The strcat function (s1, s2) combines two char characters s1+s2

Code question

1. Correction question: several classic mistakes (simple but easy to make mistakes)

[program function]
Rearrange the characters in a string composed of only numbers and lowercase letters from left to right according to the following rules: lowercase letters are placed to the right from the left end of the string, and numbers are placed to the left from the right end of the string.
[test data and operation results]
Test data: "g1bc3d24fa"
Output: gbcdfa4231

#include <string.h>
#include <stdio.h>
#include<conio.h>
void adjust(char *str)
{
    int i,j,k,len;
    char c;
    len=strlen(str);
    i=0; j=0;
    for(i<len-j)       
    {
        c=str[i];
        if('a'<=c<='z')        
            i++;
        else
        {
            for(k=i+1;k<len-j; k++)  //If you have j numbers, you don't have to worry about J
                str[k-1]=str[k];
            str[len-1]=c;        //Achieve the effect of reverse order of numbers
            j++;
        }
    }
}
int main()
{
    char str[20]="g1bc3d24fa";
    adjust(str[20]);   
    puts(str);
    getch();
    return 0;
}

These questions are classic

[1] i=0 and i + + are written before and after the: for (; i < len-j;) loop, so expression 1 and expression 3 can be hidden, but expression 2 can never be omitted

It can also be changed to while (I < len-j)

[2]: 'a' <= c && c <= 'z'

In order to meet the conditions, the code in the middle wants to   Put the character element forward and move the number element one bit later to complete the function of the topic

j is the number of statistical elements,

[3]: str[len-(j+1)] = c  

[4] : str it can be seen from the function called that str is an array, so you don't need brackets and numbers in adjust. After all, you don't want elements

2. Write it yourself: simulate Joseph Ring problem

[programming requirements]

  • Write the void joseph(int a[],int b[],int n,int m) function.

The first n elements of the array pointed to by a store the numbers of n people (1,2..., n). N people sit in a circle, start counting from the person with number 1 (from 1 to m), and those who check in M are listed. The numbers of each listed person are saved in the array pointed to by b until the last person is listed

  • Write the main function.

Declare two one-dimensional arrays and initialize one of them with {1,2,3,4,5,6,7,8,9,10}. Declare two variables n and m and initialize with the test data. Call the joseph function with two arrays and two variables as arguments. Output the numbers of the n listed persons obtained after the function processing to the display screen and the file myf2out. Finally, output the examinee's own admission card number to the file 2,out

 

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

void joseph(int a[],int b[],int n,int m)
{
	int i ,j,k = 0;//i: Subscript of array a; j: count the number of people in 1-m; k: subscript of array b 
	
	for(i = 0;i < n ;)
	{
		if( i == 0 && a[0] != -1 ) 
		{
			for(j = 0;j < m-1;j++)
			{
				i++;
				 if(i == n) i = 0;
				 if(a[i] == -1)  j--;//Let it cycle again
			}
		}	
		else
		{
			for(j = 0;j < m;j++)
			{
				i++;
			 	if(i == n) i = 0;
			 	if(a[i] == -1)  j--;//Let it cycle again 
			}
		}
		//Put the losers in array b
		b[k++]=a[i];
		a[i] = -1;
		if (k == n)break; 
	}
 } 
 
 int main()
 {
 	int n = 10,m = 3;
 	int s1[] = {1,2,3,4,5,6,7,8,9,10},s2[10];
 	joseph(s1,s2,n,m); 
 	
 	int i;
 	for(i = 0;i < 10;i++)
 	{
 		printf("%d ",s2[i]);
	 }
	 printf("\n");
	
	char testid[20];
	printf("Please enter the admission number:");
	gets(testid);

	FILE *fp;
	fp = fopen("myf2.out","w");
	
	for(i = 0;i < 10;i++)
 	{
 		fprintf(fp,"%d",s2[i]);
	 }
 	
 	for(i = 0;testid[i] != '\0';i++)
 	{
 		fprintf(fp,"%d",testid);
	 }
 	return 0;
 }

Keywords: C Algorithm

Added by nadeem14375 on Sat, 02 Oct 2021 04:12:43 +0300