Final topic exercise of information C language programming (personal summary)

Keywords: C Back-end

1. Connect the circuit

Title Description:

As a student of department 2, Xiao Qiuyue is tired of connecting the circuit every day, and even the cat's ears and tail droop.

Now he is connecting a pile of resistors one by one. Please help him calculate the combined resistance of these resistors.

Input format:

Multiple groups of input, number of lines > = 1.

A number in the first line represents the resistance value of the first resistance. Next, two spaces separated numbers per line, opop and RR.

The value of opop is 0 or 1. 0 means to connect this resistance in series with the connected overall resistance, 1 means to connect in parallel, and RR is the resistance of this resistance.

The resistance of each resistor is a positive integer.

Output format:

Output a decimal as the overall resistance value, with two digits reserved.

Reference code:

#include<stdio.h>
int main(){
	int op,r0;
	double r;
	scanf("%d",&r0);
	r = (double)r0;
	while(~scanf("%d%d",&op,&r0)){
		switch(op){
			case 0:
				r += r0;
				break;
			case 1:
			    r = (r*(double)r0)/(r+(double)r0);
				break; 
		}
	}
	printf("%.2lf",r);
	return 0;
}

2. Xiaoqiuyue's code

Title Description:

Xiaoqiuyue took a selfie with a black-and-white filter. Each pixel of the photo can be represented by a gray value of 0-255. Because this selfie is so cute, xiaoqiuyue doesn't want others other than Xiaoqi to see it clearly. He wants to blur this photo.

The way to blur the photo is to replace the gray value of each pixel with the average value of the gray value of it and its adjacent pixels.

Xiaoqiuyue secretly showed you her selfie. Please help him blur the photos and output the gray value of each pixel of the processed photos in turn.

Input format:

The two numbers m, nm and N in the first row represent that the pixels have m rows and N columns. (m,n≤100)

The next m lines, each with n integers, represent the gray value of the pixel.

Output format:

Output m lines, n integers in each line separated by spaces, representing the gray value of this pixel after coding (the result is rounded), and there is also a space after the last number in each line.

Xiaoqiuyue likes to be beautiful and tidy, so each number is required to occupy 3 digits and align to the right.

Reference code:

#include<stdio.h>
#include<math.h>
int main()
{
	int m,n,sum,num;
	scanf("%d%d",&m,&n);
	int image[105][105],image0[105][105];
	for(int i=0;i<m;i++){
		for(int j=0;j<n;j++){
			scanf("%d",&image0[i+1][j+1]);
		}
	}
	for(int i=1;i<=m;i++){
		for(int j=1;j<=n;j++){
			num=(image0[i-1][j-1]!=0)+(image0[i-1][j]!=0)+(image0[i-1][j+1]!=0)+(image0[i][j-1]!=0)+(image0[i][j]!=0)+(image0[i][j+1]!=0)+(image0[i+1][j-1]!=0)+(image0[i+1][j]!=0)+(image0[i+1][j+1]!=0);
			sum=image0[i-1][j-1]+image0[i-1][j]+image0[i-1][j+1]+image0[i][j-1]+image0[i][j]+image0[i][j+1]+image0[i+1][j-1]+image0[i+1][j]+image0[i+1][j+1];
			image[i][j]=(int)((double)sum/(double)num+0.5);
	}
}
	for(int i=0;i<m;i++){
		for(int j=0;j<n;j++){
			printf("%3d ",image[i+1][j+1]);
			if(j==n-1){
				printf("\n");
			}
		}
	}
    return 0;
}

(the exhaustive method is adopted here, the algorithm is relatively low, and the bet pixel value will not appear 0. Other methods are recommended during the examination)

3. Longest and shortest line

Title Description:

There is a konjac that can't count. I hope you can tell ta which line is the shortest and which line is the longest among the several lines of data input.

Input format:

A string consisting of several lines of visible characters containing spaces, the number of lines ≤ 1000, and the length of each line ≤ 1000.

Output format:

Four lines, the first line is the length of the shortest line, the second line is the content of the shortest line, the third line is the length of the longest line, and the fourth line is the content of the longest line.

If there are multiple shortest / longest lines, the shortest / longest line appearing for the first time is output.

Reference code:

