程序设计与算法(一)C语言程序设计--第三周测试

001:奇偶数判断

#define _CRT_SECURE_NO_WARNINGS
#include <iostream> 
#include <cstdio>
using namespace std;
int main()
{
	int n;
	cin >> n;
	if (n % 2)
		cout << "odd" << endl;
	else
		cout << "even" << endl;
}

002:求一元二次方程的根

#define _CRT_SECURE_NO_WARNINGS
#include <iostream> 
#include <cstdio>
#include<cmath>
using namespace std;
double eps = 1e-10;
int main()
{
	double a, b, c, delta,rel,img;
	cin >> a >> b >> c;
	delta = b * b - 4 * a * c;
	if (delta < eps && -delta < eps) 
		printf("x1=x2=%.5f",-b / (2 * a));
	else if (delta < 0) {
		rel = -b / (2 * a);
		img = sqrt(-delta) / (2 * a);
		if (rel < eps && -rel < eps)
			rel = 0;
		printf("x1=%.5f+%.5fi;x2=%.5f-%.5fi", rel, img, rel, img);
	}
	else {
		printf("x1=%.5f;x2=%.5f", (-b + sqrt(delta)) / (2 * a), (-b - sqrt(delta)) / (2 * a));
	}
	return 0;
}

003:点和正方形的关系

#define _CRT_SECURE_NO_WARNINGS
#include <iostream> 
#include <cstdio>
#include<cmath>
using namespace std;
double eps = 1e-10;
int main()
{
	int x, y;
	cin >> x >> y;
	if (x <= 1 && x >= -1 && y <= 1 && y >= -1)
		printf("yes");
	else
		cout << "no";
	return 0;
}

004:苹果和虫子2

#define _CRT_SECURE_NO_WARNINGS
#include <iostream> 
#include <cstdio>
using namespace std;
int main()
{
	int n, x, y, eat,rem;
	cin >> n >> x >> y;
	eat = y / x;
	if (y % x)
		eat++;
	rem = eat < n ? n-eat : 0;
	cout << rem;
	return 0;

}

005:简单计算器

#define _CRT_SECURE_NO_WARNINGS
#include <iostream> 
#include <cstdio>
using namespace std;
int main()
{
	int x, y;
	char oper;
	cin >> x >> y >> oper;
	switch (oper)
	{
	case '+':
		cout << x + y;
		break;
	case '-':
		cout << x - y;
		break;
	case '*':
		cout << x * y;
		break;
	case '/':
		if (y)
			cout << x / y;
		else
			cout << "Divided by zero!";
		break;
	default:
		cout << "Invalid operator!";
		break;
	}
	return 0;
}

006:求整数的和与均值

#define _CRT_SECURE_NO_WARNINGS
#include <iostream> 
#include <cstdio>
using namespace std;
int main()
{
	int n,sum=0,a;
	double average;
	cin >> n;
	for (int i = 0;i < n;i++) {
		cin >> a;
		sum += a;
	}
	printf("%d %.5f", sum, double(sum) / n);
}

007:整数序列的元素最大跨度值

#define _CRT_SECURE_NO_WARNINGS
#include <iostream> 
#include <cstdio>
using namespace std;
int main()
{
	int n,a,max=0,min=1001;
	cin >> n;
	for (int i = 0;i < n;i++) {
		cin >> a;
		max = max > a ? max : a;
		min = min > a ? a : min;
	}
	cout<<max-min;
}

008:奥运奖牌计数

#define _CRT_SECURE_NO_WARNINGS
#include <iostream> 
#include <cstdio>
using namespace std;
int main()
{
	int n,a,b,c,gold=0, silver=0, brass=0;
	cin >> n;
	for (int i = 0;i < n;i++) {
		cin >> a >> b >> c;
		gold += a;
		silver += b;
		brass += c;
	}
	printf("%d %d %d %d", gold , silver , brass , gold + silver + brass);
	return 0;
}

009:乘方计算

#define _CRT_SECURE_NO_WARNINGS
#include <iostream> 
#include <cstdio>
using namespace std;
int main()
{
	int a, n,result=1;
	cin >> a >> n;
	for (int i = 0;i < n;i++)
		result *= a;
	cout << result;
	return 0;
}

010:鸡尾酒疗法

#define _CRT_SECURE_NO_WARNINGS
#include <iostream> 
#include <cstdio>
using namespace std;
int main()
{
	int n, total, valid;
	double cocktail, perc;
	cin >> n >> total >> valid;
	cocktail = double(valid) / total;
	for (int i = 0;i < n - 1;i++) {
		cin >> total >> valid;
		perc = double(valid) / total;
		if (perc - cocktail > 0.05)
			cout << "better" << endl;
		else if ( cocktail-perc > 0.05)
			cout << "worse" << endl;
		else
			cout << "same" << endl;
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值