牛客编程语言练习赛第三场(C++)

 

题目描述

KiKi学会了printf在屏幕输出信息,他想输出一架小飞机。请帮他编写程序输出这架小飞机。

输入描述:

输出描述:

 

AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <stdlib.h>
#include <algorithm>
using namespace std;

int main()
{
	cout<<"     **     "<<endl;
	cout<<"     **     "<<endl;
	cout<<"************"<<endl;
	cout<<"************"<<endl;
	cout<<"    *  *    "<<endl;
	cout<<"    *  *    ";
	return 0;
}


 




 

题目描述

依次输入一个学生的学号,以及3科(C语言,数学,英语)成绩,在屏幕上输出该学生的学号,3科成绩。

输入描述:

学号以及3科成绩,学号和成绩之间用英文分号隔开,成绩之间用英文逗号隔开。

输出描述:

学号,3科成绩,输出格式详见输出样例。

示例1

输入

17140216;80.845,90.55,100.00

 

输出

The each subject score of No. 17140216 is 80.85, 90.55, 100.00.

 

AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <stdlib.h>
#include <algorithm>
using namespace std;

int main()
{
	string id;
	float C,math,eng;
	getline(cin,id,';');
	scanf("%f,%f,%f",&C,&math,&eng);
	cout<<"The each subject score of  No. "<<id;
	printf(" is %.2f, %.2f, %.2f.",C,math,eng);
	
	return 0;
}

 



 

题目描述

请计算表达式“(-8+22)×a-10+c÷2”,其中,a = 40,c = 212。

 

输入描述:

无。

输出描述:

(-8+22)×a-10+c÷2计算之后的结果,为一个整数。

AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <stdlib.h>
#include <algorithm>
using namespace std;

int main()
{
	int a=40, c=212;
	cout<<(-8+22)*a-10+c/2;
	return 0;
}

 





 

题目描述

根据给出的三角形3条边a, b, c(0 < a, b, c < 100,000),计算三角形的周长和面积。

输入描述:

一行,三角形3条边(能构成三角形),中间用一个空格隔开。

输出描述:

一行,三角形周长和面积(保留两位小数),中间用一个空格隔开,输出具体格式详见输出样例。

示例1

输入

3 3 3

 

输出

circumference=9.00 area=3.90

 

AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <stdlib.h>
#include <algorithm>
using namespace std;

int main()
{
	float a,b,c;
	cin>>a>>b>>c;
	float s = (a+b+c)/2;
	printf("circumference=%.2f area=%.2f",a+b+c,sqrt(s*(s-a)*(s-b)*(s-c)));
	return 0;
}

 


 

题目描述

从键盘输入a, b, c的值,编程计算并输出一元二次方程ax2 + bx + c = 0的根,当a = 0时,输出“Not quadratic equation”,当a ≠ 0时,根据△ = b2 - 4*a*c的三种情况计算并输出方程的根。

输入描述:

多组输入,一行,包含三个浮点数a, b, c,以一个空格分隔,表示一元二次方程ax2 + bx + c = 0的系数。

输出描述:

 

针对每组输入,输出一行,输出一元二次方程ax2 + bx +c = 0的根的情况。

  如果a = 0,输出“Not quadratic equation”;

  如果a ≠  0,分三种情况:

△ = 0,则两个实根相等,输出形式为:x1=x2=...

△  > 0,则两个实根不等,输出形式为:x1=...;x2=...,其中x1  <=  x2。

△  < 0,则有两个虚根,则输出:x1=实部-虚部i;x2=实部+虚部i,即x1的虚部系数小于等于x2的虚部系数,实部为0时不可省略。实部= -b / (2*a),虚部= sqrt(-△ ) / (2*a)

所有实数部分要求精确到小数点后2位,数字、符号之间没有空格。
 

示例1

输入

2.0 7.0 1.0

 

输出

x1=-3.35;x2=-0.15

 

示例2

输入

0.0 3.0 3.0

 

输出

Not quadratic equation

 

示例3

输入

1 2 1

 

输出

x1=x2=-1.00

 

示例4