#include<stdio.h>
#include<string.h>
int main()
{
	char s[1005],smax[1005],smin[1005];
	int len,lenmax,lenmin;
	gets(s);
	len = strlen(s);
	lenmax=len;
	lenmin=len;
	strcpy(smax,s);
	strcpy(smin,s);
	while(gets(s)!=NULL){
		len = strlen(s);
		if(len>lenmax){
			strcpy(smax,s);
			lenmax=len;
		}
		else if(len<lenmin){
			strcpy(smin,s);
			lenmin=len;
		}
	}
	printf("%d\n",lenmin);
	puts(smin);
	printf("%d\n",lenmax);
	puts(smax);
	return 0;
}

4. Bit operation

Title Description:

For a number   n. In its binary representation, we specify the rightmost bit as the second bit   0   Bit.

There are now multiple sets of queries, each given two numbers   n. M, please count   n   The first   0 ∼ m bit and   m+1 ∼ 2m+1 bit exchange.

Input:

Indefinite group data input

Two numbers separated by spaces on each line   nm

A nonnegative integer   n   Represents the operand (decimal)

A nonnegative integer   See above for the meaning of m

Output:

For each group of data, the number of outputs   n   The first   0 ∼ m bit and m+1 ∼ 2m+1   Result after bit exchange (decimal representation)

Output a set of results per line

Reference code:

#include<stdio.h>
#include<string.h>
#include<stdbool.h>
bool d[63];
int main()
{
	unsigned long long n;int m;
	while(~scanf("%llu%d",&n,&m)){
		for(int i=0;i<=63;i++){
			d[i]=((n>>i&1)==1);
		}
		for(int j=0,k=m+1;j<=m;j++,k++){
			bool t=d[j];
			d[j] = d[k];
			d[k] = t;
		}
		n = 0;
		for(int i=0;i<=63;i++){
			if(d[i]){
				n = ((1ull<<i)|n);
			}
			d[i]=0;
		}
		printf("%llu\n",n);
	}
	return 0;
}

(boolean type is used here, by calling stdpool. H function library)

5. Choose whoever you click

Title Description:

The double eleventh day came again. Xiaohan played a big adventure of truth with his classmates. They made a rule to decide who would challenge. The rules are as follows:

1. n students form a circle, face the center, and stipulate that someone should start first.

2. Everyone can choose to pass left or right. Once it is determined, it cannot be changed.

3. Students outside the field will perform m operations.

4. Each operation consists of two parameters. The first s is to select the opposite or positive direction selected by the student, and the second parameter x represents the transfer of x students.

Please write a program to judge which student won the prize in the end~

Specific operations can be explained and understood in combination with examples.

Input:

The first line of input contains two positive integers n and m, which represent the number of students participating in the game and the number of operations of students outside the field.

next   n lines, each line contains two integers a and b. The first integer a has only two values 0, 1. 0 means that the student will pass to the left, 1 means that the student will pass to the right. The second integer b represents the student number of the student. It is an 8-bit positive integer without leading 0. Given in counterclockwise order.

Next m   Rows, each containing two integers   s  ,  x. if   s=0, indicating the positive direction transfer to the selected direction of the student; If s=1, it means to transfer to the opposite direction of the student's selected direction.   X means that X students have been passed on.

Data range: 1 ≤ n,m ≤ 10 (5), 0 ≤ x < n

Hint:

1. Seems to know which direction to pass by using XOR for a and s?

2. Note that they are in a circle. Oh ~ if you use the array method, you can consider taking a module or adding n when the array subscript exceeds the boundary.

3. Note that the reading is counterclockwise!

Reference code:

#include<stdio.h>
long long stu[100005];
int d[100005];
int main()
{
	int n,m,num=0;
	int s;
	long long x;
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;i++){
		scanf("%d%lld",&d[i],&stu[i]);
	}
	for(int j=0;j<m;j++){
		scanf("%d%lld",&s,&x);
		while(num<0){
			num += n;
		}
		while(num>=n){
			num -= n;
		}
		if(d[num]^s==1){
			num += x;
		}
		else if(d[num]^s==0){
			num -= x;
		}
	}
	while(num<0){
		num = num + n;
	}
	while(num>=n){
		num -= n;
	}
	printf("%lld",stu[num]);
	return 0;
}

Posted by jasonman1 on Sat, 20 Nov 2021 21:44:31 -0800