【全】程序设计与算法(一)测验汇总(2021夏季)

001:输出第二个整数

在这里插入图片描述

#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
   
	int a, b, c;
	scanf("%d %d %d", &a, &b, &c);
	printf("%d", b);
	return 0;
}

002:字符菱形

在这里插入图片描述

#include <iostream>
using namespace std;
int main()
{
   
	char c;
	cin >> c;
	cout << "  " << c << "\n";
	cout << " " << c << c << c << "\n";
	cout << c << c << c << c << c << "\n";
	cout << " " << c << c << c << "\n";
	cout << "  " << c;
	return 0;
}

003:打印ASCII码

在这里插入图片描述

#include <iostream>
using namespace std;
int main()
{
   
	int c;
	scanf("%c", &c);
	printf("%d", c);
	return 0;
}

004:打印字符

在这里插入图片描述

#include <iostream>
using namespace std;
int main()
{
   
	int c;
	scanf("%d", &c);
	printf("%c\n", c);
	return 0;
}

005:整型数据类型存储空间大小

在这里插入图片描述

#include <iostream>
using namespace std;
int main()
{
   
	int a;
	short b;
	cout << sizeof(a) << " " << sizeof(b);
	return 0;
}

006:浮点型数据类型存储空间大小

在这里插入图片描述

#include <iostream>
using namespace std;
int main()
{
   
	float a;
	double b;
	cout << sizeof(a) << " " << sizeof(b);
	return 0;
}

007:对齐输出

在这里插入图片描述

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
   
	int a, b, c;
	scanf("%d %d %d", &a, &b, &c);
	printf("%8d %8d %8d", a, b, c);
	return 0;
}

008:输出保留12位小数的浮点数

在这里插入图片描述

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
   
	double x;
	scanf("%lf", &x);
	printf("%.12f", x);
	return 0;
}

009:空格分隔输出

在这里插入图片描述

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
   
	char a;
	int b;
	float c;
	double d;
	scanf("%c %d %f %lf", &a, &b, &c, &d);
	printf("%c %d %.6f %.6f", a, b, c, d);
	return 0;
}

010:计算球的体积

在这里插入图片描述

#include <cstdio>
#include <iostream>
int main()
{
   
	double r, v;
	scanf("%lf", &r);
	v = 4.0 / 3 * 3.14 * r * r * r;
	printf("%.2f\n", v);
	return 0;
}

011:大象喝水

在这里插入图片描述

#include <iostream>
#include <cstdio>
#define PI 3.14159
using namespace std;
int main()
{
   
	int h, r, num;
	double v;
	scanf("%d %d", &h, &r);
	v = (PI * r * r * h) / 1000;
	num = 20 / v + 1;
	printf("%d", num);
	return 0;
}

012:奇偶数判断

在这里插入图片描述

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

013:求一元二次方程的根

在这里插入图片描述

#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
   
	double a, b, c; //系数
	double del, s;
	double x1, x2; // 两个解
	cin >> a >> b >> c;
	del = b * b - 4 * a * c;
	s = -b / (2 * a);
	if (abs(s) < 0.000001)
		s = 0.0;
	if (del > 0.000001)
		printf("x1=%.5f;x2=%.5f\n", s + sqrt(del) / (2 * a), s - sqrt(del) / (2 * a));
	else if (del < -0.000001)
		printf("x1=%.5f+%.5fi;x2=%.5f-%.5fi\n", s, sqrt(-del) / (2 * a), s, sqrt(-del) / (2 * a));
	else
		printf("x1=x2=%.5f\n", s);
	return 0;
}

014:点和正方形的关系

在这里插入图片描述

#include <iostream>
using namespace std;
int main()
{
   
	int x = 0, y = 0;
	scanf("%d %d", &x, &y);
	if (x >= -1 && x <= 1 && y >= -1 && y <= 1)
		printf("yes");
	else
		printf("no");
	return 0;
}

015:苹果和虫子2

在这里插入图片描述

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
   
	int n, x, y; //n苹果数,每x个小时能吃一个,y总时间
	scanf("%d%d%d", &n, &x, &y);
	if (y % x == 0 && n > y / x)
		printf("%d", n - y / x);
	else if (y % x > 0 && n > y / x)
		printf("%d", n - 1 - y / x);
	else
		printf("0");
	return 0;
}

016:简单计算器

在这里插入图片描述

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
   
	int x, y;
	char a;
	cin >> x >> y >> a;
	if (y != 0)
	{
   
		switch (int(a))
		{
   
		case 42:
			printf("%d", x * y);
			break;
		case 43:
			printf("%d", x + y);
			break;
		case 45:
			printf("%d", x - y);
			break;
		case 47:
			printf("%d", x / y);
			break;
		default:
			printf("Invalid operator!");
		}
	}
	else
		printf("Divided by zero!");
	return 0;
}

017:求整数的和与均值

在这里插入图片描述

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
   
	int n = 0, a = 0, sum = 0;
	scanf("%d", &n);
	int count = n;
	while (n != 0)
	{
   
		scanf("%d", &a);
		sum += a;
		n--;
	}
	printf("%d %.5f", sum, (double)(sum) / count);
	return 0;
}

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

在这里插入图片描述

#include <iostream>
using namespace std;
int main()
{
   
	int n, a, max = 0, min = 1000;
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
   
		scanf("%d", &a);
		if (a > max)
			max = a;
		if (a < min)
			min = a;
	}
	printf("%d", max - min);
	return 0;
}

019:奥运奖牌计数

在这里插入图片描述

#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
   
	int n, i, medal[3], total[4];
	// 初始化国家金银铜牌数组和合计数组
	for (i = 0; i < 3; i++)
	{
   
		medal[i] = 0;
		total[i] = 0;
	}
	total[3] = 0;
	// 读入数据
	cin >> n;
	for (i = 0; i < n; i++)
	{
   
		for (int j = 0; j < 3; j++)
		{
   
			cin >> medal[j];
			total[j] += medal[j];
		}
	}
	// 打印金银铜牌总数和奖牌总数
	for (i = 0; i < 3; i++)
	{
   
		printf("%d ", total[i]);
		total[3] += total[i];
	}
	printf("%d\n", total[3]);

	return 0;
}

020:乘方计算

在这里插入图片描述

#include <cstdio>
#include <iostream>
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;
}

021:鸡尾酒疗法

在这里插入图片描述

#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
   
	int n, a, b;
	double haart, newtreatment;
	cin >> n >> a >> b;
	haart = (double)b / a;
	for (int i = 0; i < n - 1; i++)
	{
   
		cin >> a >> b;
		newtreatment = (double)b / a;
		if (newtreatment - haart > 0.050001)
			cout << "better" << endl;
		else if (haart - newtreatment > 0.050001)
			cout << "worse" << endl;
		else
			cout << "same" << endl;
	}
	return 0;
}

022:角谷猜想

在这里插入图片描述

#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
   
	//中间过程是个巨大的整数,要声明成 long long int
	long long int n, result;
	cin >> n;
	while (n > 1)
	{
   
		if (n % 2)
		{
   
			result = n * 3 + 1;
			//long long int 输出时使用 %lld
			printf("%lld*3+1=%lld\n", n, result);
		}
		else
		{
   
			result = n / 2;
			printf("%lld/2=%lld\n", n, result);
		}
		n = result;
	}
	printf("End\n");
	return 0;
}

023:正常血压

在这里插入图片描述

#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
   
	int n, a, b, c = 0, d = 0;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
   
		cin >> a >> b;
		if (a >= 90 && a <= 140 && b >= 60 && b <= 90)
			c++;
		else
			c = <
  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值