
【描述】
编写程序,比较两个有理数的大小。
要求使用结构表示有理数。
【输入】
输入在一行中按照“a1/b1 a2/b2”的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的正整数。
【输出】
在一行中按照“a1/b1 关系符 a2/b2”的格式输出两个有理数的关系。
其中“>”表示“大于”,“<”表示“小于”,“=”表示“等于”。
【输入示例1】
1/2 3/4
【输出示例1】
1/2 < 3/4
【输入示例2】
6/8 3/4
【输出示例2】
6/8 = 3/4
#include
#include
using namespace std;
int main(){
struct num{
int a;
char c;
int b;
};
struct num n[2];
int i;
char ch;
for(i=0;i<2;i++){
cin>>n[i].a;
n[i].c=getchar();
cin>>n[i].b;
}
if(1.0*n[0].a/n[0].b == 1.0*n[1].a/n[1].b) ch='=';
else if (1.0*n[0].a/n[0].b > 1.0*n[1].a/n[1].b) ch='>';
else ch ='<';
cout<
【描述】
编写程序,计算两个有理数的和。
要求使用结构表示有理数。
【输入】
输入在一行中按照“a1/b1 a2/b2”的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的正整数。
【输出】
在一行中按照“a/b”的格式输出两个有理数的和。
注意必须是该有理数的最简分数形式,若分母为1,则只输出分子。
【输入示例1】
1/3 1/6
【输出示例1】
1/2
【输入示例2】
4/3 2/3
【输出示例2】
2
#include
#include
using namespace std;
int gcd(int m,int n){
int temp;
if(m>n){
temp=m;
m=n;
n=temp;
}
while(m!=0){
temp=m;
m=n%m;
n=temp;
}
return n;
}
int main(){
struct num{
int a;
int b;
char c;
};
struct num n[3];
int i;
for(i=0;i<2;i++){
cin>>n[i].a;
n[i].c=getchar();
cin>>n[i].b;
}
n[2].a=n[0].a*n[1].b+n[1].a*n[0].b;
n[2].b=n[0].b*n[1].b;
i=gcd(n[2].a,n[2].b);
if(n[2].a%n[2].b==0)
cout<
【描述】
给定一组点(x, y),求距离最远的两个点之间的距离。
【输入】
第一行是点数n(n≥2)。
接着每一行代表一个点,由两个浮点数x、y组成。
【输出】
输出一行是最远两点之间的距离,保留4位小数。
【输入示例】
6 34.0 23.0 28.1 21.6 14.7 17.1 17.0 27.2 34.7 67.1 29.3 65.1
【输出示例】
53.8516
#include
#include
#include
using namespace std;
int main()
{
int n;
cin>>n;
float *x,*y;
double max=0;
double t;
int i,j;
x=new float[n];
y=new float[n];
for(i=0;i>x[i]>>y[i];
for(i=0;i
描述】
编写程序,计算N个有理数的平均值。
要求使用结构表示有理数。
【输入】
输入第1行给出正整数N(≤100);第2行中按照“a1/b1 a2/b2 ……”的格式给出N个分数形式的有理数,其中分子和分母全是整型范围内的整数;如果是负数,则负号一定出现在最前面。
【输出】
在一行中按照“a/b”的格式输出N个有理数的平均值。
注意必须是该有理数的最简分数形式,若分母为1,则只输出分子。
【输入示例1】
4 1/2 1/6 3/6 -5/10
【输出示例1】
1/6
【输入示例2】
2 4/3 2/3
【输出示例2】
1
#include
#include
using namespace std;
int gcd(int m, int n) {
int temp;
if (m > n) {
temp = m;
m = n;
n = temp;
}
while (m != 0) {
temp = m;
m = n % m;
n = temp;
}
return n;
}
int main() {
struct num {
int a;
int b;
char c;
int sum;
};
struct num n[3];
int i = 0, j = 0, t;
n[0].sum = n[1].sum = 0;
cin >> t;
cin >> n[j].a;
n[j].c = getchar();
cin >> n[j].b;
j++;
t--;
while (t--) {
cin >> n[j].a;
n[j].c = getchar();
cin >> n[j].b;
n[j].a = n[j].a * n[j - 1].b + n[j - 1].a * n[j].b;
n[j].b = n[j - 1].b * n[j].b;
j++;
}
n[j - 1].b *= j;
i = gcd(n[j - 1].a, n[j - 1].b);
if (n[j - 1].a % n[j - 1].b == 0)
cout << n[j - 1].a / i << endl;
else
cout << n[j - 1].a / i << "/" << n[j - 1].b / i << endl;
}
【描述】
通讯录中的一条记录包含下述基本信息:朋友的姓名、出生日期、性别、固定电话号码、移动电话号码。
编写程序,录入N条记录,并且根据要求显示任意某条记录。
要求使用结构表示基本信息。
【输入】
输入在第1行给出正整数N(<=10);随后N行,每行按照格式“姓名 生日 性别 固话 手机”给出一条记录。
其中“姓名”是不超过10个字符、不包含空格的非空字符串;生日按“yyyy/mm/dd”的格式给出年月日;性别用“M”表示“男”、“F”表示“女”;“固话”和“手机”均为不超过15位的连续数字,前面有可能出现“+”。
在通讯录记录输入完成后,最后一行给出正整数K,并且随后给出K个整数,表示要查询的记录编号(从0到N-1顺序编号)。
数字间以空格分隔。
【输出】
对每一条要查询的记录编号,在一行中按照“姓名 固话 手机 性别 生日”的格式输出该记录。
若要查询的记录不存在,则输出“Not Found”。
【输入示例】
3 Chris 1984/03/10 F +86181779452 13707010007 LaoLao 1967/11/30 F 057187951100 +8618618623333 QiaoLin 1980/01/01 M 84172333 10086 2 1 7
【输出示例】
LaoLao 057187951100 +8618618623333 F 1967/11/30 Not Found
#include
#include
using namespace std;
int main()
{
struct phone
{
char name[20];
char date[20];
char hua[20];
char fm;
char shou[20];
};
struct phone s[11];
int n, k,p;
cin>>n;
for (int i = 0; i < n; i++)
{
scanf("%s %s %c %s %s", &s[i].name, &s[i].date, &s[i].fm, &s[i].hua, &s[i].shou);
}
cin>>k;
for (int j = 0; j < k; j++)
{
cin>>p;
if (p >=0 &&p < n)
cout<
描述】
编写程序,求按顺时针方向输入n(0<n≤10)个顶点坐标的多边形周长。
要求顶点坐标用结构表示。
【输入】
第一行输入正整数n;下面n行,每行两个浮点数,以空格间隔,为多边形各顶点的坐标。
【输出】
多边形的周长,精确到小数点后2位。
【输入示例】
4 0 0 0 1 1 1 1 0
【输出示例】
4.00
【提示】
顶点坐标不一定是整数。
#include
#include
#include
using namespace std;
double computeSide(double x1, double y1, double x2, double y2) {
double side = 0.0;
side = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
return side;
}
int main() {
struct top {
double x;
double y;
};
struct top dd[20];
int n;
int i = 0, j = 0;
double C = 0.0;
cin >> n;
for (i = 0; i < n; i++) {
cin >> dd[i].x >> dd[i].y;
}
for ( j = 0; j < n - 1; j++) {
C += computeSide(dd[j].x, dd[j].y, dd[j + 1].x, dd[j + 1].y);
}
if (n > 2)
C += computeSide(dd[0].x, dd[0].y, dd[j].x, dd[j].y);
cout << fixed << setprecision(2) << C << endl;
return 0;
}
【描述】
某个科室的病房分为重症和普通,只有当病人的疾病严重程度超过了入住重症病房的最低严重值,才可以安排入住重症病房。
现在要求设计一个程序,给病人安排好病房。
疾病的严重程度用0到10来表示,0表示小毛病,10表示非常严重。
【输入】
第一行输入病人的个数m(m<50),以及安排住入重症病房的最低严重值a。
紧接着m行,每行表示病人编号(三位,用0补齐)及其疾病的严重程度(浮点数,1位小数)。
【输出】
每个病人的疾病严重程度都不一样。
输出要求按照病人的严重程度输出住在重症病房里的病人的编号。
【注意】
如果当前所有病人的严重程度并不满足住在重症病房里,则输出“None.”。
【输入示例】
10 7.55 006 6.5 005 8.0 004 3.5 009 8.5 011 7.0 043 9.5 003 5.0 103 6.0 112 4.0 118 9.0
【输出示例】
043 9.5 118 9.0 009 8.5 005 8.0
【提示】
可以定义如下结构类型:
struct Person {
int no; // 病人的编号
double num; // 病人病情严重程度
};
#include
#include
using namespace std;
int main() {
struct Person {
int no;
double num;
};
struct Person p[51];
int m, i, j;
float a;
cin >> m >> a;
for (i = 0; i < m; i++) {
cin >> p[i].no >> p[i].num;
}
for (j = 1; j < m; j++) {
for (i = 0; i < j; i++) {
if (p[i].num < p[j].num) {
struct Person temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
}
bool judge = false;
for (i = 0; i < m; i++) {
if (p[i].num > a) {
cout << setw(3) << setfill('0') << p[i].no << " ";
cout << fixed << setprecision(1) << p[i].num << endl;
judge = true;
}
}
if (!judge)
cout << "None." << endl;
return 0;
}
描述】
编写程序,将登记看病的病人按照以下原则排出看病的先后顺序:
1.老年人(年龄≥60岁)比非老年人优先看病。
2.老年人按年龄从大到小的顺序看病,年龄相同的按登记的先后顺序排序。
3.非老年人按登记的先后顺序看病。
【输入】
第一行输入一个小于100的正整数,表示病人的个数;
下面按照病人登记的先后顺序,每行输入一个病人的信息,包括:一个长度小于10的字符串表示病人的ID(每个病人的ID各不相同且只含数字和字母),一个整数表示病人的年龄,其间以空格分隔。
【输出】
按照排好的看病顺序输出病人的ID,每行一个。
【输入示例】
5 021075 40 004003 15 010158 67 021033 75 102012 30
【输出示例】
021033 010158 021075 004003 102012
#include
using namespace std;
struct Patient {
int no;
char id[11];
int age;
} p[101];
bool cmp(Patient a, Patient b) {
if (a.age >= 60 && b.age >= 60) {
if (a.age != b.age)
return a.age > b.age;
else
return a.no < b.no;
} else if (a.age >= 60)
return true;
else if (b.age >= 60)
return false;
else
return a.no < b.no;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> p[i].id >> p[i].age;
p[i].no = i;
}
sort(p, p + n, cmp);
for (int i = 0; i < n; i++)
cout << p[i].id << endl;
return 0;
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)