1 字符串最后一个单词的长度
#include <iostream>
using namespace std;
int main()
{
char ch[128];
while (cin.getline(ch, 128))
{
int len = 0;
for (int i = 0; ch[i] != '\0'; i++)
{
if (ch[i] == ' ')
{
len = 0;
}
else
{
len++;
}
}
cout<<len<<endl;
}
}
2 合唱队
//动态规划,两头遍历
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int num;
while (cin>>num)
{
vector<int> p(num);
for (int i = 0; i < num; i++)
{
cin>>p[i];
}
vector<int> left(num, 1);
vector<int> right(num, 1);
for (int i = 1; i < num; i++)
{
for (int j = 0; j < i; j++)
{
if (p[i] > p[j] && left[j] + 1 > left[i])
{
left[i] = left[j] + 1;
}
}
}
for (int i = num - 2; i >= 0; i--)
{
for (int j = num - 1; j > i; j--)
{
if (p[i] > p[j] && right[j] + 1 > right[i])
{
right[i] = right[j] + 1;
}
}
}
int res = 0;
for (int i = 0; i < num; i++)
{
res = max(res, left[i] + right[i] - 1);
}
cout<<num-res<<endl;
}
}
3 挑7
//找出不大于n的数里面7的倍数以及某位数为7的数的个数
#include <iostream>
using namespace std;
bool fun(int n)
{
while (n)
{
if (n % 10 == 7)
{
return true;
}
n /= 10;
}
return false;
}
int main()
{
int n;
while (cin>>n)
{
int res = 0;
for (int i = 7; i <= n; i++)
{
if (i % 7 == 0 || fun(i))
{
res++;
}
}
cout<<res<<endl;
}
}
4 配置文件恢复
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
string key1[6] = {"reset","reset","board","board","reboot","backplane"};
string key2[6] = {"","board","add","delet","backplane","abort"};
string result[6] = {"reset what","board fault","where to add","no board at all","impossible","install first"};
bool fun(string str1, string str2)
{
bool b = true;
if (str1.size() > str2.size())
{
return false;
}
for (int i = 0; i < str1.size(); i++)
{
if (str1[i] != str2[i])
{
b = false;
}
}
return b;
}
int main()
{
string str;
while (getline(cin, str))
{
string first = "";
string second = "";
int blank = 0;
for (int i = 0; i < str.size(); i++)
{
if (str[i] == ' ')
{
blank++;
}
else if (blank == 0)
{
first += str[i];
}
else
{
second += str[i];
}
}
int res = -1;
if (first == "") //无关键字
{
//res = -1;
cout<<endl;
continue;
}
else if (second == "") //一个关键字
{
if (fun(first, key1[0]))
{
res = 0;
}
}
else //两个关键字
{
int cnt = 0;
for (int i = 1; i < 6; i++)
{
if (fun(first, key1[i]) && fun(second, key2[i]))
{
cnt++;
res = i;
}
}
if (cnt != 1)
{
res = -1;
}
}
if (res == -1)
{
cout<<"unkown command"<<endl;
}
else
{
cout<<result[res].c_str()<<endl;
}
}
}
5 购物单
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int total;
int cnt;
while (cin>>total>>cnt)
{
vector<vector<int>> goods(cnt + 1, vector<int>(3, 0));
for (int i = 1; i <= cnt; i++)
{
cin>>goods[i][0]>>goods[i][1]>>goods[i][2];
}
vector<int> res(total + 1, 0);
for (int i = 1; i <= cnt; i++)
{
for (int j = total; j >= 1; j--)
{
int temp = goods[i][0];
int zhujian = goods[i][2];
while (zhujian != 0)
{
temp += goods[zhujian][0];
zhujian = goods[zhujian][2];
}
if (j >= temp)
{
res[j] = max(res[j], res[j - temp] + goods[i][0] * goods[i][1]);
}
}
}
cout<<res[total]<<endl;
}
}
6坐标移动
#include <iostream>
#include <string>
#include <vector>
using namespace std;
enum Direction { A = 'A', S = 'S', W = 'W', D = 'D'};
void move(int &x, int &y, Direction dir, int dist)
{
switch (dir)
{
case 'A':
x -= dist;
break;
case 'D':
x += dist;
break;
case 'W':
y += dist;
break;
case 'S':
y -= dist;
break;
}
}
void transfer(string str, int& x, int& y)
{
Direction dir;
int dist;
if (str[0] == 'A' || str[0] == 'D' || str[0] == 'W' || str[0] == 'S')
{
dir = (Direction)str[0];
if (str[1] >= '0' && str[1] <= '9')
{
dist = str[1] - '0';
if (str.size() == 3)
{
if (str[2] >= '0' && str[2] <= '9')
{
dist = dist * 10 + str[2] - '0';
}
else
{
return;
}
}
move(x, y, dir, dist);
}
}
}
int main()
{
string str;
getline(cin, str);
int x = 0;
int y = 0;
string temp = "";
for (int i = 0; i < str.size(); i++)
{
if (str[i] != ';')
{
temp += str[i];
}
else
{
if (temp.size() == 3 || temp.size() == 2)
{
transfer(temp, x, y);
}
temp = "";
}
}
cout<<x<<","<<y;
}
7 识别有效的IP地址和掩码并进行分类统计
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <cstdio>
using namespace std;
bool checkip(vector<int> num)
{
for (int i = 0; i < 4; i++)
{
if (num[i] < 0 || num[i] > 255)
{
return false;
}
}
return true;
}
bool checkmark(vector<int> num)
{
int sum = num[0] << 24 | num[1] << 16 | num[2] << 8 | num[3];
bool b = false;
for (int i = 0; i < 32; i++)
{
if ((sum & 1 << i) == 0)
{
if (b == true)
{
return false;
}
}
else
{
b = true;
}
}
return true;
}
int str2num(string str)
{
int res = 0;
for (int i = 0; i < str.size(); i++)
{
res = res * 10 + str[i] - '0';
}
return res;
}
vector<int> seperate(string str)
{
vector<int> ss;
int i = 0;
int j;
string temp;
while ((j = str.find('.', i)) != string::npos)
{
temp = str.substr(i, j - i);
if (temp.size() != 0)
{
ss.push_back(str2num(temp));
}
else
{
ss.push_back(-1);
}
i = j + 1;
}
temp = str.substr(i);
if (temp.size() != 0)
{
ss.push_back(str2num(temp));
}
else
{
ss.push_back(-1);
}
return ss;
}
int main()
{
string str;
int cnt[7] = {0};
while (getline(cin, str))
{
if (str.size() == 0)
{
break;
}
int i = 0;
int j;
j = str.find('~');
string strIp = str.substr(0, j);
string strMark = str.substr(j + 1);
vector<int> ip = seperate(strIp);
vector<int> mark = seperate(strMark);
if (checkip(ip) && checkmark(mark))
{
if (ip[0] >= 1 && ip[0] <= 126)
{
cnt[0]++;
}
else if (ip[0] >= 128 && ip[0] <= 191)
{
cnt[1]++;
}
else if (ip[0] >= 192 && ip[0] <= 223)
{
cnt[2]++;
}
else if (ip[0] >= 224 && ip[0] <= 239)
{
cnt[3]++;
}
else
{
cnt[4]++;
}
if ((ip[0] == 10) || (ip[0] == 172 && ip[1] >= 16 && ip[1] <= 31)
|| (ip[0] == 192 && ip[1] == 168))
{
cnt[6]++;
}
}
else
{
cnt[5]++;
}
}
for (int i = 0; i < 7; i++)
{
cout<<cnt[i]<<" ";
}
}
8 字符逆序
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string str;
while (getline(cin, str))
{
reverse(str.begin(), str.end());
cout<<str.c_str()<<endl;
}
return 0;
}
9 公共字串计算
//忽略大小写。还以为是字符串有空格,导致错了两次
#include <iostream>
#include <cstring>
#include <vector>
#include <cmath>
using namespace std;
int getCommonStrLength(char * str1, char * str2)
{
int len1 = strlen(str1);
int len2 = strlen(str2);
vector<vector<int>> dp(len1 + 1, vector<int>(len2 + 1, 0));
int res = 0;
for (int i = 1; i <= len1; i++)
{
for (int j = 1; j <= len2; j++)
{
if (str1[i - 1] == str2[j - 1] || abs(str1[i - 1] - str2[j - 1]) == 32)
{
dp[i][j] = dp[i - 1][j - 1] + 1;
res = max(res, dp[i][j]);
}
}
}
return res;
}
int main()
{
char str1[100];
char str2[100];
while (cin>>str1>>str2)
{
cout<<getCommonStrLength(str1, str2)<<endl;
}
return 0;
}
10 统计大写字母个数
#include <iostream>
#include <string>
using namespace std;
int CalcCapital(string str)
{
int cnt = 0;
for (int i = 0; i < str.size(); i++)
{
if (str[i] >= 'A' && str[i] <= 'Z')
{
cnt++;
}
}
return cnt;
}
int main()
{
string str;
while (getline(cin, str))
{
cout<<CalcCapital(str)<<endl;
}
return 0;
}
11 字符串反转
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string str;
while (getline(cin, str))
{
reverse(str.begin(), str.end());
cout<<str.c_str()<<endl;
}
return 0;
}
12 找出字符串中第一个只出现一次的字符
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool FindChar(char* str, char* c)
{
int ch[128] = {0};
char *p = str;
while (*p != '\0')
{
ch[*p++]++;
}
p = str;
while (*p != '\0')
{
if (ch[*p] == 1)
{
*c = *p;
return true;
}
p++;
}
*c = '.';
return false;
}
int main()
{
char str[100];
while (cin.getline(str, 100))
{
char ch;
FindChar(str, &ch);
cout<<ch<<endl;
}
return 0;
}
13 计算字符个数
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int main()
{
string str;
while (getline(cin, str))
{
string temp;
getline(cin, temp);
char ch = temp[0];
int cnt = 0;
for (int i = 0; i < str.size(); i++)
{
if (str[i] == ch)
{
cnt++;
}
}
cout<<cnt<<endl;
}
return 0;
}
14 字符串加解密
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
void Encrypt(char aucPassword[], char aucResult[])
{
for (int i = 0; i < strlen(aucPassword); i++)
{
if (aucPassword[i] >= 'a' && aucPassword[i] < 'z')
{
aucResult[i] = aucPassword[i] - 31;
}
else if (aucPassword[i] >= 'A' && aucPassword[i] < 'Z')
{
aucResult[i] = aucPassword[i] + 33;
}
else if (aucPassword[i] >= '0' && aucPassword[i] < '9')
{
aucResult[i] = aucPassword[i] + 1;
}
else if (aucPassword[i] == 'Z')
{
aucResult[i] = 'a';
}
else if (aucPassword[i] == 'z')
{
aucResult[i] = 'A';
}
else if (aucPassword[i] == '9')
{
aucResult[i] = '0';
}
}
}
void unEncrypt(char result[], char password[])
{
for (int i = 0; i < strlen(result); i++)
{
if (result[i] >= 'a' && result[i] < 'z')
{
password[i] = result[i] - 33;
}
else if (result[i] >= 'A' && result[i] < 'Z')
{
password[i] = result[i] + 31;
}
else if (result[i] >= '0' && result[i] < '9')
{
password[i] = result[i] - 1;
}
else if (result[i] == 'A')
{
password[i] = 'z';
}
else if (result[i] == 'a')
{
password[i] = 'Z';
}
else if (result[i] == '0')
{
password[i] = '9';
}
}
}
int main()
{
char str1[100] = {0};
char str2[100] = {0};
cin.getline(str1, 100);
Encrypt(str1, str2);
cout<<str2<<endl;
cin.getline(str1, 100);
memset(str2, 0, strlen(str1));
unEncrypt(str1, str2);
cout<<str2<<endl;
return 0;
}
15 蛇形矩阵
样例输入
4
样例输出
1 3 6 10
2 5 9
4 8
7
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int n;
cin>>n;
vector<vector<int>> num(n, vector<int>(n, 0));
int p = 1;
for (int i = 0; i < n; i++)
{
for (int j = i, k = 0; j >= 0 && k <= i; j--, k++)
{
num[j][k] = p++;
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n - i; j++)
{
cout<<num[i][j];
if (j != n - i - 1)
{
cout<<" ";
}
}
cout<<endl;
}
return 0;
}
16 字符串加密
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
int main()
{
char key[100];
char str[100];
while (cin.getline(key, 100))
{
cin.getline(str, 100);
char cnt[26] = {0};
char hash[26];
int index = 0;
for (int i = 0; i < strlen(key); i++)
{
if (key[i] >= 'a')
{
key[i] -= 32;
}
if (cnt[key[i] - 'A'] == 0)
{
cnt[key[i] - 'A']++;
hash[index++] = key[i];
}
}
for (char ch = 'A'; ch <= 'Z'; ch++)
{
if (cnt[ch - 'A'] == 0)
{
hash[index++] = ch;
}
}
for (int i = 0; i < strlen(str); i++)
{
if (str[i] == ' ')
{
continue;
}
bool flag = false;
if (str[i] >= 'a')
{
str[i] -= 32;
flag = true;
}
str[i] = hash[str[i] - 'A'];
if (flag)
{
str[i] += 32;
}
}
cout<<str<<endl;
}
}
17 称砝码
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int n;
while (cin>>n)
{
vector<int> fama(n, 0);
vector<int> num(n, 0);
int total = 0;
for (int i = 0; i < n; i++)
{
cin>>fama[i];
}
for (int i = 0; i < n; i++)
{
cin>>num[i];
total += fama[i] * num[i];
}
vector<bool> b(total + 1, false);
b[0] = true;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < num[i]; j++)
{
for (int k = total; k >= fama[i]; k--)
{
b[k] = b[k] || b[k - fama[i]];
}
}
}
int res = 0;
for (int i = 0; i <= total; i++)
{
if (b[i])
{
res++;
}
}
cout<<res<<endl;
}
return 0;
}
/*
6
1 2 3 5 10 20
1 2 2 0 0 1
ans:24
*/
18 iNOC产品部-杨辉三角的变形
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int n;
cin>>n;
vector<vector<int>> num(n, vector<int>(2 * n - 1, 0));
num[0][n - 1] = 1;
for (int i = 1; i < n; i++)
{
for (int j = n - 1 - i; j <= n - 1 + i; j++)
{
int temp = 0;
if (j - 1 >= 0)
{
temp += num[i - 1][j - 1];
}
if (j + 1 < 2 * n - 1)
{
temp += num[i - 1][j + 1];
}
temp += num[i - 1][j];
num[i][j] = temp;
}
}
int index = -1;
for (int i = 0; i < 2 * n - 1; i++)
{
if (num[n - 1][i] % 2 == 0)
{
index = i + 1;
break;
}
}
cout<<index<<endl;
return 0;
}
19 查找组成一个偶数最接近的两个素数
#include <iostream>
#include <cmath>
using namespace std;
bool isPrime(int n)
{
for (int i = 2; i <= n / 2; i++)
{
if (n % i == 0)
{
return false;
}
}
return true;
}
int main()
{
int n;
while (cin>>n)
{
int a, b;
int diff = n;
for (int i = 3; i < n; i++)
{
if (isPrime(i) && isPrime(n - i))
{
if (abs(n - 2 * i) < diff)
{
a = i;
b = n - i;
diff = abs(n - 2 * i);
}
}
}
cout<<a<<endl<<b<<endl;
}
return 0;
}
20 放苹果
#include <iostream>
#include <cmath>
using namespace std;
int fun(int m, int n)
{
if (m == 0 || n == 1)
{
return 1;
}
else if (m < n)
{
return fun(m, m);
}
else
{
return fun(m, n - 1) + fun(m - n, n);
}
}
int main()
{
int m, n;
while (cin>>m>>n)
{
cout<<fun(m, n)<<endl;
}
return 0;
}
时间: 2024-10-27 02:15:10