做题笔记15

本文通过六个编程题目,探讨了算法在不同场景下的应用,包括指数增长、最短路径、组合计数、数组操作、回文串构建以及模拟计算器的实现。每个题目都附带了代码实现和反思,强调了类型选择、精度控制、内存初始化、复杂度优化等关键点,旨在提升读者的算法思维和编程技巧。
摘要由CSDN通过智能技术生成

A、log2(N)

题目描述

在这里插入图片描述

代码

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
	long n;
	cin >> n;
	long k = 0;
	long sum = 1;
	while (1)
	{
		sum *= 2;
		k++;
		if (sum > n)
		{
			cout << k - 1 << endl;
			break;
		}
	}
}

反思

1)幂增长速度很快,需要注意数据的类型。可用unsigned long long;
2)“ *=2 ”与“<<=1”作用相同;

B、如何溜的最快

题目描述

在这里插入图片描述

代码

#include <iostream>
#include <math.h>
#include <stdio.h>
using namespace std;

int main()
{
	int r, x, y;
	while(scanf("%d %d %d",&r,&x,&y) != EOF)
    {
	int dis = x * x + y * y;
	int an = sqrt(dis) / r;
	if(an==0)
    {
        an+=1;
    }
	if (sqrt(dis) - r * an != 0)
	{
		cout << an + 1 << endl;
	}
	else
	{
		cout << an << endl;
	}
	}
}

反思

1)为浮点数加上偏置,如:1e-8,能提高浮点数的精度;
2)pace += pace ? 1 : 2; 表达的意思为,如果pace的值为0,则pace=pace+2;若pace不为0,则pace=pace+1;

C、200和整数对之间的情缘

题目描述

在这里插入图片描述

代码

#include<iostream>
#include<stack>
using namespace std;

int num[200];

long long nums(int n)
{
	long long num = 1;
	for (int i = 1; i <= n; i++)
	{
		num *= i;
	}
	return num;
}

int main()
{	
	int t = 5;
	for (int i = 0; i < 200; i++)
	{
		num[i] = 0;
	}
	int n;
	long long ans = 0;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		int t;
		cin >> t;
		num[t % 200]++;
	}
	for (int i = 0; i < 200; i++)
	{
		if (num[i] > 1)
		{
			
			ans += num[i] * (num[i] - 1) / nums(2);
		}
	}
	cout << ans << endl;
}

反思

memset()
memset 函数的第三个参数 n 的值一般用 sizeof() 获取,这样比较专业。注意,如果是对指针变量所指向的内存单元进行清零初始化,那么一定要先对这个指针变量进行初始化,即一定要先让它指向某个有效的地址。而且用memset给指针变量如p所指向的内存单元进行初始化时,n 千万别写成 sizeof§,这是新手经常会犯的错误。因为 p 是指针变量,不管 p 指向什么类型的变量,sizeof§ 的值都是 4。

D、两面包夹芝士

题目描述

在这里插入图片描述

代码


#include<iostream>
using namespace std;

int main()
{
	int n;
	cin >> n;
	int* a = new int[n];
	int* b = new int[n];
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	for (int i = 0; i < n; i++)
	{
		cin >> b[i];
	}
	int max = -9999999;
	int min = 999999;
	for (int i = 0; i < n; i++)
	{
		if (a[i] > max)
		{
			max = a[i];
		}
	}
	for (int i = 0; i < n; i++)
	{
		if (b[i] < min)
		{
			min = b[i];
		}
	}
	if (min > max)
	{
		cout << min - max + 1 << endl;
	}
	else
	{
		cout << 0 << endl;
	}
}

反思

需要初始化最大最小值的时候,可以通过16进制表达该类型的最大最小值。

E、构造回文串

题目描述

在这里插入图片描述

代码

#include<iostream>
using namespace std;

int main()
{
	int n;
	cin >> n;
	int* a = new int[n];
	int* b = new int[n];
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	for (int i = 0; i < n; i++)
	{
		cin >> b[i];
	}
	int max = -9999999;
	int min = 999999;
	for (int i = 0; i < n; i++)
	{
		if (a[i] > max)
		{
			max = a[i];
		}
	}
	for (int i = 0; i < n; i++)
	{
		if (b[i] < min)
		{
			min = b[i];
		}
	}
	if (min > max)
	{
		cout << min - max + 1 << endl;
	}
	else
	{
		cout << 0 << endl;
	}
}

反思

bool Judge(char *buf)
{
    int cntz = 0, l, r;
    for(l = 0; buf[l] && buf[l] == '0'; l ++)
        cntz ++;
    for(r = strlen(buf) - 1; r >= 0 && buf[r] == '0'; r --)
        cntz --;
    if(cntz > 0) return false;
    for(; l < r; l ++, r --)
        if(buf[l] != buf[r]) return false;
    return true;
}

开始时的两个for循环,能够判断字符串前后的0的个数,也在同时将left,right定位在非0处,相当于删去字符串前后的0。

F、模拟计算器

题目描述

在这里插入图片描述

代码

#include<iostream>
#include<stack>
using namespace std;

int main()
{
	int n;
	cin >> n;
	int no = n - 1;
	stack<long long>num;
	stack<char>op;
	while (n--)
	{
		long long t;
		cin >> t;
		num.push(t);
	}
	while (no--)
	{
		char t;
		cin >> t;
		op.push(t);
	}
	while (!op.empty())
	{
		char t1;
		t1 = op.top();
		op.pop();
		if (!op.empty())
		{
			char t2;
			t2 = op.top();
			op.pop();
			if ((t1 == '+' || t1 == '-') && (t2 == '-' || t2 == '*'))
			{
				if (t2 == '*')
				{
					op.push(t1);
					long long n1, n2, n3;
					n1 = num.top();
					num.pop();
					n2 = num.top();
					num.pop();
					n3 = num.top();
					num.pop();
					n2 = n3 * n2;

					num.push(n2);
					num.push(n1);
				}
				else if (t1 == '-' && t2 == '-')
				{
					op.push(t1);
					long long n1, n2;
					n1 = num.top();
					num.pop();
					n2 = num.top();
					num.pop();
					n2 += n1;
					num.push(n2);
				}
				else if (t1 == '+' && t2 == '-')
				{
					op.push(t2);
					long long n1, n2;
					n1 = num.top();
					num.pop();
					n2 = num.top();
					num.pop();
					n2 -= n1;
					num.push(n2);
				}
			}

			else
			{
				op.push(t2);
				long long n1, n2;
				n1 = num.top();
				num.pop();
				n2 = num.top();
				num.pop();
				if (t1 == '+')
				{
					n2 = n2 + n1;
				}
				else if (t1 == '-')
				{
					n2 = n2 - n1;
				}
				else if (t1 == '*')
				{
					n2 = n2 * n1;
				}
				num.push(n2);
			}
		}
		else
		{
			long long n1, n2;
			n1 = num.top();
			num.pop();
			n2 = num.top();
			num.pop();
			if (t1 == '+')
			{
				n2 = n2 + n1;
			}
			else if (t1 == '-')
			{
				n2 = n2 - n1;
			}
			else if (t1 == '*')
			{
				n2 = n2 * n1;
			}

			num.push(n2);
		}
	}

	while (!num.empty())
	{
		cout << num.top();
		num.pop();
	}
}

反思

将数字与符号全都先入栈再根据符号出栈的方法,需要考虑当前符号与下一个符号。若当前符号不为乘号且下一个符号为减号时,相当于加上了小括号提高优先级,需要注意变号的问题,该解决方法较为繁琐。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值