C. Letters

C. Letters

链接: link.

问题描述

有 n 栋楼,标号为1到 n,第i 栋楼,有的房间数为 ai。所有的房间数从1到 n 排序。由所给的每栋楼的房间数和在全部房间排序后的序号,推断出这个房间在哪栋楼的第几个房间。

样例分析

在这里插入图片描述
所给样例有3栋楼,第一栋楼有10个房间,第二栋楼有15个房间,第三栋楼有12个房间。序号为1的房间的具体位置在1栋楼的1个房间;序号为9的房间的具体位置在1栋楼的第9个房间;序号为12的房间的具体位置在第2栋楼的第2个房间(12>10因此对应15,为第二栋楼, 具体房间为12-10=2)……

大致思路

可以用两个队列来存储每栋楼的房间数目,以及房间在全部房间中的序号。之所以用队列是因为其先进先出的特点。大致的思想就是,比较完一个抛出一个,一直比较两队列的头元素。

代码如下:

#include<bits/stdc++.h>
#define int long long
using namespace std;

signed main(){
	int n,m;
	cin>>n>>m;
	queue<int> q1,q2;
	for(int i=0;i<n;i++){
		int x;
		cin>>x;
		q1.push(x);
	}
	for(int i=0;i<m;i++){
		int y;
		cin>>y;
		q2.push(y);
	}
	int f=1,w=0;
	while(!q2.empty()){
		if(q2.front()<=q1.front()){
			cout<<f<<" "<<((q2.front()>w)?(q2.front()-w):(q2.front()))<<endl;
			q2.pop();
		}
		else{
			w=q1.front();
			q1.pop();
			q1.front()+=w;
			f++;
		}
	}
} 

总结

学习和运用是两方面的事情,只有真正熟练运用才称之为学会。虽然之前学过了队列知识,但并未实际运用。通过这道题,我更加掌握了队列相关知识。

Design a program named letters_between.c that accepts a starting letter and target letter as input. The program should calculate the shortest path between the two given letters by either moving forwards or backwards through the alphabet. The program then prints out all the letters from the starting letter to the target letter along the shortest path. For example, if 'c' and 'k' were entered as the starting and target letters respectivley, the program would calculate the path moving forwards through the alphabet to be cdefghijk, which takes 9 letters and the the path moving backwards through the alphabet to be cbazyxwvutsrqponmlk, which takes 19 letters. Hence the program will print cdefghijk as 9 letters is less than 19. Note In the case where the number of letters are equal, the program will print the letters moving forwards through the alphabet. To ensure correct input handling, you might want to include a space before the %c specifier in the scanf function for reading characters to help clear any preceding whitespace characters or newline characters left in the input buffer from previous inputs. Examples dcc letters_between.c -o letters_between ./letters_between Please enter starting letter: H Please enter target letter: K HIJK ./letters_between Please enter starting letter: b Please enter target letter: w bazyxw ./letters_between Please enter starting letter: Q Please enter target letter: D QRSTUVWXYZABCD ./letters_between Please enter starting letter: m Please enter target letter: m m
最新发布
06-22
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值