HJ1 字符串最后一个单词的长度
描述
计算字符串最后一个单词的长度,单词以空格隔开,字符串长度小于5000。(注:字符串末尾不以空格为结尾)
输入描述:
输入一行,代表要计算的字符串,非空,长度小于5000。
输出描述:
输出一个整数,表示输入字符串最后一个单词的长度。
示例1
输入:
hello nowcoder
输出:
8
说明:
最后一个单词为nowcoder,长度为8
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
int length=0;
while (cin>>str) {
if (cin.get()=='\n')
{
length=str.size();
break;
}
}
cout<<length<<endl;
}
// 64 位输出请用 printf("%lld")
HJ2 计算某字符出现次数
描述
写出一个程序,接受一个由字母、数字和空格组成的字符串,和一个字符,然后输出输入字符串中该字符的出现次数。(不区分大小写字母)
数据范围: 1≤n≤1000 1≤n≤1000
输入描述:
第一行输入一个由字母、数字和空格组成的字符串,第二行输入一个字符(保证该字符不为空格)。
输出描述:
输出输入字符串中含有该字符的个数。(不区分大小写字母)
示例1
输入:
ABCabc
A
输出:
2
#include <iostream>
#include <iostream>
using namespace std;
int main() {
string str;
char ch;
int count=0;
getline(cin,str);
cin>>ch;
if(ch>64)//判断对比字符是否为字母
{
for(int i=0;i<str.size();i++)
{
if(str[i]==ch || str[i]+32==ch || str[i]-32==ch)
{
count++;
}
}
}
else
{
for(int i=0;i<str.size();i++)
{
if(str[i]==ch)
{
count++;
}
}
}
cout<<count<<endl;
}
// 64 位输出请用 printf("%lld")
HJ3 明明的随机数
描述
明明生成了NN个1到500之间的随机整数。请你删去其中重复的数字,即相同的数字只保留一个,把其余相同的数去掉,然后再把这些数从小到大排序,按照排好的顺序输出。
数据范围: 1≤n≤1000 1≤n≤1000 ,输入的数字大小满足 1≤val≤500 1≤val≤500
输入描述:
第一行先输入随机整数的个数 N 。接下来的 N 行每行输入一个整数,代表明明生成的随机数。具体格式可以参考下面的"示例"。
输出描述:
输出多行,表示输入数据处理后的结果
示例1
输入:
3
2
2
1
输出:
1
2
说明:
输入解释:
第一个数字是3,也即这个小样例的N=3,说明用计算机生成了3个1到500之间的随机整数,接下来每行一个随机数字,共3行,也即这3个随机数字为:
2
2
1
所以样例的输出为:
1
2
#include <iostream>
#include <string>
using namespace std;
int main() {
int N,val;//N是个数,n是数据
while(cin>>N)
{
bool a[501]={false};
while(N--)
{
cin>>val;
a[val]=true;
}
for (int i=1; i<501; i++) {
if (a[i]) {
cout<<i<<endl;
}
}
}
}
// 64 位输出请用 printf("%lld")
HJ4 字符串分隔 描述
•输入一个字符串,请按长度为8拆分每个输入字符串并进行输出;
•长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
输入描述:
连续输入字符串(每个字符串长度小于等于100)
输出描述:
依次输出所有分割后的长度为8的新字符串
示例1
输入:
abc
输出:
abc00000
#include <iostream>
#include <cmath>
using namespace std;
int main() {
long n;
cin>>n;
for (long i=2; i<=sqrt(n)&&i<=n; i++) {
while (n%i==0) {
cout<<i<<" ";
n/=i;
}
}
if (n-1) {//自己本身就是质数
cout<<n<<" ";
}
return 0;
}
// 64 位输出请用 printf("%lld")
HJ7 取近似值 描述
写出一个程序,接受一个正浮点数值,输出该数值的近似整数值。如果小数点后数值大于等于 0.5 ,向上取整;小于 0.5 ,则向下取整。
数据范围:保证输入的数字在 32 位浮点数范围内
输入描述:
输入一个正浮点数值
输出描述:
输出该数值的近似整数值
示例1
输入:
5.5
输出:
6
说明:
0.5>=0.5,所以5.5需要向上取整为6
示例2
输入:
2.499
输出:
2
说明:
0.499<0.5,2.499向下取整为2
#include <iostream>
using namespace std;
int main() {
float x;
cin>>x;
cout<<(int)(x+0.5)<<endl;
return 0;
}
// 64 位输出请用 printf("%lld")
HJ8 合并表记录
描述:
数据表记录包含表索引index和数值value(int范围的正整数),请对表索引相同的记录进行合并,即将相同索引的数值进行求和运算,输出按照index值升序进行输出。
提示:
0 <= index <= 11111111
1 <= value <= 100000
输入描述:
先输入键值对的个数n(1 <= n <= 500)
接下来n行每行输入成对的index和value值,以空格隔开
输出描述:
输出合并后的键值对(多行)
示例1
输入:
4
0 1
0 2
1 2
3 4
输出:
0 3
1 2
3 4
示例2
输入:
3
0 1
0 2
8 9
输出:
0 3
8 9
#include<iostream>
#include<map>
using namespace std;
int main(){
int n;
cin >> n;
map<int, int> mp; //有序哈希
for(int i = 0; i < n; i++){
int key, value;
cin >> key >> value;
if(mp.find(key) != mp.end()) //不是新的index,就累加到之前的哈希表中
mp[key] += value;
else //否则添加新的index
mp[key] = value;
}
map<int,int>::iterator iter;
for(iter = mp.begin(); iter != mp.end(); iter++)
cout << iter->first << " " << iter->second << endl;
return 0;
}
// 64 位输出请用 printf("%lld")
HJ9 提取不重复的整数
描述
输入一个 int 型整数,按照从右向左的阅读顺序,返回一个不含重复数字的新的整数。
保证输入的整数最后一位不是 0 。
数据范围: 1≤n≤108 1≤n≤108
输入描述:
输入一个int型整数
输出描述:
按照从右向左的阅读顺序,返回一个不含重复数字的新的整数
示例1
输入:
9876673
输出:
37689
#include <bits/stdc++.h>
#include <iostream>
#include <string>
using namespace std;
bool vis[10]={false};
int main() {
string n;
cin>>n;
reverse(n.begin(),n.end());
for (int i=0;i<n.size();i++) {
if (!vis[n[i]]) {
vis[n[i]]=true;
cout<<n[i];
}
}
return 0;
}
// 64 位输出请用 printf("%lld")
HJ10 字符个数统计
描述
编写一个函数,计算字符串中含有的不同字符的个数。字符在 ASCII 码范围内( 0~127 ,包括 0 和 127 ),换行表示结束符,不算在字符里。不在范围内的不作统计。多个相同的字符只计算一次
例如,对于字符串 abaca 而言,有 a、b、c 三种不同的字符,因此输出 3 。
数据范围: 1≤n≤500 1≤n≤500
输入描述:
输入一行没有空格的字符串。
输出描述:
输出 输入字符串 中范围在(0~127,包括0和127)字符的种数。
示例1
输入:
abc
输出:
3
示例2
输入:
aaa
输出:
1
#include <iostream>
using namespace std;
int main() {
string str;
cin>>str;
int len = str.size();
int count =0;
bool A[128]={false};
for (int i=0; i<len; i++) {
if (!A[str[i]]) {
A[str[i]]=true;
count++;
}
}
cout<<count<<endl;
}
// 64 位输出请用 printf("%lld")
HJ11 数字颠倒
输入描述:
输入一个int整数
输出描述:
将这个整数以字符串的形式逆序输出
示例1
输入:
1516000
输出:
0006151
示例2
输入:
0
输出:
0
#include <bits/stdc++.h>
#include <iostream>
#include <string>
using namespace std;
int main() {
int N;
cin>>N;
string str=to_string(N);
reverse(str.begin(),str.end());
cout<<str;
}
// 64 位输出请用 printf("%lld")
HJ12 字符串反转
描述
接受一个只包含小写字母的字符串,然后输出该字符串反转后的字符串。(字符串长度不超过1000)
输入描述:
输入一行,为一个只包含小写字母的字符串。
输出描述:
输出该字符串反转后的字符串。
示例1
输入:
abcd
输出:
dcba
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int main() {
string str;
cin>>str;
reverse(str.begin(),str.end());
cout<<str<<endl;
}
// 64 位输出请用 printf("%lld")
HJ13 句子逆序
描述
将一个英文语句以单词为单位逆序排放。例如“I am a boy”,逆序排放后为“boy a am I”
所有单词之间用一个空格隔开,语句中除了英文字母外,不再包含其他字符
数据范围:输入的字符串长度满足 1≤n≤1000 1≤n≤1000
注意本题有多组输入
输入描述:
输入一个英文语句,每个单词用空格隔开。保证输入只包含空格和字母。
输出描述:
得到逆序的句子
示例1
输入:
I am a boy
输出:
boy a am I
示例2
输入:
nowcoder
输出:
nowcoder
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int main() {
vector<string> cur;
string s;
while(cin>>s)
{
//遇到空格或回车,就将当前输入的字符串添加到vector容器尾部
cur.push_back(s);//在vector容器尾部添加一个元素
}
reverse(cur.begin(),cur.end());
for (int i=0; i<cur.size(); i++) {
if (i) {//单词之间用空格隔开,第一个单词前面不需要空格
cout<<' ';
}
cout<<cur[i];
}
cout<<endl;
return 0;
}
// 64 位输出请用 printf("%lld")
HJ14 字符串排序
描述
给定 n 个字符串,请对 n 个字符串按照字典序排列。
数据范围: 1≤n≤1000 1≤n≤1000 ,字符串长度满足 1≤len≤100 1≤len≤100
输入描述:
输入第一行为一个正整数n(1≤n≤1000),下面n行为n个字符串(字符串长度≤100),字符串中只含有大小写字母。
输出描述:
数据输出n行,输出结果为按照字典序排列的字符串。
示例1
输入:
9
cap
to
cat
card
two
too
up
boat
boot
输出:
boat
boot
cap
card
cat
to
too
two
up
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
vector<string> cur;
cur.resize(n);
for (int i=0; i<n; i++) {
cin>>cur[i];
}
sort(cur.begin(),cur.end());
for (int i=0; i<n; i++) {
cout<<cur[i]<<endl;
}
}
// 64 位输出请用 printf("%lld")
HJ15 求int型正整数在内存中存储时1的个数
描述
输入一个 int 型的正整数,计算出该 int 型数据在内存中存储时 1 的个数。
数据范围:保证在 32 位整型数字范围内
输入描述:
输入一个整数(int类型)
输出描述:
这个数转换成2进制后,输出1的个数
示例1
输入:
5
输出:
2
示例2
输入:
0
输出:
0
//#include <bitset>
#include <iostream>
using namespace std;
int main() {
//int n;
//cin>>n;
//bitset<32> b(n);
//cout<<b.count();
int x;
cin>>x;
int count=0;
while(x){
count +=x&1;
x>>=1;
}
cout<<count<<endl;
return 0;
}
// 64 位输出请用 printf("%lld")
HJ21 简单密码
描述
现在有一种密码变换算法。
九键手机键盘上的数字与字母的对应: 1--1, abc--2, def--3, ghi--4, jkl--5, mno--6, pqrs--7, tuv--8 wxyz--9, 0--0,把密码中出现的小写字母都变成九键键盘对应的数字,如:a变成 2,x 变成 9.
而密码中出现的大写字母则变成小写之后往后移一位,如:X ,先变成小写,再往后移一位,变成了 y ,例外:Z 往后移是 a 。
数字和其它的符号都不做变换。
数据范围: 输入的字符串长度满足 1≤n≤100 1≤n≤100
输入描述:
输入一组密码,长度不超过100个字符。
输出描述:
输出密码变换后的字符串
示例1
输入:
YUANzhi1987
输出:
zvbo9441987
#include <iostream>
using namespace std;
string Password_Transformation(string str)
{
char a[]={"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"};
char b[]={"bcdefghijklmnopqrstuvwxyza222333444555666777788899990123456789"};
int len =str.size();
for (int i=0; i<len; i++) {
//lower character to number
if ((str[i]>='a')&&(str[i]<='z')) {
str[i]=b[str[i]-'a'+26];
}
//upper character to lower character
if ((str[i]>='A')&&(str[i]<='Z')) {
str[i]=b[str[i]-'A'];
}
//number remains unchanged
}
return str;
}
int main() {
string str;
while (getline(cin,str)) { // 注意 while 处理多个 case
cout << Password_Transformation(str) << endl;
}
}
// 64 位输出请用 printf("%lld")
HJ22 汽水瓶
描述
某商店规定:三个空汽水瓶可以换一瓶汽水,允许向老板借空汽水瓶(但是必须要归还)。
小张手上有n个空汽水瓶,她想知道自己最多可以喝到多少瓶汽水。
数据范围:输入的正整数满足 1≤n≤100 1≤n≤100
注意:本题存在多组输入。输入的 0 表示输入结束,并不用输出结果。
输入描述:
输入文件最多包含 10 组测试数据,每个数据占一行,仅包含一个正整数 n( 1<=n<=100 ),表示小张手上的空汽水瓶数。n=0 表示输入结束,你的程序不应当处理这一行。
输出描述:
对于每组测试数据,输出一行,表示最多可以喝的汽水瓶数。如果一瓶也喝不到,输出0。
示例1
输入:
3
10
81
0
输出:
1
5
40
说明:
样例 1 解释:用三个空瓶换一瓶汽水,剩一个空瓶无法继续交换
样例 2 解释:用九个空瓶换三瓶汽水,剩四个空瓶再用三个空瓶换一瓶汽水,剩两个空瓶,向老板借一个空瓶再用三个空瓶换一瓶汽水喝完得一个空瓶还给老板
#include <iostream>
using namespace std;
int main() {
int n;
while(cin>>n)
{
if (!n) {
break;
}
cout<<(n/2)<<endl;
}
return 0;
}
// 64 位输出请用 printf("%lld")
HJ23 删除字符串中出现次数最少的字符
描述
实现删除字符串中出现次数最少的字符,若出现次数最少的字符有多个,则把出现次数最少的字符都删除。输出删除这些单词后的字符串,字符串中其它字符保持原来的顺序。
数据范围:输入的字符串长度满足 1≤n≤20 1≤n≤20 ,保证输入的字符串中仅出现小写字母
输入描述:
字符串只包含小写英文字母, 不考虑非法输入,输入的字符串长度小于等于20个字节。
输出描述:
删除字符串中出现次数最少的字符后的字符串。
示例1
输入:
aabcddd
输出:
aaddd
#include <map>
#include <iostream>
using namespace std;
int main() {
string str;
cin>>str;
int len =str.size();
map<char,int> mp;
//找到字符传中出现次数最小的字符
for(int i=0;i<len;i++){
//记录字符出现的次数
mp[str[i]]++;//如果map[i]没有被定义过,则map[i]=0 。
}
int mn=len;
for (auto x:mp) {
mn=min(mn,x.second);
}
for (auto c:str) {
if (mp[c]==mn) {
continue;
}
cout<<c;
}
return 0;
}
// 64 位输出请用 printf("%lld")
HJ31 单词倒排
描述
对字符串中的所有单词进行倒排。
说明:
1、构成单词的字符只有26个大写或小写英文字母;
2、非构成单词的字符均视为单词间隔符;
3、要求倒排后的单词间隔符以一个空格表示;如果原字符串中相邻单词间有多个间隔符时,倒排转换后也只允许出现一个空格间隔符;
4、每个单词最长20个字母;
数据范围:字符串长度满足 1≤n≤10000 1≤n≤10000
输入描述:
输入一行,表示用来倒排的句子
输出描述:
输出句子的倒排结果
示例1
输入:
I am a student
输出:
student a am I
示例2
输入:
$bo*y gi!r#l
输出:
l r gi y bo
#include<bits/stdc++.h>
#include <cctype>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int main() {
vector<string> words;
string str;
getline(cin,str);
for (int i=0; i<str.size(); i++) {
if(!isalpha(str[i]))//其他字符转为空格
{
str[i]=' ';
}
}
//stringstream input;
//input<<str;
stringstream input(str);
while(input>>str)//按照空格分割字符串
words.push_back(str);//尾部插入字符串
for (int i=words.size()-1; i>0; i--) {
cout<<words[i]<<" ";
}
cout<<words[0];
return 0;
}
// 64 位输出请用 printf("%lld")
HJ34 图片整理
描述
Lily上课时使用字母数字图片教小朋友们学习英语单词,每次都需要把这些图片按照大小(ASCII码值从小到大)排列收好。请大家给Lily帮忙,通过代码解决。
Lily使用的图片使用字符"A"到"Z"、"a"到"z"、"0"到"9"表示。
数据范围:每组输入的字符串长度满足 1≤n≤1000 1≤n≤1000
输入描述:
一行,一个字符串,字符串中的每个字符表示一张Lily使用的图片。
输出描述:
Lily的所有图片按照从小到大的顺序输出
示例1
输入:
Ihave1nose2hands10fingers
输出:
0112Iaadeeefghhinnnorsssv
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
string str;
getline(cin,str);
sort(str.begin(), str.end());
cout<<str;
}
// 64 位输出请用 printf("%lld")
HJ35 蛇形矩阵
描述
蛇形矩阵是由1开始的自然数依次排列成的一个矩阵上三角形。
例如,当输入5时,应该输出的三角形为:
1 3 6 10 15
2 5 9 14
4 8 13
7 12
11
输入描述:
输入正整数N(N不大于100)
输出描述:
输出一个N行的蛇形矩阵。
示例1
输入:
4
输出:
1 3 6 10
2 5 9
4 8
7
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin>>N;
int i,j;
int first=1;
int temp;
for(i=1;i<=N;i++)
{
cout<<first<<' ';
temp=first;//记录此刻的当前值
for(j=i+1;j<=N;j++)
{
temp+=j;
cout<<temp<<' ';
}
cout<<endl;
first+=i;
}
}
// 64 位输出请用 printf("%lld")
HJ37 统计每个月兔子的总数
描述
有一种兔子,从出生后第3个月起每个月都生一只兔子,小兔子长到第三个月后每个月又生一只兔子。
例子:假设一只兔子第3个月出生,那么它第5个月开始会每个月生一只兔子。
一月的时候有一只兔子,假如兔子都不死,问第n个月的兔子总数为多少?
数据范围:输入满足 1≤n≤31 1≤n≤31
输入描述:
输入一个int型整数表示第n个月
输出描述:
输出对应的兔子总数
示例1
输入:
3
输出:
2
#include <iostream>
using namespace std;
int getSum(int n){
if (n==1 || n==2) {
return 1;
}
return getSum(n-1)+getSum(n-2);
}
int main() {
int n;
while(cin>>n){
cout<<getSum(n)<<endl;
}
return 0;
}
// 64 位输出请用 printf("%lld")
HJ40 统计字符
描述
输入一行字符,分别统计出包含英文字母、空格、数字和其它字符的个数。
数据范围:输入的字符串长度满足 1≤n≤1000 1≤n≤1000
输入描述:
输入一行字符串,可以有空格
输出描述:
统计其中英文字符,空格字符,数字字符,其他字符的个数
示例1
输入:
1qazxsw23 edcvfr45tgbn hy67uj m,ki89ol.\\/;p0-=\\][
输出:
26
3
10
12
#include <cctype>
#include <iostream>
#include <cstring>
using namespace std;
int main() {
string str;
getline(cin,str);
int len = str.size();
int Count[4]={0};
for (int i=0; i<len; i++) {
if (isalpha(str[i])) {
Count[0]++;
}
else if (str[i]==' ') {
Count[1]++;
}
else if ('0'<=str[i]&&str[i]<='9') {
Count[2]++;
}
else {
Count[3]++;
}
}
for (int i=0; i<4; i++) {
cout<<Count[i]<<endl;
}
}
// 64 位输出请用 printf("%lld")
HJ46 截取字符串
描述
输入一个字符串和一个整数 k ,截取字符串的前k个字符并输出
数据范围:字符串长度满足 1≤n≤1000 1≤n≤1000 , 1≤k≤n 1≤k≤n
输入描述:
1.输入待截取的字符串
2.输入一个正整数k,代表截取的长度
输出描述:
截取后的字符串
示例1
输入:
abABCcDEF
6
输出:
abABCc
示例2
输入:
bdxPKBhih
6
输出:
bdxPKB
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main() {
string str;
int N;
getline(cin, str);
cin>>N;
cout<<str.substr(0,N);
}
// 64 位输出请用 printf("%lld")
HJ58 输入n个整数,输出其中最小的k个
描述
输入n个整数,找出其中最小的k个整数并按升序输出
本题有多组输入样例
数据范围:1≤n≤1000 1≤n≤1000 ,输入的整数满足 1≤val≤10000 1≤val≤10000
输入描述:
第一行输入两个整数n和k
第二行输入一个整数数组
输出描述:
从小到大输出最小的k个整数,用空格分开。
示例1
输入:
5 2
1 3 5 7 2
输出:
1 2
#include <algorithm>
#include <iostream>
#include <bits/stdc++.h>
#include <vector>
using namespace std;
int main() {
int n,k;
while (cin>>n>>k) {
vector<int> num;
for (int i=0; i<n; i++) {
//逐个储存n个整数
int temp;
cin>>temp;
num.push_back(temp);
}
sort(num.begin(), num.end());
for(int i=0;i<k-1;i++)
{
//输出前k个数字
cout<<num[i]<<' ';
}
cout<<num[k-1]<<endl;
}
return 0;
}
// 64 位输出请用 printf("%lld")
HJ73 计算日期到天数转换
描述
根据输入的日期,计算是这一年的第几天。
保证年份为4位数且日期合法。
进阶:时间复杂度:O(n) O(n) ,空间复杂度:O(1) O(1)
输入描述:
输入一行,每行空格分割,分别是年,月,日
输出描述:
输出是这一年的第几天
示例1
输入:
2012 12 31
输出:
366
示例2
输入:
1982 3 4
输出:
63
#include <cstddef>
#include <iostream>
#include <cstring>
#include <sstream>
#include <vector>
using namespace std;
int ComputeDays(int year,int month,int day)
{
int count=day;
bool isYun;
if ((year%4==0&&year%100!=0) || (year%400==0)) {
isYun=true;
}
else {
isYun=false;
}
switch(month-1)
{
case 12:count+=31;
case 11:count+=30;
case 10:count+=31;
case 9:count+=30;
case 8:count+=31;
case 7:count+=31;
case 6:count+=30;
case 5:count+=31;
case 4:count+=30;
case 3:count+=31;
case 2:(isYun==true)?count+=29:count+=28;
case 1:count+=31;
}
return count;
}
int main() {
int year,month,day;
cin>>year>>month>>day;
cout<<ComputeDays(year, month, day);
}
// 64 位输出请用 printf("%lld")
HJ81 字符串字符匹配
描述
判断短字符串S中的所有字符是否在长字符串T中全部出现。
请注意本题有多组样例输入。
数据范围:1≤len(S),len(T)≤200 1≤len(S),len(T)≤200
进阶:时间复杂度:O(n) O(n) ,空间复杂度:O(n) O(n)
输入描述:
输入两个字符串。第一个为短字符串,第二个为长字符串。两个字符串均由小写字母组成。
输出描述:
如果短字符串的所有字符均在长字符串中出现过,则输出字符串"true"。否则输出字符串"false"。
示例1
输入:
bc
abc
输出:
true
说明:
其中abc含有bc,输出"true"
#include <iostream>
#include <type_traits>
using namespace std;
bool isContainChar(string S,char c)
{
bool iscontained=false;
for (int i=0; i<S.size(); i++) {
if (c==S[i]) {
iscontained=true;
break;
}
}
return iscontained;
}
bool isContainAllChars(string T,string S)
{
bool isallcontained=true;
for (int i=0; i<T.size(); i++) {
if (!isContainChar(S,T[i])) {
isallcontained=false;
break;
}
}
return isallcontained;
}
int main() {
string sS,lS;
bool res;
while (cin>>sS>>lS) {
if (isContainAllChars(sS,lS)) {
cout<<"true";
}
else {
cout<<"false";
}
}
}
// 64 位输出请用 printf("%lld")
HJ84 统计大写字母个数
描述
找出给定字符串中大写字符(即'A'-'Z')的个数。
数据范围:字符串长度:1≤∣s∣≤250 1≤∣s∣≤250
字符串中可能包含空格或其他字符
进阶:时间复杂度:O(n) O(n) ,空间复杂度:O(n) O(n)
输入描述:
对于每组样例,输入一行,代表待统计的字符串
输出描述:
输出一个整数,代表字符串中大写字母的个数
示例1
输入:
A 1 0 1 1150175017(&^%&$vabovbaoadd 123#$%#%#O
输出:
2
#include <iostream>
using namespace std;
int main() {
string str;
getline(cin,str);
int count=0;
for(int i=0;i<str.size();i++)
{
if (str[i]>='A' && str[i]<='Z') {
count++;
}
}
cout<<count;
}
// 64 位输出请用 printf("%lld")
HJ85 最长回文子串
描述
给定一个仅包含小写字母的字符串,求它的最长回文子串的长度。
所谓回文串,指左右对称的字符串。
所谓子串,指一个字符串删掉其部分前缀和后缀(也可以不删)的字符串
数据范围:字符串长度1≤s≤350 1≤s≤350
进阶:时间复杂度:O(n) O(n) ,空间复杂度:O(n) O(n)
输入描述:
输入一个仅包含小写字母的字符串
输出描述:
返回最长回文子串的长度
示例1
输入:
cdabbacc
输出:
4
说明:
abba为最长的回文子串
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
bool isPalindrome(string s)
{
string _s=s;
reverse(_s.begin(),_s.end());
if (_s==s) {
return true;;
}
else {
return false;
}
}
int main() {
string str;
getline(cin,str);
int len=str.size();
int max=1;
for (int i=0; i<str.size(); i++) {
for (int j=len-i; j>0; j--) {//截取的字符长度
if (isPalindrome(str.substr(i,j))) {
if (max<j) {
max=j;
}
}
}
}
cout<<max;
return 0;
}
// 64 位输出请用 printf("%lld")
HJ87 密码强度等级
描述
密码按如下规则进行计分,并根据不同的得分为密码进行安全等级划分。
一、密码长度:
5 分: 小于等于4 个字符
10 分: 5 到7 字符
25 分: 大于等于8 个字符
二、字母:
0 分: 没有字母
10 分: 密码里的字母全都是小(大)写字母
20 分: 密码里的字母符合”大小写混合“
三、数字:
0 分: 没有数字
10 分: 1 个数字
20 分: 大于1 个数字
四、符号:
0 分: 没有符号
10 分: 1 个符号
25 分: 大于1 个符号
五、奖励(只能选符合最多的那一种奖励):
2 分: 字母和数字
3 分: 字母、数字和符号
5 分: 大小写字母、数字和符号
最后的评分标准:
>= 90: 非常安全
>= 80: 安全(Secure)
>= 70: 非常强
>= 60: 强(Strong)
>= 50: 一般(Average)
>= 25: 弱(Weak)
>= 0: 非常弱(Very_Weak)
对应输出为:
VERY_SECURE
SECURE
VERY_STRONG
STRONG
AVERAGE
WEAK
VERY_WEAK
请根据输入的密码字符串,进行安全评定。
注:
字母:a-z, A-Z
数字:0-9
符号包含如下: (ASCII码表可以在UltraEdit的菜单view->ASCII Table查看)
!"#$%&'()*+,-./ (ASCII码:0x21~0x2F)
:;<=>?@ (ASCII码:0x3A~0x40)
[\]^_` (ASCII码:0x5B~0x60)
{|}~ (ASCII码:0x7B~0x7E)
提示:
1 <= 字符串的长度<= 300
输入描述:
输入一个string的密码
输出描述:
输出密码等级
示例1
输入:
38$@NoNoN
输出:
VERY_SECURE
说明:
样例的密码长度大于等于8个字符,得25分;大小写字母都有所以得20分;有两个数字,所以得20分;包含大于1符号,所以得25分;由于该密码包含大小写字母、数字和符号,所以奖励部分得5分,经统计得该密码的密码强度为25+20+20+25+5=95分。
示例2
输入:
Jl)M:+
输出:
AVERAGE
说明:
示例2的密码强度为10+20+0+25+0=55分。
#include <cctype>
#include <iostream>
#include <cstring>
#include <ctype.h>
using namespace std;
int Norm1(string password)
{
if (password.size()<=4)
{
return 5;
}
else if(password.size()>=5 && password.size()<=7)
{
return 10;
}
else {
return 25;
}
}
int Norm2(string password)
{
int score[3]={0};
for (auto c:password) {
if (!isalpha(c)) {
score[0]=0;
}
else {
if (isupper(c)&&score[1]==0) {
score[1]=10;
}
else if(islower(c)&&score[2]==0) {
score[2]=10;
}
}
if (score[0]+score[1]+score[2]==20) {
break;
}
}
return score[0]+score[1]+score[2];
}
int Norm3(string password)
{
int score=0;
for (auto c:password) {
if (isdigit(c)) {
score+=10;
}
if (score==20) {
break;
}
}
return score;
}
int Norm4(string password)
{
int score=0;
int n=0;
for (auto c:password) {
if (!(isalpha(c)||isdigit(c))) {
n++;
}
if (n>1) {
break;
}
}
if (n==1) {
score=10;
}
else if(n>1)
{
score =25;
}
return score;
}
int Norm5(string password)
{
if(Norm2(password)==20&&Norm3(password)>0&&Norm4(password)>0)
{
return 5;
}
if(Norm2(password)==10&&Norm3(password)>0&&Norm4(password)>0)
{
return 3;
}
if (Norm2(password)>0&&Norm3(password)>0) {
return 2;
}
return 0;
}
void print(int score)
{
if (score>=90) {
cout<<"VERY_SECURE";
return;
}
if (score>=80) {
cout<<"SECURE";
return;
}
if (score>=70) {
cout<<"VERY_STRONG";
return;
}
if (score>=60) {
cout<<"STRONG";
return;
}
if (score>=50) {
cout<<"AVERAGE";
return;
}
if (score>=25) {
cout<<"WEAK";
return;
}
if (score>=0) {
cout<<"VERY_WEAK";
return;
}
}
int main() {
int score=0;
string password;
getline(cin,password);
score+=Norm1(password);
score+=Norm2(password);
score+=Norm3(password);
score+=Norm4(password);
score+=Norm5(password);
print(score);
}
// 64 位输出请用 printf("%lld")
HJ96 表示数字
描述
将一个字符串中所有的整数前后加上符号“*”,其他字符保持不变。连续的数字视为一个整数。
数据范围:字符串长度满足 1≤n≤100 1≤n≤100
输入描述:
输入一个字符串
输出描述:
字符中所有出现的数字前后加上符号“*”,其他字符保持不变
示例1
输入:
Jkdi234klowe90a3
输出:
Jkdi*234*klowe*90*a*3*
#include <bits/stdc++.h>
#include <iostream>
#include <ctype.h>
#include <regex>
#include <string>
#include <vector>
using namespace std;
int main() {
string str;
while (getline(cin, str))
{
vector<string> vc;
bool lastnumflag = false;
for (int i = 0;i < str.size(); i++) {
string s = "";
if (isdigit(str[i])) {//当前字符为数字,数字前加*,并标记lastnumflag=true;
if (!lastnumflag) {//上一个字符不是数字,这个字符是数字
s += "*";
}
s.push_back(str[i]);
lastnumflag = true;
if (i==str.size()-1)//最后一个字符是数字
{
s += "*";
}
}
else {//当前字符不是数字,判断上个字符是否为数字
if (lastnumflag) {
s += "*";
}
s.push_back(str[i]);
lastnumflag = false;
}
vc.push_back(s);
}
for (string c : vc) {
cout << c;
}
cout << endl;
}
}
// 64 位输出请用 printf("%lld")
HJ101 输入整型数组和排序标识,对其元素按照升序或降序进行排序
描述
输入整型数组和排序标识,对其元素按照升序或降序进行排序
数据范围: 1≤n≤1000 1≤n≤1000 ,元素大小满足 0≤val≤100000 0≤val≤100000
输入描述:
第一行输入数组元素个数
第二行输入待排序的数组,每个数用空格隔开
第三行输入一个整数0或1。0代表升序排序,1代表降序排序
输出描述:
输出排好序的数字
示例1
输入:
8
1 2 4 9 3 55 64 25
0
输出:
1 2 3 4 9 25 55 64
示例2
输入:
5
1 2 3 4 5
1
输出:
5 4 3 2 1
#include <algorithm>
#include <iostream>
#include <bits/stdc++.h>
#include <vector>
using namespace std;
int main() {
int n;
while (cin>>n) {
vector<int> num;
int m;
for(int i=0;i<n;i++)
{
cin>>m;
num.push_back(m);
}
int k;
cin>>k;
if(k==0)
{
sort(num.begin(), num.end(),less<int>());//less<T>变成升序(从左到右遍历下标时,数组元素是从小到大)
}
else {
sort(num.begin(), num.end(), greater<int>());//greater<T>变成降序(从左到右遍历下标时,数组元素是从大到小)
}
for (auto i:num) {
cout<<i<<" ";
}
}
}
// 64 位输出请用 printf("%lld")
HJ102 字符统计
描述
输入一个只包含小写英文字母和数字的字符串,按照不同字符统计个数由多到少输出统计结果,如果统计的个数相同,则按照ASCII码由小到大排序输出。
数据范围:字符串长度满足 1≤len(str)≤1000 1≤len(str)≤1000
输入描述:
一个只包含小写英文字母和数字的字符串。
输出描述:
一个字符串,为不同字母出现次数的降序表示。若出现次数相同,则按ASCII码的升序输出。
示例1
输入:
aaddccdc
输出:
cda
说明:
样例里,c和d出现3次,a出现2次,但c的ASCII码比d小,所以先输出c,再输出d,最后输出a.
#include <algorithm>
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
bool cmp(pair<char,int> a,pair<char,int> b)
{
if(a.second==b.second)
{
//当出现次数相同时
return a.first<b.first;//输出ASCII码较小的字符
}
return a.second>b.second;//输出出现次数较多的字符
}
int main() {
string str;
while(cin>>str)
{
map<char,int> mp;
for(int i=0;i<str.size();i++)//逐个统计字符出现的次数
{
mp[str[i]]++;
}
vector<pair<char,int>> v(mp.begin(),mp.end());
sort(v.begin(),v.end(),cmp);//按照出现次数进行排序
for(auto it:v)
{
cout<<it.first;//按照次数大小输出
}
cout<<endl;
}
}
// 64 位输出请用 printf("%lld")
HJ106 字符逆序
描述
将一个字符串str的内容颠倒过来,并输出。
数据范围:1≤len(str)≤10000 1≤len(str)≤10000
输入描述:
输入一个字符串,可以有空格
输出描述:
输出逆序的字符串
示例1
输入:
I am a student
输出:
tneduts a ma I
示例2
输入:
nowcoder
输出:
redocwon
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main() {
string str;
getline(cin,str);
reverse(str.begin(),str.end());
cout<<str;
return 0;
}
// 64 位输出请用 printf("%lld")