1005 博学楼的楼梯
众所周知,博学楼有11层楼,并且有三个电梯。假设电梯上升一层需要6秒,下降一层需要4秒,在每在一层停留需要3秒。电梯初始在1楼,现在给你电梯要去楼层顺序,例如3,2,5代表电梯从1楼到达三楼,再从三楼到达2楼,再从2楼到达5楼。问通过这些操作,电梯需要花多少时间?例如3,2,5,从1楼到3楼需要2 * 6秒,然后停留3秒,再从3楼到2楼需要4秒,再停留3秒,再由2楼到5楼需要3 * 6秒,停留3秒。所以总共需要2 * 6 + 3 + 4 + 3 + 3 * 6 + 3 = 43。如果上次要停留的楼层与这次相同,则只需要再停留3秒即可。
输入输出格式
输入描述:
输入:输入一个整数T(1 <= T <= 100),代表有T组样例。
每组样例有一个整数n(1 <= n <= 100),代表有n层楼是电梯需要去的。然后给出n个整数,给出的整数小于等于11,代表电梯到达楼层的顺序。
输出描述:
输出:对每组样例,输出一个整数,代表今天博学楼开放教室的总数。每个输出结果后面均包含换行符。
#include<bits/stdc++.h>
using namespace std;
int main() {
int T, n, num;
cin >> T;
int ans = 0;
//定义一个临时变量接收上次停留的楼层
int temp = 1;
while (T > 0)
{
cin >> n;
while (cin >> num) {
if (num >temp)
{
ans = ans + (num - temp) * 6 +3 ;
temp = num;
}else if (num < temp)
{
ans = ans + (temp - num) * 4 + 3;
temp = num;
}
else {
ans = ans + 3;
}
n--;
//当n=0时
if (n == 0) {
break;
}
}
cout << ans << endl;;
T--;
}
return 0;
}
1016 字符分类
从键盘上输入一个字符串,将该字符串分为数字、字母、其他字符三个部分输出。
例如:
输入:Thdad29#@12aH;77
输出:
ThdadaH
291277
#@;
#include <iostream>
using namespace std;
#include<string>
#include<vector>
int main(){
string s;
vector<char> letter, number, v3;
getline(cin, s);
for (int i = 0; i < s.length(); i++)
{
if ((s[i]>='a'&&s[i]<='z')|| (s[i] >= 'A' && s[i] <= 'Z')) {
letter.push_back(s[i]);
}
else if (s[i] >= '0' && s[i] <= '9') {
number.push_back(s[i]);
}
else {
v3.push_back(s[i]);
}
}
for (int i = 0; i < letter.size(); i++)
{
cout << letter[i];
}
cout << endl;
for (int i = 0; i < number.size(); i++)
{
cout <<number[i];
}
cout << endl;
for (int i = 0; i < v3.size(); i++)
{
cout << v3[i];
}
cout << endl;
return 0;
}
1380 二进制数
大家都知道,数据在计算机里中存储是以二进制的形式存储的。 有一天,小明学了C语言之后,他想知道一个类型为unsigned int 类型的数字,存储在计算机中的二进制串是什么样子的。 你能帮帮小明吗?并且,小明不想要二进制串中前面的没有意义的0串,即要去掉前导0。
#include<iostream>
using namespace std;
#include<vector>
#include<string>
//转二进制数
int main() {
int num;
cin >> num;
//声明一个v1数组来接收num转换的二进制数
vector<int> v1;
vector<int> v2;
while (num>0)
{
v1.push_back(num % 2);
num = num / 2;
}
for (int i = v1.size()-1; i >=0; i--)
{
v2.push_back(v1[i]);
}
int j = 0;
while (v2[j]==0) {
j++;
}
for ( j ; j < v2.size(); j++)
{
cout << v2[j];
}
return 0;
}
KY15 abc
#include<bits/stdc++.h>
using namespace std;
void abc() {
int a = 0, b = 0, c = 0;
for (a; a <= 9; a++) {
b = 0;
for (b; b <= 9; b++) {
c = 0;
for (c; c <= 9; c++) {
if ((a * 100 + b * 10 + c + b * 100 + c * 10 + c) == 532)
{
cout << a << " " << b << " " << c << endl;
}
}
}
}
}
int main() {
abc();
return 0;
}
KY72 Digital Roots
#include<bits/stdc++.h>
#include <string>
using namespace std;
int main() {
string num;
while (getline(cin, num)) {
//假如输入的数字直接为个位数 直接输出
if (num.length() == 1) {
cout << num << endl;
continue;
}
else {
//设置temp接收数字中每一位数的和
int temp = 0;
//string_temp为temp的字符串形式
string string_temp;
//当string_temp的长度不为1时,反复处理
while (string_temp.length() != 1) {
temp = 0;
for (int i = 0; i < num.length(); i++) {
temp = temp + num[i] - '0';
}
string_temp = to_string(temp);
num = string_temp;
}
cout << temp << endl;
}
}
return 0;
}
KY115 后缀子串排序
#include<bits/stdc++.h>
using namespace std;
//手写冒泡排序
void bubble_sort(vector<string>& arr, int len) {
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j < len - i - 1; j++) {
//按首字母进行从小到大排序
if (arr[j][0] > arr[j + 1][0]) {
string temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
//若首字母相同,则按第二个字母进行排序.
if (arr[j][0] == arr[j + 1][0] && arr[j][1] > arr[j + 1][1]) {
string temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
//假如需要降序排序的话 按我这么写
bool compare(string v1, string v2) {
return v1 > v2;
}
int main() {
string s;
cin >> s;
vector<string> temp;
for (int i = 0; i < s.length(); i++)
{
temp.push_back(s.substr(i, s.length() - i));
}
//bubble_sort(temp, s.length()); 这道题这样写其实是欺骗了测试案例 我没有写到当第一第二个字母都相同的情况
sort(temp.begin(),temp.end(),compare);
for (int i = 0; i < s.length(); i++)
{
cout << temp[i] << endl;
}
return 0;
}