2023.3.15上机练习

1.A+B和C (15)
时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小)
题目描述
给定区间[-231, 231]内的3个整数A、B和C,请判断A+B是否大于C。

输入描述:
输入第1行给出正整数T(<=10),是测试用例的个数。随后给出T组测试用例,每组占一行,顺序给出A、B和C。整数间以空格分隔。

输出描述:
对每组测试用例,在一行中输出“Case #X: true”如果A+B>C,否则输出“Case #X: false”,其中X是测试用例的编号(从1开始)。

输入例子:
4
1 2 3
2 3 4
2147483647 0 2147483646
0 -2147483648 -2147483647

输出例子:
Case #1: false
Case #2: true
Case #3: true
Case #4: false

#include<iostream>
using namespace std;
int main()
{
	long a, b, c;
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> a >> b >> c;
		if (a + b > c)
			cout << "Case #" << i << ": true" << endl;
		else
			cout<< "Case #" << i << ": false" << endl;
	}
	return 0;
}

2.求约数个数
超时…(暴力法无脑遍历真是不行啊)

#include<iostream>
using namespace std;
int main()
{
	int n;
	cin >> n;
	long num;
	for (int i = 0; i < n; i++)
	{
		cin >> num;
		int count = 0;
		for (int j = 1; j <= num; j++)
		{
			if (num % j == 0)
				count++;
		}
		cout << count << endl;
	}
	return 0;
}

正解

#include<iostream>
#include<cmath>
using namespace std;
//用开根号,将时间复杂度降为nlogn
int divisor_count(int x)
{
    int count = 0;
    int n = sqrt(x);
    for (int i = 1; i * i < x; i++)
        if (x % i == 0)
            count += 2;
    if (n * n == x)//sqrt(x)为整数,count*2 + 1;否则,count*2
        count++;
    return count;
}
int main()
{
    int n;
    while (cin >> n)
    {
        while (n--)
        {
            int x;
            cin >> x;
            cout << divisor_count(x) << endl;
        }
    }
    return 0;
}

3.在这里插入图片描述

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	char s;
	char a[21];
	int len = 0;
	while (cin >> s) 
	{
		a[len] = s;
		len++;
	}
	sort(a, a + len);
	for (int i = 0; i < len; i++)
		cout << a[i];
	return 0;
}

KY78 最大上升子序列和
题目
题解(8)
讨论(71)
排行
简单 通过率:40.71% 时间限制:1秒 空间限制:64M
知识点
动态规划
warning 校招时部分企业笔试将禁止编程题跳出页面,为提前适应,练习时请使用在线自测,而非本地IDE。
描述
一个数的序列bi,当b1 < b2 < … < bS的时候,我们称这个序列是上升的。对于给定的一个序列(a1, a2, …,aN),我们可以得到一些上升的子序列(ai1, ai2, …, aiK),这里1 <= i1 < i2 < … < iK <= N。比如,对于序列(1, 7, 3, 5, 9, 4, 8),有它的一些上升子序列,如(1, 7), (3, 4, 8)等等。这些子序列中序列和最大为18,为子序列(1, 3, 5, 9)的和. 你的任务,就是对于给定的序列,求出最大上升子序列和。注意,最长的上升子序列的和不一定是最大的,比如序列(100, 1, 2, 3)的最大上升子序列和为100,而最长上升子序列为(1, 2, 3)。
输入描述:
输入包含多组测试数据。 每组测试数据由两行组成。第一行是序列的长度N (1 <= N <= 1000)。第二行给出序列中的N个整数,这些整数的取值范围都在0到10000(可能重复)。
输出描述:
对于每组测试数据,输出其最大上升子序列和。
示例1
输入:
7
1 7 3 5 9 4 8
复制
输出:
18

#include<iostream>
using namespace std;
int main()
{
	int n;
	cin >> n;
	int num[10000],dp[10000];
	for (int i = 0; i < n; i++)
		cin >> num[i];
	int count = 0;
//        动态规划
	for (int i = 0; i < n; i++)
	{
		dp[i] = num[i];
		for (int j = 0; j < i; j++)
		{
			if (num[i] > num[j])
				dp[i] = max(dp[i], dp[j] + num[i]);
		}
		count = max(count, dp[i]);
	}
	cout << count << endl;
	return 0;
}
  1. 202006-1原题网址
    在这里插入图片描述
    在这里插入图片描述
#include<iostream>
using namespace std;
int main()
{
	int n, m;
	cin >> n >> m; //n个点,m个查询
	int x[1000], y[1000];
	char type[1000];
	for (int i = 0; i < n; i++)
		cin >> x[i] >> y[i] >> type[i];
	for (int i = 0; i < m; i++)
	{
		int z1, z2, z3;
		cin >> z1 >> z2 >> z3;
		int A, B;
		if (type[0] == 'A')
		{
			if ((z1 + z2 * x[0] + z3 * y[0]) > 0)
			{
				A = 1;
				B = -1;
			}
			else
			{
				A = -1;
				B = 1;
			}
		}
		else
		{
			if ((z1 + z2 *x[0] + z3 * y[0]) > 0)
			{
				A = -1;
				B = 1;
			}
			else
			{
				A = 1;
				B = -1;
			}
		}
		int t = 1;//代表多少点符合分类 
		for (int j = 1; j < n; j++)
		{
			if (type[j] == 'A')
			{
				if (((z1 + z2 * x[j] + z3 * y[j]) * A) > 0)
					t++;
				else
					break;
			}
			else if (type[j] == 'B')
			{
				if (((z1 + z2 * x[j] + z3 * y[j]) * B) > 0)
					t++;
				else
					break;
			}
		}
		if (n == t)
			cout << "Yes" << endl;
		else
			cout << "No" << endl;
	}
	return 0;
}
  1. 201403-2原题网址
#include<iostream>
#include<algorithm>

using namespace std;

struct windows
{
	int x1, x2, y1, y2;
	int number;//记录窗口的编号 
	int no;//记录窗口的层数 
};

bool cmp(windows a, windows b)
{
	if (a.no > b.no) {
		return true;
	}
	else {
		return false;
	}
}
int main() 
{
	int n, m;
	cin >> n >> m;
	windows w[n];
	for (int i = 0; i < n; i++)
	{
		cin >> w[i].x1 >> w[i].y1 >> w[i].x2 >> w[i].y2;
		w[i].number = i + 1;
		w[i].no = i + 1;
	}
	int x, y;
	for (int i = 0; i < m; i++)
	{
		cin >> x >> y;
		int f = 0;
		int tempt = 0;
		sort(w, w + n, cmp);//重写sort,变为降序排列
		for (int j = 0; j < n; j++) 
		{
			if (x >= w[j].x1 && x <= w[j].x2 && y >= w[j].y1 && y <= w[j].y2) 
			{
				cout << w[j].number << endl;
				f = 1;
				tempt = w[j].no;
				break;
			}
		}
		if (f == 0) 
		{
			cout << "IGNORED" << endl;
		}
		else if (tempt != n) {//表示该窗口不是顶层窗口时,更改窗口层数
			for (int j = 0; j < n; j++)
			{
				if (w[j].no > tempt)
				{
					w[j].no -= 1;
				}
				else if (w[j].no == tempt) 
				{
					w[j].no = n;
				}
			}
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值