C++练习三

  1.  货物征税

【问题描述】货物征税问题。对一批货物征收税金,价格在1万元以上部分的征5%,5000元以上、1万元以下部分的征3%,1000元以上、5000以下部分的征2%,1000元以下部分的免税,读入货物价格,计算并输出税金。征税应分段累计,各段采用不同税率进行征收。

(提示:如对2万元货物,应分1万以上、5千~1万及1千~5千部分进行三段征收)。

【输入形式】输入货物金额

【输出形式】输出应付税金(保留小数点后两位)。税金前添加"Tax="进行输出。单独占一行。

【样例输入】20005

【样例输出】Tax=730.25

#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
	double a;
	double tax = 0;
	cin >> a;
	if (a > 10000)
	{
		tax = (a - 10000) * 0.05 + (5000 - 1000) * 0.02 + (10000 - 5000) * 0.03;
	}
	else if (a > 5000)
	{
		tax = (a - 5000) * 0.03 + (5000 - 1000) * 0.02;
	}
	else if (a > 1000)
	{
		tax = (a - 1000) * 0.02;
	}
	else
		tax = 0;
	cout << "Tax=" << fixed << setprecision(2) << tax;
	return 0;
}
2. 分段函数

【问题描述】分段函数。编程根据x的值,计算分段函数y的值(保留小数点后两位)。y的计算公式如下。

【输入形式】用户输入x的值

【输出形式】输出y值。单独占一行

【样例输入】-10.348

【样例输出】10.35

#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;

int main()
{
	double x;
	cin >> x;
	if (x < 0)
		cout << fixed << setprecision(2) << -x;
	else if (x < 10)
		cout << fixed << setprecision(2) << exp(x) * sin(x);
	else if (x < 20)
		cout << fixed << setprecision(2) << pow(x, 3);
	else
		cout << fixed << setprecision(2) << (3 + 2 * x) * log(x);
	return 0;
}
3. 判断字符类别

【问题描述】判断字符类别。判别键盘输入字符的类别,分类规则为:ASCII值小于32的为控制字符;在‘0’和‘9’之间的为数字;在‘A’和‘Z’之间为大写字母; 在‘a’和‘z’之间为小写字母;其余则为其它字符。

【输入形式】用户输入一个字符

【输出形式】输出该字符类别,单独占一行。控制字符输出"A control character";数字字符输出"A numeric character";大写字符输出"A capital letter";小写字符输出"A small letter";其字符输出"An other character"。各单词间用一个空格隔开。

【样例输入】0

【样例输出】A numeric character

【样例输入】$

【样例输出】An other character

#include<iostream>
using namespace std;

int main()
{
	char i;
	cin >> i;
	if (i < 32)
		cout << "A control character";
	else if (i >= '0' && i <= '9')
		cout << "A numeric character";
	else if (i >= 'A' && i <= 'Z')
		cout << "A capital letter";
	else if (i >= 'a' && i <= 'z')
		cout << "A small letter";
	else
		cout << "An other character";
	return 0;
}
4. 字符解密

【问题描述】字符解密。设字符加密规则是:将字母和数字字符在各自范围内用其后面的第n个字符替换,n由用户输入。如果n=4,这样字母A就用E替换、B用F……,当然最后四个字符,W需用A、X需用B替换……,以此类推。现编程让用户输入一加密后的字符和及其加密替换值n,请对它进行解密,求出加密前的字符。

【输入形式】用户输入加密后的字符和加密替换值。两个数据间空白字符隔开。

【输出形式】输出加密前的字符。单独占一行。

【样例输入】F 4

【样例输出】B

【样例输入】8  3

【样例输出】5

#include<iostream>
using namespace std;

int main()
{
	int n;
	char ch;
	cin >> ch >> n;
	if (ch >= 'a' && ch <= 'z')
	{
		if (ch - n >= 'a')
			cout << char(ch - n);
		else
			cout << char(ch - n + 26);
	}
	if (ch >= 'A' && ch <= 'Z')
	{
		if (ch - n >= 'A')
			cout << char(ch - n);
		else
			cout << char(ch - n + 26);
	}
	if (ch >= '0' && ch <= '9')
	{
		if (ch - n >= '0')
			cout << char(ch - n);
		else
			cout << char(ch - n + 10);
	}
	return 0;
} 
5. 求方程的根

【问题描述】编写程序,求方程ax2+bx+c=0的根,需要充分考虑到输入数据的各种情况
【输入形式】a,b,c的值
【输出形式】根值
【样例输入1】0 0 0

【样例输出2】无数个根

1 2 3

x1=-1+1.41421i

x2=-1-1.41421i

2 -8 6

x1=3 x2=1

1 -2 1

x1=x2=1

0 5 8

-1.6

#include<iostream>
#include<cmath>
using namespace std;

int main()
{
	double a, b, c;
	cin >> a >> b >> c;
	if (a == 0)
	{
		if (b == 0)
		{
			if (c == 0)
				cout << "无数个根";
			else
				cout << "无解";
		}
		else
			cout << -c / b;
	}
	else
	{
		double delta = b * b - 4 * a * c;
		if (delta > 0)
		{
			cout << "x1=" << (-b + sqrt(delta)) / 2 / a << ' ';
			cout << "x2=" << (-b - sqrt(delta)) / 2 / a << endl;
		}
		else if (delta == 0)
		{
			cout << "x1=x2=" << -b / 2 / a;
		}
		else
		{
			cout << "x1=" << -b/2/a<<'+'<<sqrt(-delta)/2/a<<'i' << endl;
			cout << "x2=" << -b / 2 / a << '-' << sqrt(-delta) / 2 / a << 'i' << endl;
		}
	}
	return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值