Huazhong University of Science and Technology SPOC Programming Questions Chapter 4

1 Calculate the height of the ball. (10 points)
Subject content:

A small ball falls from a height of 100 meters, bounces back to half of its original height each time it hits the ground, and then falls again. What is the total number of meters that it has passed on the nth landing? How high is the nth rebound? The value of n is input by the user and the range is 1<=n<=10.

Input format:

Enter the value of n

Output format:

First output the total number of meters passed during the nth landing, and then output the rebound height. Note that the output value retains 4 decimal places after the decimal point,

The punctuation marks in the output sentence are Chinese punctuation marks.

Input sample:

10

Sample output:

On the 10th landing, a total of 299.6094 meters passed; the rebound height was 0.0977 meters.

Time limit: 500ms Memory limit: 32000kb

#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main(){
    
    
int n,i;
double h=0,sh=100;cin>>n;
for( i=0;i<n;i++)
{
    
    
sh+=2*h;h=100*pow(0.5,i+1);
}
cout<<"第"<<n<<"次落地时,共经过"<<setiosflags(ios::fixed)<<setprecision(4)<<sh<<"米"<<";反弹高度是"<<setiosflags(ios::fixed)<<setprecision(4)<<h<<"米。"<<endl;}

2 Calculate the value of the expression. (10 points)
Subject content:

Write a program to calculate the value of the following expressions.

f(x)=1/n(1 2+2 2+3 2+...+n 2) (n is an integer, input by the user)

Note: In the formula, n is the value of 1,2,3,..., and n^2 is the square of n.

If the value of n is less than or equal to 0, the value of the expression is 0.

Input format:

Enter the value of n

Output format:

Output the value of the calculated expression.

Input sample:

15

Sample output:

The value of the expression = 82.6667

Time limit: 500ms Memory limit: 32000kb

#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main(void){
    
    
double n;double s;cin>>n;
if(n<=0)
s=0;
else s=(n+1)*(2*n+1)/6;

cout<<"表达式的值="<<s<<endl;}

3 system conversion (10 points)
topic content:

Please write a program to convert a 5-digit integer of any length to a decimal number and output the conversion result. If the input data is illegal, it will display Input error.

Note that there is an English space between Input errors.

Input format:

Enter the number to be converted

Output format:

Output the result of the conversion.

Input example 1:

1234

Output sample 1:

194

Input example 2:

789

Output sample 2:

Input error

Time limit: 500ms Memory limit: 32000kb

#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main(void){
    
    int n,s=0,i=0,flag=0;cin>>n;
while(n!=0)
{
    
    if(n%10>4)
{
    
    flag=1;break;}
s+=(n%10)*pow(5,i);
n/=10;i++;
}
if(flag==1)
cout<<"Input error"<<endl;
else cout<<s<<endl;

}

Guess you like

Origin blog.csdn.net/weixin_51236357/article/details/112075635