输入

2 2 5

 

输出

x1=-0.50-1.50i;x2=-0.50+1.50i

 

示例5

输入

1 0 1

 

输出

x1=0.00-1.00i;x2=0.00+1.00i

 

AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <stdlib.h>
#include <algorithm>
#include <iomanip>
using namespace std;
const double eps = 1.0e-6;
int main()
{
	double a,b,c;
while(cin>>a>>b>>c) {
	
	if(fabs(a) <= eps) {
		cout<<"Not quadratic equation";
	} else {
		double delta = b*b - 4*a*c;
		if(fabs(delta) <= eps) {
//			if(fabs(b) <= eps) cout<<fixed<<setprecision(2)<<"x1=x2="<<0.00;
//			else  cout<<fixed<<setprecision(2)<<"x1=x2="<<-b/(2*a);
			printf("x1=x2=%.2f",-b/(2*a));
		}else if(delta > eps) {
			//cout<<"x1="<<fixed<<setprecision(2)<<(-b-sqrt(delta))/(2*a)<<";x2="<<fixed<<setprecision(2)<<(-b+sqrt(delta))/(2*a);
			printf("x1=%.2f;x2=%.2f",(-b-sqrt(delta))/(2*a),(-b+sqrt(delta))/(2*a));
		} else {
			if(fabs(a) > eps) {
				if(fabs(b) <= eps){
					printf("x1=%.2f%.2fi;",0.00,-sqrt(-delta)/(2*a));
					printf("x2=%.2f+%.2fi",0.00,sqrt(-delta)/(2*a));
				} else {
					printf("x1=%.2f%.2fi;",(-b)/(2*a),-sqrt(-delta)/(2*a));
					printf("x2=%.2f+%.2fi",(-b)/(2*a),sqrt(-delta)/(2*a));
				}
		} else {
			if(fabs(b) <= eps){
				printf("x1=%.2f%.2fi;",0.00,sqrt(-delta)/(2*a));
				printf("x2=%.2f+%.2fi",0.00,-sqrt(-delta)/(2*a));
			} else {
				printf("x1=%.2f%.2fi;",(-b)/(2*a),sqrt(-delta)/(2*a));
				printf("x2=%.2f+%.2fi",(-b)/(2*a),-sqrt(-delta)/(2*a));
			}
		}		
	}
}

	cout<<endl;
}

	return 0;
}
 

 





 

题目描述

从键盘输入5个学生的成绩(整数),求他们的平均成绩(浮点数,保留一位小数)。

输入描述:

一行,连续输入5个整数(范围0~100),用空格分隔。

输出描述:

一行,输出5个数的平均数(保留一位小数)。

示例1

输入

75 80 43 67 96

 

输出

72.2

 

AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <stdlib.h>
#include <algorithm>
#include <iomanip>
using namespace std;
 
int main()
{
    int sum = 0, t;
    for(int i=0;i<5;i++)
    {
        cin>>t;
        sum += t;
    }
    cout<<fixed<<setprecision(1)<<1.0*sum/5;
    return 0;
}


 



 

题目描述

KiKi学习了循环,BoBo老师给他出了一系列打印图案的练习,该任务是打印用“*”组成的翻转金字塔图案。

输入描述:

多组输入,一个整数(2~20),表示翻转金字塔边的长度,即“*”的数量,也表示输出行数。

输出描述:

针对每行输入,输出用“*”组成的金字塔,每个“*”后面有一个空格。

示例1

输入

5

 

输出

 

* * * * * 
 * * * * 
  * * * 
   * * 
    * 

示例2

输入

6

 

输出

 

* * * * * * 
 * * * * * 
  * * * * 
   * * * 
    * * 
     * 


AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <stdlib.h>
#include <algorithm>
using namespace std;
 
int main()
{
    int n;
     
     
while(cin>>n) {
     
    int cnt = n;
    int space = 0;
     
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<space;j++)
        {
            cout<<" ";
        }
        space++;
         
        for(int k=0;k<cnt;k++)
        {
            cout<<"* ";
        }
        cnt--;
         
        cout<<endl;
    }
     
}
 
    return 0;
}

 




 

