2018年内蒙古大学计算机学院专业课真题级答案

1. 输入俩个整数,求两个数之间所有偶数的和(含这两个数)

    int n, m,mid;
	int result = 0;
	cin >> m >> n;
	if (m > n) { mid = m; m = n; n = mid; }
	for (int i = m; i <= n; i++)
	{
		if (i % 2 == 0) result += i;
	}
	cout << result << endl;

2、1+2+2+3+3+3+…+n+n+…+n求和

    int n;
	int result=0;
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= i; j++)
		{
			result += i;
		}
	}
	cout << result;

3、迭代法求平方根,求平方根的迭代公式为: a[n+1]=1/2(a[n]+X/a[n]) 要求前后两次求出的得差的绝对值少于0.00001。`

float x1, x2, a;
cin >> a;
x1 = 1;                            //赋个初值
x2 = a;
while (fabs(x1 - x2) >= 0.000001)   //迭代过程
{
	x1 = x2;
	x2 = (x1 + a / x1) / 2;
}
cout << x1 << endl;

`## 4、把文件里面的信息读入 然后80个字符是一行 统计行数 小写字母 大写字母 数字的个数

ifstream in_stream("home/1.txt");
	ofstream out_stream;
	string str;
	int line_num=0;
	int B_num=0;
	int S_num=0;
	int D_num = 0;
	if (in_stream.is_open())
	{
		while (getline(in_stream, str))                 //按行读取
		{
			line_num++;
			for (int i = 0; i= 'A'&&str[i] <= 'Z')
					B_num++;
				if (str[i] >= 'a'&&str[i] <= 'z')
					S_num++;
				if (str[i] >= '0'&&str[i] <= '9')
					D_num++;
			}
		}
	}
	in_stream.close();
	cout << line_num << " " << B_num << " " << S_num << " " << D_num << endl;

5、输入一个整数,用递归获取每一位上的数字,并且输出。比如:输入123,输出3 2 1

void get_Num(int n)
{
if (n == 0) return;
else {
int yu;
yu = n % 10;
cout << yu<<" ";
get_Num(n / 10);
}

}
int main()
{

int n;
cin >> n;
get_Num(n);
system("pause");

}

6、考选择排序

void selectSort(int a[], int n)
{
	int i, j;
	int index = 0;
	for (i = 1; i < n; i++)           //执行第i遍扫描操作
	{
		index = i;
		for (j = i+1; j <=n; j++)
		{
			if (a[j] < a[index]) index = j;
		}
		if (i!= index) { int mid = a[i]; a[i] = a[index]; a[index] = mid; }
	}
}
int main()
{

	int a[100];
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	selectSort(a, n);
	for (int i = 1; i <= n; i++)
		cout << a[i];
	system("pause");
}

7、是考杨辉三角的 (a+b)n次方方程的展开后的各项系数

你可能感兴趣的:(2018年内蒙古大学计算机学院专业课真题级答案)