BFU 2020 program design basic experimental questions

table of Contents

experiment one

Experiment two

Experiment Three

Experiment Four


experiment one:

 

1. Write a program to achieve the following functions: find the value.

Thinking: Pay attention to the sign before the score. When is it positive and when is it negative?

2. Write a program to achieve the following functions: find a number between 200 and 300, and meet the conditions: the product of their three numbers is 42, and the sum of the three numbers is 12.

Thinking: How to find a three-digit hundreds, tens, and ones?

 

Ideas:

1. The program can be realized by a for loop, and the key lies in the odd addition and even subtraction, and the sum output is enough.

#include<stdio.h>

int main()
{
	double dsum = 0.; // 用于存储最终答案的数值
	int i;

	for(i = 1; i <= 100; i++) // 通过for循环实现对1~100的操作
	{
		if (1 == i % 2)// int强制类型转换 以满足取模的参数要求 
		{	// 通过取模操作判断当前的i是奇还是偶,若奇则加
			dsum += 1. / i;
		}
		else
		{	// 若偶则减
			dsum -= 1. / i;
		}
	}

	printf("ans = %f", dsum);

	return 0;
}

 

2. This program disassembles the numbers, and performs the operations of integration and summation respectively, and then judges whether the result meets the requirements of the question.

#include<stdio.h>

int main()
{
	int i;
	int cnt = 0; // 对答案进行编码并计数

	for (i = 200; i <= 300; i++)
	{
		int a = i % 10; // 取出个位数字
		int b = (i / 10) % 10; // 取出十位数字
		int c = i / 100; // 取出百位数字

		if ( (42 == a * b * c) && (12 == a + b + c) ) 
		{ // 如若满足题述条件
			printf("ans%d = %d\n", ++cnt, i);
		}
	}

	return 0;
}

 

 

Experiment 2:

Write a program to achieve the following tasks: input several integers from the keyboard, the number of input integers is less than 100, and its value is in the range of 0 to 100, and -1 is used as a sign of the end of input. Count the number of each integer and sort from largest to smallest, and output the sorted result.

Tip: Define two arrays, one is used to store input integers, and the other is used to store statistics.

 

Ideas:

The program opens an acnt array to record the number of occurrences of the number corresponding to the index. First, the acnt array is initialized by obtaining user input.

The code starting from line 17 is to realize the function of outputting from large to small according to the number of occurrences, where i is used to compare the number of occurrences, and its change is from large to small to achieve output in descending order of occurrences, and j is used to traverse the array , To determine whether the number of occurrences of each number is the same as the comparison variable i. The required function can be achieved through this process.

#include<stdio.h>

int acnt[101];// 该数组用于记录 索引所对应的数字 的出现次数

int main()
{
	while(1)
	{ // 通过while循环进行对数据的持续输入
		int t;
		scanf("%d", &t);
		if (-1 == t) break; // 如果输入是-1则跳出循环 即停止输入
		acnt[t]++; // 对数字t的出现次数进行+1
	}

	int i, j;

	for (i = 100; i > 0; i--)
	{ // i用来做出现次数的比对,从大到小,以实现按次数的降序输出
		for (j = 100; j >= 0 ; j--)
		{ // j用来遍历数组,判断每一个数出现的次数是否与 比对变量i 相同
			if(acnt[j] == i) // 数字j出现的次数为i
			{
				printf("出现:%d次的是数字:%d\n", i, j);
			}
		}
	}


	return 0;
}

 

Experiment 3:

Write a function to read characters from standard input until EOF is encountered. The program should output whether each character is a letter. If it is, it is also required to output the numerical position of the letter in the alphabet.

 

Ideas:

This program reads characters in the main function and passes them as actual parameters to the CheckAlpha function. Through the return value of the function, you can know whether the current character is a letter and its order in the alphabet.

The implementation method of the CheckAlpha function is: by judging whether the ascii value of the character is between the letters a/A and z/Z, if it is, the difference with a/A plus 1 is the alphabet order. If it is not a letter or a newline, the corresponding identification value is returned for the processing operation in the main function.

#include<stdio.h>

int CheckAlpha (char a)
{
	if ( a >= 'A' && a <= 'Z')
	{ // 返回字母所在字母表中的位置
		return a - 'A' + 1;
	}
	if ( a >= 'a' && a <= 'z')
	{ // 返回字母所在字母表中的位置
		return a - 'a' + 1;
	}
	if (10 == a) 
	{ // 如果是换行符(ascii对应10)
		return -1;
	}
	else 
	{ // 如果是非字母
		return 0;
	}
}

int main()
{
	char t;// 用于临时存储当前输入的字符

	while ((t = getchar()) != EOF)
	{
		int re = CheckAlpha(t); // re用于存储check函数的返回值,以减少重复调用

		if (0 == re)
		{ // 若非字母
			printf("该字符不是字母\n");
		}
		else if (-1 == re)
		{ // 若为回车键 则不进行操作
			continue;
		}
		else
		{ // 若为字母
			printf("该字符是字母,其位置是%d\n", re);
		}
	}

	return 0;
}

 

Experiment 4:

Define the function void mystrcat(char *s1,char *s2,char *new_s) to realize the cross connection of two strings.

For example: there are two character strings "abcd" and "1234", the result of cross-connection is "a4b3c2d1".

 

Idea:
First, perform a demand analysis on the core function of the program. It needs to cross-connect the string s1 and the string s2, and store the result of the cross-connection in the new_s string. Therefore, according to mathematical analysis, the expression can be obtained. The answer can be obtained by designing a for loop according to the general term formula, and the transmission of parameters is achieved through pointers.

However, under special circumstances (s1 and s2 are not the same length), you need to connect the remainder to the string

 

All codes:

#include<stdio.h>
#include<string.h>
#define LEN 100

void mystrcat (char *s1, char *s2, char *new_s);

int main()
{
	char a[LEN], b[LEN], c[LEN * 2];
	
	printf("pleas input two strings, separated by ENTER\n");
	scanf("%s", a);
	scanf("%s", b);


	mystrcat(a, b, c); // 进行处理 

	printf("After merged: %s\n", c);

	return 0;
}

void mystrcat (char *s1, char *s2, char *new_s)
{
	int l1=strlen(s1),l2=strlen(s2), i;
	
	if(l1 == l2)
	{
		for(i = 0; i < l1; i++)
		{
			new_s[2 * i] = s1[i];
			new_s[2 * i + 1] = s2[l2 - i - 1];
		}
		new_s[l1 + l2]= '\0'; 
	}
	
	if (l1 > l2)
	{
		for (i = 0; i < l2; i++)
		{ // 把能正常弄得先弄上 
			new_s[2 * i] = s1[i];
			new_s[2 * i + 1] = s2[l2 - i - 1];
		} 
		// now i = l2  
		// new should start from 
		for (; i < l1; i++)
		{
			// 根据算法推导可知 从l2 + i 开始记录新数 
			new_s[l2 + i] = s1[i];
		}
		new_s[l1 + l2]= '\0'; 
		
	}
	
	if (l1 < l2)
	{
		for (i = 0; i < l1; i++)
		{ // 把能正常弄得先弄上 
			new_s[2 * i] = s1[i];
			new_s[2 * i + 1] = s2[l2 - i - 1];
		} 
		// now i = l1 
		// new should start from 
		for (; i < l2; i++)
		{
			// 根据算法推导可知 从l2 + i 开始记录新数 
			new_s[l1 + i] = s2[l2 - i - 1];
		}
		new_s[l1 + l2]= '\0'; 
		
	}
}

 

Guess you like

Origin blog.csdn.net/ShuoCHN/article/details/112420945