题目描述

给出两幅相同大小的黑白图像(用0-1矩阵)表示,求它们的相似度。若两幅图像在相同位置上的像素点颜色相同,则称它们在该位置具有相同的像素点。两幅图像的相似度定义为相同像素点数占总像素点数的百分比。

 

输入描述:

第一行包含两个整数m和n,表示图像的行数和列数,用单个空格隔开。1≤m≤100, 1≤n≤100。之后m行,每行n个整数0或1,表示第一幅黑白图像上各像素点的颜色,相邻两个数用单个空格隔开。之后m行,每行n个整数0或1,表示第二幅黑白图像上各像素点的颜色,相邻两个数用单个空格隔开。

输出描述:

一个实数,表示相似度(以百分比的形式给出),精确到小数点后两位。

示例1

输入

 

3 3
1 0 1
0 0 1
1 1 0
1 1 0
0 0 1
0 0 1

输出

 

44.44

 

AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <stdlib.h>
#include <algorithm>
using namespace std;

int main()
{
	int h,l, count = 0;
	int a[100][100], b[100][100];
	cin>>h>>l;
	for(int i=0;i<h;i++)
	{
		for(int j=0;j<l;j++)
		{
			cin>>a[i][j];
			//cout<<a[i][j]<<endl;
		}
	}
	
	for(int i=0;i<h;i++)
	{
		for(int j=0;j<l;j++)
		{
			cin>>b[i][j];
			//cout<<b[i][j]<<endl;
		}
	}
	
	for(int i=0;i<h;i++)
	{
		for(int j=0;j<l;j++)
		{
			if(a[i][j] == b[i][j]) {
				count++;
			}
			//cout<<count<<endl;
		}
	}
	
	printf("%.2f\n",100.0*count/(h*l));
	return 0;
}


 





 

题目描述

输入n个成绩,换行输出n个成绩中最高分数和最低分数的差。

输入描述:

两行,第一行为n,表示n个成绩,不会大于10000。

第二行为n个成绩(整数表示,范围0~100),以空格隔开。

输出描述:

一行,输出n个成绩中最高分数和最低分数的差。

示例1

输入

 

10
98 100 99 97 95 99 98 97 96 100

输出

 

5

AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <stdlib.h>
#include <algorithm>
using namespace std;

int main()
{
	int n, max = -1, min = 1000, t; //这里的最低分初始化要注意
	
while(cin>>n) {
	for(int i=0;i<n;i++)
	{
		cin>>t;
		if(t>max) max=t;
		if(t<min) min=t;
	}
	
	cout<<max-min<<endl;
}
	return 0;
}


 


 

题目描述

KiKi知道什么叫杨辉三角之后对杨辉三角产生了浓厚的兴趣,他想知道杨辉三角的前n行,请编程帮他解答。杨辉三角,本质上是二项式(a+b)的n次方展开后各项的系数排成的三角形。其性质包括:每行的端点数为1, 一个数也为1;每个数等于它左上方和上方的两数之和。

输入描述:

第一行包含一个整数数n。 (1≤n≤30)

输出描述:

 

包含n行,为杨辉三角的前n行,每个数输出域宽为5。

示例1

输入

6

 

输出

 

    1
    1    1
    1    2    1
    1    3    3    1
    1    4    6    4    1
    1    5   10   10    5    1




AC代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <stdlib.h>
#include <algorithm>
using namespace std;

int main()
{
	int n;
	int a[1000][100];
	while(cin>>n) {
		for(int i=1;i<=n;i++)
		{
			a[i][1]=a[i][i]=1;
		}
		
		for(int i=3;i<=n;i++)
		{
			for(int j=2;j<i;j++)
			{
				a[i][j]=a[i-1][j-1]+a[i-1][j];
			}
		}
		
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=i;j++)
			printf("%5d",a[i][j]);
			cout<<endl;
		}
	}
			
	return 0;
}

 


 

 


这次的题目还是比较基础,细心读题就可以AC。
加油努力吧!

 

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

重剑DS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值