C++ programming foundation_Huazhong University of Science and Technology MOOC original question

MOOC topic of Huazhong University of Science and Technology.

Today, a friend of mine asked me to do the topic of their MOOC at Huazhong University of Science and Technology, and I reluctantly did it for him.
Let me declare at the beginning that the answer to the question is not unique, and mine is not the best answer in terms of computer efficiency.

First, let's take a look at their topic?

Topic One

For a positive integer n, we add its bits to get a new number. If the number is a single digit, we call it the root of n,
otherwise repeat the process until it becomes a single digit. This single digit is the root of n.
One-digit number root is 0

For example: 24,2+4=6,6 is the number
398 of 24, 3+9+8=20, 2+0=2, 2 is the number of 398

#include<iostream>
#include<windows.h>

int main()
{
    
    
	int num;										//定义用户将要输入的数。
	do
	{
    
    
		std::cout << "请输入您需要求的数:	";
		std::cin >> num;							//接受用户输入。
	}while( num < 0 );								//过滤掉用户的不正确输入。

	if( num < 10 )									//确定用户输入为个位数的特殊情况。
	{
    
    
		std::cout << "数根是:0" << std::endl;

		system("pause");							//使程序暂停,有利于观察。
		return 0;									//退出程序。
	}

	int sum = 0;
LOOP1:	
	while( num > 0 )
	{
    
    
		sum = sum + num % 10;						//提取出num的个位将其加到sum中。
		num = num / 10;								//将num的个位去掉变成一个新的数。
	}

	if( sum > 9 )									//判断经过上面的操作后,得到的sum是否为个位数,若不是个位数将继续执行上面的操作。
	{
    
    
		num = sum;
		sum = 0;
		goto LOOP1;									//跳字上方loop1处。
	}

	std::cout << "他的数根是:" << sum << std::endl;

	system("pause");
	return 0;
}

Topic two

Calculate sum = 2/1 + 3/2+…+(n+1)/n

//计算sum = 2/1 + 3/2+…+(n+1)/n
#include<iostream>

int main()
{
    
    
	int n;								//定义用户即将输入的数。
	do
	{
    
    
		std::cout << "n = ";
		std::cin >> n;					//接受用户输入。
	}while( n <= 0 );

	double sum = 0;						//定义最终答案变量

	double i;							//定义一个双精度型的i变量,用于后面的计算。(若将double改为int的话,会发生不堪设想的后果。)
	for( i = 1 ; i <= n ; i++ )
	{
    
    
		sum = sum + ( i + 1 ) / i;		//加
	}

	std::cout << "total=" << sum << std::endl;		//输出结果

	return 0;
}

Personally, this question is too simple. After all, the author himself has done similar questions before.

Topic Three

Topic content: programming calculation s=a+aa+aaa+…+aaa…a (n a), the value of a is 0-9, the value of n is 0-5, a and n are input by keyboard

For example: a=1, n=3, then s=1+11+111=123

/*
题目内容:编程计算s=a+aa+aaa+…+aaa…a(n个a),a的取值为0~9,n的取值为0~5,a和n由键盘输入

例如:a=1,n=3,则s=1+11+111=123
*/

#include<iostream>
#include<math.h>

int main()
{
    
    
	int n , a;						//定义 n 和 a 

	do
	{
    
    
		std::cout << "n = " ;
		std::cin >> n;				//接收用户输入。
	}while( n < 0 || n > 5 );		//该循环用于避免使用者的非法输入。

	do
	{
    
    
		std::cout << "a = " ;
		std::cin >> a;				//接收用户输入。
	}while( a <= 0 || a > 9 );		//该循环用于避免使用者的非法输入。

	int sum = 0 , i , j , temp ;	 //Temp表示一个数。例如111、77、55555。

	for( i = 1 ; i <= n ; i++ )
	{
    
    
		j = i;
		for( temp = 0 ; j ; j-- )
		{
    
    
			temp = temp + a * pow( 10 , j - 1 );	//计算temp的值。
		}
		sum = sum + temp;							//将每次计算出的temp的值加入到sum变量中。
	}

	std::cout << "total = " << sum << std::endl;		//输出结果。

	return 0;
}

This question slightly took a certain amount of brain space. But everything is fine. > ^ _ ^ <

Last but not least,
1. The strength of programming is achieved through continuous practice and continuous accumulation of experience.
2. I hope that all the elders will raise your hands high and give the baby a thumbs up.

Guess you like

Origin blog.csdn.net/xiaoqianqian88/article/details/112402715