Review of real questions for professional courses (2019)

1. Topic description:
Write a function for judging prime numbers, input an integer in the main function, and output whether it is a prime number.

#include<stdio.h>
int isprime(int n)
{
    
    
	int i;
	for(i=2;i<=n/2;i++)
	{
    
    
		if(n%i==0)
		{
    
    
			return 0;
		}
	}
	return 1;
}
int main(void)
{
    
    
	int number;
	printf("请输入一个整数:\n");
	scanf("%d",&number);
	if(isprime(number))
	{
    
    
		printf("该数是一个素数,"); 
	}else{
    
    
		printf("该数不是一个素数");
	}
	return 0;
}

2. Title description:
Write the function dayInMonth (int y, int m) method. The function of the function is to calculate the number of days contained in month m of year y.

#include<stdio.h>
int dayInMonth(int y,int m)
{
    
    
	int monthtab[12]={
    
    31,28,31,30,31,30,31,31,30,31,30,31};
	if((y%4==0)||(y%100!=0&&y%4==0))
	{
    
    
		if(m==2)
		{
    
    
			return 29;
		}
	}
	return monthtab[m-1];
 } 
 int main(void)
 {
    
    
 	int year,month;
 	printf("请分别输入年月:\n");
 	scanf("%d%d",&year,&month);
 	printf("包含的天数为%d",dayInMonth(year,month));
 	return 0;
 }

3. Title description:
13 people form a circle, and report numbers 1, 2, and 3 in order from the first person. Those who report to 3 will launch the circle. Find out the original serial number of the last person left in the circle. Requires processing with a linked list.

#include<stdio.h>
struct people
{
    
    
	int no;
	int next;
}peoples[13];
int main()
{
    
    
	int i,j,count;
	int number=13;
	for(i=0;i<13;i++)
	{
    
    
		peoples[i].no=i+1;
		peoples[i].next=i+1;
		if(i==12)
		{
    
    
			peoples[i].next=0;
		}
	 } 
	 i=0;
	 count=0;
	 while(number>1)
	 {
    
    
	 	printf("number=%d\n",number);
	 	
	 	if(peoples[i].no!=0)
	 	{
    
    
	 		count++;
	 		
	 		if(count==3)
	 		{
    
    
	 			peoples[i].no=0;
	 			number--;
	 			count=0;
			 }
			 
		 }
		 i=peoples[i].next;
	 }
	 for(i=0;i<13;i++)
	 {
    
    
	 	printf("%d",peoples[i].no);
	 	if(peoples[i].no!=0)
	 	{
    
    
	 		printf("最后剩下的人的序号为%d",peoples[i].no);
		 }
	 }
	 return 0;
}

Note: 1. What is given in the book is made of static linked list, and other places are dynamic linked list.
2. When instructing the last one, let him point to the head pointer again.

4. Title description:
Suppose that each line of text is read in the file data.txt at a time, and the character order of each line of text is reversed, that is, the character at the original k position will appear at the position of n-k+1, where n is the length of the text of the line (excluding the last carriage return, line feed or end of file), and finally save the text of each line with the reversed character order to the file result.txt.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void reverse(char *str)
{
    
    
	
	int end=strlen(str)-1;
	int start=0;
	char temp;
	while(start<=end)
	{
    
    
		temp=str[end];
		str[end]=str[start];
		str[start]=temp;
		start++;
		end--;
	}
}
int main(void)
{
    
    
	FILE *fd;
	FILE *fr;
	char arr[1000];
	int n;
	int len=0;
	if((fd=fopen("data.txt","r"))==NULL)
	{
    
    
		printf("can not open this file.");
		exit(0);
	}
	if((fr=fopen("result.txt","w"))==NULL)
	{
    
    
		printf("can not open this file.");
		exit(0);
	}
	while(!feof(fd))
	{
    
    
		fgets(arr,1000,fd);
		n=strlen(arr);
		if(arr[n-1]=='\n')
		{
    
    
			n-=1;
		}
		
	int end=n-1;
	int start=0;
	char temp;
	while(start<=end)
	{
    
    
		temp=arr[end];
		arr[end]=arr[start];
		arr[start]=temp;
		start++;
		end--;
	}
		fprintf(fr,"%s",arr);
	   
	}
	fclose(fd);
	fclose(fr);
	return 0;
}

Note:
1. The condition in the title is a number of lines, so the number of lines cannot be given directly, but the fscanf() function also regards the space as the sign of the end of the file. This thinking problem is given in the textbook.
2. Use the feof() function to determine whether it is the end of the file, read 1000 characters each time, and when encountering a carriage return, the length should be -1.
strlen includes the newline character, excluding the terminator.

Guess you like

Origin blog.csdn.net/qq_44867340/article/details/121876659