分支
/*
//阶梯电价:每月用电d度,第一档0~240度(含),每度电0.4883元,第二档241~400度(含),每度电0.5383元;第三档400度以上,每度电0.7883元,求本月电费(保留两位)
//如果用电量是246度,那么其中240度执行第一档的价格0.4883×240,超过240度的部分还有6度,执行第二档价格0.5383×6,实际电费为两档电费的总和
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
float d, D;
cin >> d;
switch (int(d / 10))
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
case 16:
case 17:
case 18:
case 19:
case 20:
case 21:
case 22:
case 23:
case 24:D = d*0.4883; break;
case 25:
case 26:
case 27:
case 28:
case 29:
case 30:
case 31:
case 32:
case 33:
case 34:
case 35:
case 36:
case 37:
case 38:
case 39:
case 40:D = 240 * 0.4883 + (d - 240)*0.5383; break;
default:D = 240 * 0.4883 + 160 * 0.5383 + (d - 400)*0.7883; break;
}
cout << fixed << setprecision(2) << D << endl;
return 0;
}
*/
/*
//题意理解错误,这个代码意思是
//0<d<=240则全部0.4883元
//240<d<=400则全部0.5383元
//d>400则全部0.7883元
#include<iostream>
using namespace std;
int main()
{
float d, D;
cin >> d;
if (d <= 240)
{
switch (int(d / 10))
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
case 16:
case 17:
case 18:
case 19:
case 20:
case 21:
case 22:
case 23:
case 24:D = d*0.4883;
}
if (d > 240)
{
switch (int(d / 10))
case 24:
case 25:
case 26:
case 27:
case 28:
case 29:
case 30:
case 31:
case 32:
case 33:
case 34:
case 35:
case 36:
case 37:
case 38:
case 39:
case 40:D = d*0.5383;
}
if (d > 400)
{
switch (int(d / 10))
defult:D = d*0.7883;
}
cout << D << endl;
return 0;
}
*/
/*
//输入一个字符,输出ASCII表中在该字符之后的一个字符
#include<iostream>
using namespace std;
int main()
{
char a,b;
cin >> a;
b = a + 1;
cout << b << endl;
return 0;
}
*/
/*
//蛋糕店促销,购买的数量在1~5个之间,2元 / 个
//购买的数量在6~10个之间,1.9元 / 个
//购买的数量在11~15个之间,1.8元 / 个
//购买的数量在16~20之间,1.7元 / 个
//购买的数量在20个以上,1.6元 / 个
//输入所购买的蛋糕数量,输出所需要付款的金额(保留两位)
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int a;
float b;
cin >> a;
switch (a)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:b = a * 2; break;
case 6:
case 7:
case 8:
case 9:
case 10:b = a*1.9; break;
case 11:
case 12:
case 13:
case 14:
case 15:b = a*1.8; break;
case 16:
case 17:
case 18:
case 19:
case 20:b = a*1.7; break;
default:b = a*1.6; break;
}
cout << fixed << setprecision(2) << b << endl;
return 0;
}
*/
/*
//步行每秒行走1.2米,骑车每秒行走3.0米,找车锁车要花50秒。判断走不同的距离去,骑车快还是走路快
#include<iostream>
using namespace std;
int main()
{
float s,a,b;
cin >> s;
a = 50 + s / 3.0;
b = s / 1.2;
if (a > b)cout << "Walk" << endl;
if (a < b)cout << "Bike" << endl;
if (a == b)cout << "All" << endl;
return 0;
}
*/
/*
//根据邮件的重量和用户是否选择加急计算邮费。计算规则:重量在1000克以内(包括1000克), 基本费8元。超过1000克的部分,每500克加收超重费4元,不足500克部分按500克计算;如果用户选择加急,多收5元。
//输入一行,包含整数和一个字符,以一个空格分开,分别表示重量(单位为克)和是否加急。如果字符是y,说明选择加急;如果字符是n,说明不加急。
#include<iostream>
using namespace std;
int main()
{
int a;
char ch;
cin >> a >> ch;
if (ch == 'n')
{
if (a <= 1000)cout << 8 << endl;
else
{
if (a % 500 == 0)cout << 8 + (a - 1000) / 500 * 4;
else cout << 8 + ((a - 1000) / 500 + 1) * 4;
}
}
if (ch == 'y')
{
if (a <= 1000)cout << 13 << endl;
else
{
if (a % 500 == 0)cout << 13 + (a - 1000) / 500 * 4;
else cout << 13 + ((a - 1000) / 500 + 1) * 4;
}
}
return 0;
}
*/
/*
简单计算机
题目描述
一个最简单的计算器,支持+, -, *, / 四种运算。仅需考虑输入输出为整数的情况,数据和运算结果不会超过int表示的范围。
输入格式
输入只有一行,共有三个参数,其中第1、2个参数为整数,第3个参数为操作符(+,-,*,/)。
输出格式
输出只有一行,一个整数,为运算结果。然而:
1. 如果出现除数为0的情况,则输出:Divided by zero!
2. 如果出现无效的操作符(即不为 +, -, *, / 之一),则输出:Invalid operator!
#include<iostream>
using namespace std;
int main()
{
int x, y;
char c;
cin >> x >> y >> c;
switch (c)
{
case'+':cout << x + y; break;
case'-':cout << x - y; break;
case'*':cout << x * y; break;
case'/':
switch (y)
{
case 0: cout << "Divided by zero!"; return 0;
default:cout << x / y; return 0;
}
default: cout << "Invalid operator!"; break;
}
return 0;
}
*/
/*
#include<stdio.h>
int main()
{
char c;
int a, b, d;
scanf("%d %d %c", &a, &b, &c);
switch ( c )
{
case '+': printf("%d\n", d, d = a+b); break;
case '-': printf("%d\n", d, d = a-b); break;
case '*': printf("%d\n", d, d = a*b); break;
case '/':
if (b == 0)printf("Divided by zero!\n");
else
{
if (b != 0)printf("%d\n", d, d = a / b);
}
break;
default: printf("Invalid operator!\n");
}
return 0;
}
*/
/*
///任意输入一个字符,判断其ASCII是否是奇数,若是,输出YES,否则,输出NO
//例如,字符A的ASCII值是65,则输出YES,若输入字符B(ASCII值是66),则输出NO
#include<iostream>
using namespace std;
int main()
{
char ch;
int c;
//cin>>ch;
ch = getchar();
//c = (int)ch;
if (ch % 2 == 0)cout << "NO" << endl;
else cout << "YES" << endl;
return 0;
}
*/
/*
//给定三个正整数,分别表示三条线段的长度,判断这三条线段能否构成一个三角形。
//如果能够构成三角形,输出Yes和三角形的类型:等边三角形、等腰三角形、直角三角形、普通三角形
//如果不能构成三角形,输出No
//输入共一行,包含三个正整数,分别表示三条线段的长度,数与数之间以一个空格分开。
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int a, b, c;
cin >> a >> b >> c;
if (a + b > c&&a + c > b&&b + c > a)
{
cout << "Yes";
if (a == b&&b == c)cout << "直角三角形" << endl;
if (a == b&&a != c)cout << "等腰三角形" << endl;
if (a == c&&a != b)cout << "等腰三角形" << endl;
if (c == b&&a != c)cout << "等腰三角形" << endl;
else cout << "普通三角形";
}
else cout << "No" << endl;
return 0;
}
*/
/*
//给定一个整数,判断它能否被3,5,7整除,并输出以下信息:
//1、能同时被3,5,7整除(直接输出3 5 7,每个数中间一个空格);
//2、只能被其中两个数整除(输出两个数,小的在前,大的在后。例如:3 5或者 3 7或者5 7, 中间用空格分隔);
//3、只能被其中一个数整除(输出这个除数);
//4、不能被任何数整除,输出小写字符‘n’, 不包括单引号。
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
bool b3 = n % 3 == 0;
bool b5 = n % 5 == 0;
bool b7 = n % 7 == 0;
if (b3)cout << "3 ";
if (b5)cout << "5 ";
if (b7)cout << "7 ";
if (!b3 && !b5 && !b7)cout << 'n';
return 0;
}
*/
/*
分段函数
编写程序,计算下列分段函数y = f(x)的值。
y = -x + 2.5; 0 <= x < 5
y = 2 - 1.5(x - 3)(x - 3); 5 <= x < 10
y = x / 2 - 1.5; 10 <= x < 20
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
float x, y;
cin >> x;
if (0 <= x&&x < 5)y = -x + 2.5;
else if (5 <= x&&x < 10)y = 2 - 1.5*(x - 3)*(x - 3);
else y = x / 2 - 1.5;
cout << fixed << setprecision(3) << y << endl;
return 0;
}
*/
/*
//利用公式
//x1 = (-b + sqrt(b*b - 4 * a*c)) / (2 * a)
//x2 = (-b - sqrt(b*b - 4 * a*c)) / (2 * a)
//求一元二次方程ax2 + bx + c = 0的根,其中a不等于0。
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
double a, b, c, r;
cin >> a >> b >> c;
r = b*b - 4 * a*c;
cout << fixed << setprecision(5);
if (r == 0)
cout << "x1=x2=" << -b / (2 * a);
else if (r < 0)
cout << "无解";
else
cout << "x1=" << (-b + sqrt(r)) / (2 * a) << ';x2=' << (-b - sqrt(r)) / (2 * a);
return 0;
}
*/
/*
//有一个正方形,四个角的坐标(x, y)
//分别是(1, - 1),(1,1),( - 1, - 1),( - 1,1),x是横轴,y是纵轴
//写一个程序,判断一个给定的点是否在这个正方形内(包括正方形边界)
#include<iostream>
using namespace std;
int main()
{
float x, y;
cin >> x >> y;
if (x > -1.0 && x<1.0 && y>-1.0 && y < 1.0)cout << "yes" << endl;
else cout << "no" << endl;
return 0;
}
*/

浙公网安备 33010602011771号