Blue Leopard 2.8 programming questions

Blue Leopard 2.8 programming questions

1. Solve the quadratic equation ax`2+bx+c=0

#include<iostream>
using namespace std;
#include<cmath>
int main(){
    double a,b,c,x1,x2,t;
    cout<<"Please enter the three coefficients of the quadratic equation:"<<endl;
    cin>>a>>b>>c;
    t=b*b-4*a*c;                
    //root formula
    x1=(-b+sqrt(t))/(2*a);		
    x2=(-b-sqrt(t))/(2*a);
    cout<<"x1="<<x1<<"  x2="<<x2<<endl;
    return 0;
}

Calling sqrt in the cmath library can do more with less, without having to write the code for the power of half.

2. Design a program to complete the following functions: input two integer numbers, and output the quotient and remainder after dividing the two integer numbers.

#include<iostream>
using namespace std;
int main(){
    int num1,num2,s,y;  //s: quotient y: surplus
    cout<<"Enter two integers:"<<endl;
    cin>>num1>>num2;
    s=num1/num2;
    y=num1%num2;
    cout<<"The business is: "<<s<<endl;
    cout<<"The remainder is: "<<y<<endl;
    return 0;
}

The prototype of the daffodil number problem

3...Enter 9 integer numbers less than 8 digits, and then print in 3 lines, each column should be aligned. For example, the input is: 1, 2, 3, 11, 22, 33, 111, 222, 333, and the output is

​ 1 2 3

​ 11 22 33

​ 111 222 333

#include<iostream>
using namespace std;
int main(){
    int num1,num2,num3,num4,num5,num6,num7,num8,num9;
    cin>>num1>>num2>>num3>>num4>>num5>>num6>>num7>>num8>>num9;
    cout<<num1<<'\t'<<num2<<'\t'<<num3<<endl;
    cout<<num4<<'\t'<<num5<<'\t'<<num6<<endl;
    cout<<num7<<'\t'<<num8<<'\t'<<num9<<endl;
    return 0;
}

\t is \, not /, \t the distance of a tab; my notation: the bar can be leaned against the characters next to it.

4. For a certain type of work, wages are calculated on an hourly basis. Monthly labor hours (hours) multiplied by hourly wages equals gross wages. 10% of the provident fund is deducted from the total salary, and the remainder is the salary payable. Write a program to input labor hours and hourly wages from the keyboard, and output the wages due.

#include<iostream>
using namespace std;
int main(){
    float total_wage,twage,hour,wage;
    cout<<"Please enter hourly wage:"<<endl;
    cin>>twage;
    cout<<"Please enter monthly working hours:"<<endl;
    cin>>hour;
    total_wage=twage*hour;
    wage=0.9* total_wage;
    cout<<"Salary due this month is:"<<wage<<endl;
    return 0;
}

5. Write a program for the checkout of the salesperson in the fruit store. Known apples are 2.50 yuan per catty, pears are 1.80 yuan per catty, bananas are 2 yuan per catty, and oranges are 1.60 yuan per catty. Ask to enter the weight of various fruits and print the amount due. Then enter the customer payment amount and print the amount that should be changed.

#include<iostream>
using namespace std;
int main(){
    double p_apple=2.50,p_pear=1.80,p_ban=2.00,p_org=1.60;
    double apple,pear,ban,org;
    double money,income,change;
    cout<<"Please enter the weight of apples, pears, bananas, oranges:"<<endl;
    cin>>apple>>pear>>ban>>org;
    money=apple*p_apple+pear*p_pear+ban*p_ban+org*p_org;
    cout<<"Financial payable is: "<<money<<endl;
    cout<<"you should pay:"<<endl;
    cin>>income;
    change=income-money;
    cout<<"change"<<change<<"Yuan";
    return 0;
}

6. Write a program to complete the following functions: input a character and output its ASCII value.

#include<iostream>
using namespace std;
int main(){
	char ch;
	cout<<"Enter characters:"<<endl;
    cin>>ch;
    cout<<static_cast<int>(ch)<<endl;   //type conversion
    return 0;
}

7. Assuming that the campus electricity bill is 0.6 yuan/kWh, enter how many kWh of electricity you use this month, and calculate the electricity bill you have to pay. If you only have 1 yuan, 50, and 10 coins, how many 1 yuan, 50, and 10 coins are needed. For example, the amount of electricity used this month is 11, then the output is

Electricity: 6.6

A total of 6 pieces of 1 yuan, 1 piece of 5 angle and 1 piece of 1 angle are required

#include<iostream>
using namespace std;
int main(){
    int amount,money;
    cout<<"The amount of electricity used for the month is:"<<endl;
    cin>>amount;
    money=amount*6;   //directly use the angle as a unit
    cout<<"common needs"<<money/10<<"Zhang 1 yuan,"<<money%10/5<<"Zhang 5 angle,"<<money%5<<"Zhang 1 corner"<<endl;
    return 0;
}

8. Design and implement a program for a bank to calculate interest. The input is the deposit amount and the deposit period, and the output is the sum of the principal and interest of the deposit. Assuming the annual interest rate is 1.2%, the formula for calculating the sum of deposit principal and interest is principal + principal * annual interest rate * deposit period.

#include<iostream>
using namespace std;
int main(){
    double money,year,profit;
    cout<<"Please enter your deposit amount and deposit period:"<<endl;
    cin>>money>>year;
    profit=money+money*0.012*year;
    cout<<"The sum of the principal and interest of the deposit is:"<<profit<<endl;
    return 0;
}

9. Write a program that reads in 4 integers and outputs their average. An example of the execution result of the program is as follows: Please enter 4 integers: 5 7 9 6

The average of 5 7 9 6 is 6.75

#include<iostream>
using namespace std;
int main(){
    int num1,num2,num3,num4;
    double ava;
    cout<<"Please enter 4 integers:"<<endl;
    cin>>num1>>num2>>num3>>num4;
    ava=(num1+num2+num3+num4)/4.0;
    cout<<num1<<num2<<num3<<num4<<"The average is"<<ava;
    return 0;
}

10. Write a program that outputs a few bytes of int type data in the C++ system you use, a few bytes of double type data, a few bytes of short int, and a few bytes of float type.

#include<iostream>
using namespace std;
int main(){
	int a;
	double b;
	short int c;
	float d;
	cout<<"int type of data accounting"<<sizeof(a)<<"bytes"<<endl;
	cout<<"double type of data accounting"<<sizeof(b)<<"bytes"<<endl;
	cout<<"short int type of data accounting"<<sizeof(c)<<"bytes"<<endl;
	cout<<"float type of data accounting"<<sizeof(d)<<"bytes"<<endl;
	return 0;
}

11. For two points (x1, y1) and (x2, y2) on a two-dimensional plane, write a program to calculate the distance between the two points.

#include<iostream>
using namespace std;
#include<cmath>
int main(){
    double x1,y1,x2,y2,a,dis;
    cout<<"Please enter the first point:"<<endl;
    cin>>x1>>y1;
    cout<<"Please enter a second point:"<<endl;
    cin>>x2>>y2;
    a=(x2-x1)*(x2-x1)+(y2-y1)*(y2-y1);
    dis=sqrt(a);
    cout<<"The distance is:"<<dis<<endl;
    return 0;
}

Tags: C++ programming language

Posted by rjlowe on Thu, 30 Jun 2022 21:03:51 +0930