题目来源:牛客网
题十一. HJ66 配置文件恢复
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
bool match(string str, string s) {
return str.find(s) == 0;
}
int main() {
string str;
string cmd[6] = {"reset", "reset board", "board add", "board delete", "reboot backplane", "backplane abort" };
string res[7] = {"reset what", "board fault", "where to add", "no board at all", "impossible", "install first", "unknown command" };
while (getline(cin, str)) {
string s1, s2, temp;
stringstream ss(str);
ss >> s1 >> s2;
int resi = 6;
if (s2.empty())
resi = match(cmd[0], s1) ? 0 : 6;
else {
bool flag = false;
for (int i = 1; i < 6; i++) {
stringstream allcmd(cmd[i]);
allcmd >> temp >> temp;
if (match(cmd[i], s1) && match(temp, s2)) {
if (!flag) {
flag = true;
resi = i;
} else {
resi = 6;
break;
}
}
}
}
cout << res[resi] << endl;
}
return 0;
}
题十二. HJ72 百钱买百鸡问题
题目描述
公元前五世纪,我国古代数学家张丘建在《算经》一书中提出了“百鸡问题”:鸡翁一值钱五,鸡母一值钱三,鸡雏三值钱一。百钱买百鸡,问鸡翁、鸡母、鸡雏各几何?详细描述:
接口说明
原型:
int GetResult(vector &list)
输入参数:
无
输出参数(指针指向的内存区域保证有效):
list 鸡翁、鸡母、鸡雏组合的列表
返回值:
-1 失败 0 成功
输入描述: 输入任何一个整数,即可运行程序。
输出描述:
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int n;
while(cin >> n){
for(int x = 0; x <= 100/5; x++)
{
double y = (200 -14*x)/8.0;
double z = 100 - y - x;
if(y == int(y) && y >= 0 && z >= 0)
cout << x << " " << y << " "<< z << endl;
}
}
return 0;
}
题十三. HJ73 计算日期到天数转换
题目描述 根据输入的日期,计算是这一年的第几天。
输入描述: 输入一行,每行空格分割,分别是年,月,日
输出描述: 输出是这一年的第几天
示例1
输入
2012 12 31
输出
366
#include<iostream>
using namespace std;
int runMon[12] = {31,29,31,30,31,30,31,31,30,31,30,31};
int pingMon[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
int dataToDay(int year, int month, int day)
{
if(year <= 0|| month <= 0|| month > 12 || day >31 || day <= 0)
return -1;
int res = 0;
int flag = 0;
if( year%400 == 0 || (year%100 != 0 && year%4 == 0))
flag =1;
for(int i = 0; i < month - 1; i++)
{
if(flag == 1)
res += runMon[i];
else
res += pingMon[i];
}
res += day;
return res;
}
int main()
{
int year, month, day;
while(cin >> year >> month >> day)
{
cout << dataToDay(year, month, day)<<endl;
}
return 0;
}
题十四. HJ74 参数解析
题目描述 在命令行输入如下命令:
xcopy /s c:\ d:\,
各个参数如下:
参数1:命令字xcopy
参数2:字符串/s
参数3:字符串c:\
参数4: 字符串d:\
请编写一个参数解析程序,实现将命令行各个参数解析出来。
解析规则:
1.参数分隔符为空格
2.对于用""包含起来的参数,如果中间有空格,不能解析为多个参数。比如在命令行输入xcopy /s “C:\program files” "d:“时,参数仍然是4个,第3个参数应该是字符串C:\program
files,而不是C:\program,注意输出参数时,需要将”"去掉,引号不存在嵌套情况。
3.参数不定长
4.输入由用例保证,不会出现不符合要求的输入
输入描述: 输入一行字符串,可以有空格
输出描述: 输出参数个数,分解后的参数,每个参数都独占一行
示例1
输入xcopy /s c:\ d:\
输出
4
xcopy
/s
c:\
d:\
#include <iostream>
#include<string>
#include<vector>
using namespace std;
void cmd_explain(const string &str){
string temp = "";
vector<string> res;
int size = str.size();
int flag = 0;
for(int i = 0; i < size; i++){
if('"' == str[i])
{
flag = ~flag;
}
else if(' ' == str[i] && 0 == flag){
res.push_back(temp);
temp = "";
}else
temp += str[i];
}
res.push_back(temp);
cout << res.size() <<endl;
for(int i = 0; i < res.size(); i++){
cout << res[i] << endl;
}
}
int main()
{
string str;
while(getline(cin,str)){
cmd_explain(str);
}
return 0;
}
题十五. HJ75 公共字串计算
题目描述 给定两个只包含小写字母的字符串,计算两个字符串的最大公共子串的长度。
注:子串的定义指一个字符串删掉其部分前缀和后缀(也可以不删)后形成的字符串。
输入描述: 输入两个只包含小写字母的字符串
输出描述: 输出一个整数,代表最大公共子串的长度
示例1
输入
asdfas
werasdfaswer
输出
6
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
using namespace std;
int main()
{
string s1,s2;
while(cin >> s1 >> s2){
int m = s1.length();
int n = s2.length();
int maxLen = 0;
vector<vector<int>>dp(m+1,vector<int>(n+1,0));
for(int i = 1; i <= m; i++)
for(int j = 1; j <= n; j++)
if(s1[i-1] == s2[j-1])
{
dp[i][j] = dp[i-1][j-1] + 1;
maxLen = max(maxLen, dp[i][j]);
}
cout << maxLen <<endl;
}
return 0;
}
题十六. HJ76 尼科彻斯定理
题目描述 验证尼科彻斯定理,即:任何一个整数m的立方都可以写成m个连续奇数之和。
例如:
1^3=1
2^3=3+5
3^3=7+9+11
4^3=13+15+17+19
输入一个正整数m(m≤100),将m的立方写成m个连续奇数之和的形式输出。 本题含有多组输入数据。
输入描述: 输入一个int整数
输出描述: 输出分解后的string
示例1
输入
6
输出
31+33+35+37+39+41
#include<stdio.h>
#include<iostream>
using namespace std;
//数学公式直接可以推出来首项是m*m+1-m,有m项
int main(){
int m;
while(cin>>m){
int s = m*m+1-m;
cout<<s;
for(int i=1;i<m; i++)
cout<<'+'<<s+2*i;
cout<<endl;
}
return 0;
}
题十六. HJ83 二维数组操作
这题没看懂题目在说啥,参考评论老哥答案,传送门
#include<iostream>
using namespace std;
int main()
{
int row,col;
int i,j,x,y;
int new_row,new_col;
int m,n;
while(cin>>row>>col>>i>>j>>x>>y>>new_row>>new_col>>m>>n){
if(row<=9&&col<=9)
cout<<0<<endl;
else
cout<<-1<<endl;
if(i<=row-1&&x<=row-1&&j<=col-1&&y<=col-1)
cout<<0<<endl;
else
cout<<-1<<endl;
if(new_row < row&&new_row>=0&&(row+1)<=9)
{
cout<<0<<endl;
}
else
cout<<-1<<endl;
if(new_col < col&&new_col>=0&&(col+1)<=9)
{
cout<<0<<endl;
}
else
cout<<-1<<endl;
if(m<=row-1&&m>=0&&n<=col-1&&n>=0)
cout<<0<<endl;
else
cout<<-1<<endl;
}
return 0;
}
题十七. HJ84 统计大写字母个数
题目描述 找出给定字符串中大写字符(即’A’-‘Z’)的个数。
输入描述: 本题含有多组样例输入 对于每组样例,输入一行,代表待统计的字符串
输出描述: 对于每组样例,输出一个整数,代表字符串中大写字母的个数
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
while(getline(cin, s)){
int count =0;
for(int i = 0; i < s.size(); i++){
if(s[i] >= 'A' && s[i] <= 'Z')
count++;
}
cout << count <<endl;
}
return 0;
}
题十八. HJ85 字符串运用-密码截取
题目描述 给定一个仅包含小写字母的字符串,求它的最长回文子串的长度。 所谓回文串,指左右对称的字符串。
所谓子串,指一个字符串删掉其部分前缀和后缀(也可以不删)的字符串 (注意:记得加上while处理多个测试用例)输入描述: 输入一个仅包含小写字母的字符串
输出描述: 返回最长回文子串的长度
示例1
输入
cdabbacc
输出
4
说明 abba为最长的回文子串
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
string s;
while(cin >> s){
string t(s);
reverse(t.begin(),t.end());
int len = t.size();
vector<vector<int>> dp(len+1,vector<int>(len+1,0));
int maxlen = 0;
for(int i = 1; i <= len; i++){
for(int j = 1; j <= len; j++){
if(s[i-1] == t[j-1]){
dp[i][j] = dp[i-1][j-1] + 1;
}
maxlen = max(dp[i][j], maxlen);
}
}
cout << maxlen << endl;
}
return 0;
}
题十九. HJ86 求最大连续bit数
题目描述 求一个byte数字对应的二进制数字中1的最大连续数,例如3的二进制为00000011,最大连续2个1
本题含有多组样例输入。
输入描述: 输入一个byte数字
输出描述: 输出转成二进制之后连续1的个数
示例1
输入
3
5
输出
2
1说明 3的二进制表示是11,最多有2个连续的1。
5的二进制表示是101,最多只有1个连续的1。
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n;
while(cin >> n){
int count = 0,maxcount =0;
while(n){
if(n&1){
count++;
maxcount = max(maxcount,count);
}
else
count = 0;
n=n>>1;
}
cout << maxcount << endl;
}
return 0;
}
题二十. 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_SECURE
SECURE,
VERY_STRONG,
STRONG,
AVERAGE,
WEAK,
VERY_WEAK,
请根据输入的密码字符串,进行安全评定。
注:
字母:a-z, A-Z
数字:-9
符号包含如下: (ASCII码表可以在UltraEdit的菜单view->ASCII Table查看)
!"#$%&’()*+,-./ (ASCII码:x21~0x2F)
:;<=>?@ (ASCII<=><=><=><=><=>码:x3A~0x40)
[]^_` (ASCII码:x5B~0x60)
{|}~ (ASCII码:x7B~0x7E)
输入描述: 本题含有多组输入样例。 每组样例输入一个string的密码
输出描述: 每组样例输出密码等级
#include <iostream>
#include <string>
using namespace std;
int main()
{
string password;
while(getline(cin,password))
{
int score=0;
//密码长度得分
if(password.size()<=4){
score+=5;
}else if(password.size()>=8){
score+=25;
}else{
score+=10;
}
//字母、数字、符号统计
int Alpha=0,alpha=0,number=0,number_count=0,ch=0,ch_count=0;
for(int i=0;i<password.size();i++)
{
if(password[i]>='a' && password[i]<='z'){
alpha=1;
}else if(password[i]>='A' && password[i]<='Z'){
Alpha=1;
}else if(isdigit(password[i])){
number=1;
number_count++;
}else{
ch=1;
ch_count++;
}
}
//字母得分
if((alpha==1&&Alpha==0) || (alpha==0&&Alpha==1)){
score+=10;
}else if(alpha==1 && Alpha==1){
score+=20;
}
//数字得分
if(number_count>1){
score+=20;
}else if(number){
score+=10;
}
//符号得分
if(ch_count>1){
score+=25;
}else if(ch){
score+=10;
}
//奖励得分
if(Alpha && alpha && number && ch){
score+=5;
}else if((Alpha||alpha)&& number && ch){
score+=3;
}else if((Alpha||alpha)&& number){
score+=2;
}
if(score>=90){
cout<<"VERY_SECURE"<<endl;
}else if(score>=80){
cout<<"SECURE"<<endl;
}else if(score>=70){
cout<<"VERY_STRONG"<<endl;
}else if(score>=60){
cout<<"STRONG"<<endl;
}else if(score>=50){
cout<<"AVERAGE"<<endl;
}else if(score>=25){
cout<<"WEAK"<<endl;
}else{
cout<<"VERY_WEAK"<<endl;
}
}
return 0;
}
题二十一. HJ91 走方格的方案数
题目描述
请计算n*m的棋盘格子(n为横向的格子数,m为竖向的格子数)沿着各自边缘线从左上角走到右下角,总共有多少种走法,要求不能走回头路,即:只能往右和往下走,不能往左和往上走。本题含有多组样例输入。
输入描述: 每组样例输入两个正整数n和m,用空格隔开。(1≤n,m≤8)
输出描述: 每组样例输出一行结果
示例1
输入
2 2
1 2
输出
6
3
#include<iostream>
#include<vector>
using namespace std;
int func(int m , int n){
if(m == 0 || n ==0){
return 1;
}
return func(m, n-1)+func(m-1, n);
}
int main()
{
int m, n;
while(cin >> m >> n){
cout << func(m, n)<< endl;
}
return 0;
}
题二十二. HJ100 等差数列
题目描述 功能:等差数列 2,5,8,11,14。。。。
输入:正整数N >0
输出:求等差数列前N项和
本题为多组输入,请使用while(cin>>)等形式读取数据
输入描述: 输入一个正整数。
输出描述: 输出一个相加后的整数。
示例1
输入
2
输出
7
#include<iostream>
using namespace std;
int main()
{
int x;
while(cin >> x){
cout << (2+2+3*(x-1))*x/2 <<endl;
}
return 0;
}
题二十三. HJ106 字符逆序
题目描述 将一个字符串str的内容颠倒过来,并输出。str的长度不超过100个字符。
输入描述: 输入一个字符串,可以有空格
输出描述: 输出逆序的字符串
示例1
输入
I am a student
输出
tneduts a ma I
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
string s;
while(getline(cin, s)){
reverse(s.begin(),s.end());
cout << s << endl;
}
return 0;
}
题二十四. HJ108 求最小公倍数
题目描述 正整数A和正整数B 的最小公倍数是指 能被A和B整除的最小的正整数值,设计一个算法,求输入A和B的最小公倍数。
输入描述: 输入两个正整数A和B。
输出描述: 输出A和B的最小公倍数。
示例1
输入
5 7
输出
35
#include<iostream>
using namespace std;
int gcd(int a, int b)
{
while(a%b){
int tmp = a;
a = b;
b = tmp % b;
}
return b;
}
int main()
{
int a, b;
while(cin >> a >> b){
cout << a*b/gcd(a, b)<<endl;
}
return 0;
}