N诺其他

华工机试

1798

记录
注意两个点
一个是sort对vector排序

sort(a.begin(),a.end());

一个是输入的获取

	while(scanf("%d",&n)!=EOF)
	{
		a.push_back(n);
		//cout<<n<<endl;
		if(getchar()=='\n')
			break;	
	}

1799

过不了,应该是有个输入一直不过
mark

#include<cstdio>
#include <vector>
#include <string>
#include <string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int n,m;

int pan(char a)
{
	if(a>='a'&&a<='z')
		return 1;
	if(a>='A'&&a<='Z')
		return 2;
	if(a>='1'&&a<='9')
		return 3;
}
int main()
{
	string s;
	while(getline(cin,s))
	{
		string s1="",s2="",s3="";
		int old=0,temp=0;
		for(int i=0;i<s.size();i++)
		{
			temp=pan(s[i]);
			if(temp==1) s1+=s[i];
			else if(temp==2) s2+=s[i];
			else if(temp==3)s3+=s[i];
			if(old!=temp)
			{
				if(old==1)s1+=" ";
				else if(old==2)s2+=" ";
				else if(old==3)s3+=" ";
			}
			old=temp;
		}
		if(temp==1)s1+=" ";
		else if(temp==2)s2+=" ";
		cout<<s1<<s2<<s3<<endl;
	}
	return 0;
}

1816

先判断日期差值,再计算日期,
闰年判断

if((date2.year%400==0)||(date2.year%4==0)&&(date2.year %100!=0))

1817

记录
找出原因了,没通过是因为不是连续输入,改成单独输入就可

while(cin>>n)

1820

凑零钱问题
这篇文章讲得很好
摘录:
自顶向下:比如经典的递归斐波那契数列
注意我们刚才画的递归树(或者说图),是从上向下延伸,都是从一个规模较大的原问题比如说 f(20),向下逐渐分解规模,直到 f(1) 和 f(2) 触底,然后逐层返回答案,这就叫「自顶向下」。
运用备忘录可以消除重复计算

自底向上:动态规划
我们直接从最底下,最简单,问题规模最小的 f(1) 和 f(2) 开始往上推,直到推到我们想要的答案 f(20),这就是动态规划的思路,这也是为什么动态规划一般都脱离了递归,而是由循环迭代完成计算。

状态转移方程
和上面的剪枝类似
其实状态转移方程直接代表着暴力解法

动态规划问题的判断
这个问题是动态规划问题,因为它具有「最优子结构」的。要符合「最优子结构」,子问题间必须互相独立

状态方程步骤
1、先确定「状态」,也就是原问题和子问题中变化的变量。由于硬币数量无限,所以唯一的状态就是目标金额 amount。
2、然后确定 dp 函数的定义:当前的目标金额是 n,至少需要 dp(n) 个硬币凑出该金额。
3、然后确定「选择」并择优,也就是对于每个状态,可以做出什么选择改变当前状态。具体到这个问题,无论当的目标金额是多少,选择就是从面额列表 coins 中选择一个硬币,然后目标金额就会减少
4、最后明确 base case,显然目标金额为 0 时,所需硬币数量为 0;当目标金额小于 0 时,无解,返回 -1:

1818

过不了oj
记录:
顶点的数量不是n

#include<cstdio>
#include <vector>
#include <string>
#include <string.h>
#include<iostream>
#include <queue>
#include<algorithm>
using namespace std;
const int maxn=1000+5;

int len[maxn];//记录顶点的度
vector<int>a[maxn];

void topu(int n)
{
	queue<int>q;
	//度为1的入队列
	for(int i=0;i<maxn;i++)
	{
		if(len[i]==1)
			q.push(i);
	}

	int flag=0;
	while(!q.empty())
	{
		int now=q.front ();
		q.pop();
		flag++;
		for(int j=0;j<a[now].size();j++)
		{	
			len[a[now][j]]--;
			if(len[a[now][j]]==1)
				q.push(a[now][j]);
		}	
	}
	if(flag!=n)
		cout<<"yes"<<endl;
	else
		cout<<"no"<<endl;
}

int main()
{
	int n;
	cin>>n;
	fill(len,len+maxn,0);
	//构造邻接表
	for(int i=0;i<n;i++)
	{
		int x,y;
		cin>>x>>y;
		if(x!=y){
			a[x].push_back (y);
			len[x]++;
			a[y].push_back (x);
			len[y]++;
		}
	}
	int ding=0;
	for(int i=0;i<maxn;i++)
		if(a[i].size()!=0)
			ding++;
	topu(ding);
	system("pause");
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

王蒟蒻

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

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

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

打赏作者

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

抵扣说明:

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

余额充值