9.提取不重复的整数
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int a[10] = {0};
while(n) {
a[n % 10] ++;
if(a[n % 10] == 1) {
cout << n % 10;
}
n /= 10;
}
return 0;
}
10.字符个数统计
#include <iostream>
using namespace std;
int main() {
int res = 0;
char c;
int a[128] = {0};
while(cin >> c) {
a[c] ++;
if(c >= 0 && c <= 127) {
if(a[c] == 1) {
res ++;
}
}
}
cout << res << endl;
return 0;
}
11.数字颠倒
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n;
cin >> n;
string str;
str = to_string(n);
reverse(str.begin(), str.end());
cout << str << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
while(n) {
int num = n % 10;
cout << num;
n /= 10;
}
return 0;
}
12.字符串反转
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
string s;
cin >> s;
reverse(s.begin(), s.end());
cout << s << endl;
return 0;
}
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
string s;
cin >> s;
for(int i = s.size() - 1; ~i; i --) {
cout << s[i];
}
return 0;
}
13.句子逆序
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
string s;
string res = "";
while(cin >> s) {
if(s == "") {
res = s;
} else {
res = s + " " + res;
}
}
cout << res << endl;
return 0;
}
14.字串的连接最长路径查找
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n;
vector<string>v;
cin >> n;
while(n --) {
string s;
cin >> s;
v.push_back(s);
}
sort(v.begin(), v.end());
for(auto s : v) cout << s << endl;
return 0;
}
15.求int型正整数在内存中存储时1的个数
#include <iostream>
using namespace std;
int main() {
int n, res = 0;
cin >> n;
while(n) {
if(n & 1 == 1) {
res ++;
}
n = n >> 1;
}
cout << res << endl;
return 0;
}
16.购物单
#include <iostream>
using namespace std;
int dp[3200];
int main() {
int N, m, v, p, q;
cin >> N >> m;
N /= 10;
int *ZJ_Pri = new int[m + 1](); int *ZJ_Imp = new int[m + 1]();
int *FJ1_Pri = new int[m + 1](); int *FJ1_Imp = new int[m + 1]();
int *FJ2_Pri = new int[m + 1](); int *FJ2_Imp = new int[m + 1]();
for(int i = 1; i <= m; i ++) {
cin >> v >> p >> q;
v /= 10;
if(q == 0) {
ZJ_Pri[i] = v;
ZJ_Imp[i] = v * p;
} else if(FJ1_Pri[q] == 0) {
FJ1_Pri[q] = v;
FJ1_Imp[q] = v * p;
} else {
FJ2_Pri[q] = v;
FJ2_Imp[q] = v * p;
}
}
for(int i = 0; i <= m; i ++) {
for(int j = N; j >= 1; j --) {
if(j >= ZJ_Pri[i]) {
dp[j] = max(dp[j], dp[j - ZJ_Pri[i]] + ZJ_Imp[i]);
}
if(j >= ZJ_Pri[i] + FJ1_Pri[i]) {
dp[j] = max(dp[j], dp[j - ZJ_Pri[i] - FJ1_Pri[i]] + ZJ_Imp[i] + FJ1_Imp[i]);
}
if(j >= ZJ_Pri[i] + FJ2_Pri[i]) {
dp[j] = max(dp[j], dp[j - ZJ_Pri[i] - FJ2_Pri[i]] + ZJ_Imp[i] + FJ2_Imp[i]);
}
if(j >= ZJ_Pri[i] + FJ1_Pri[i] + FJ2_Pri[i]) {
dp[j] = max(dp[j], dp[j - ZJ_Pri[i] - FJ1_Pri[i] - FJ2_Pri[i]]
+ ZJ_Imp[i] + FJ1_Imp[i] + FJ2_Imp[i]);
}
}
}
cout << dp[N] * 10 << endl;
return 0;
}
17.坐标移动
#include <iostream>
using namespace std;
inline bool isANumber(char c) {
if(c >= '0' && c <= '9')
return true;
return false;
}
int main() {
string ss;
while(cin >> ss) {
int x = 0, y = 0;
string::size_type s = 0, e = 0; // 开始 结束
while((e = ss.find(';', s)) != string::npos) {
string str = ss.substr(s, e - s);
int num = 0;
if(str.size() == 2 && isANumber(str[1])) {
num = str[1] - '0';
} else if(str.size() == 3 && isANumber(str[1]) && isANumber(str[2])) {
num = (str[1] - '0') * 10 + str[2] - '0';
}
if(num != 0) {
if(str[0] == 'A') x -= num;
if(str[0] == 'D') x += num;
if(str[0] == 'W') y += num;
if(str[0] == 'S') y -= num;
}
s = e + 1;
}
cout << x << ',' << y << endl;
}
return 0;
}
18.识别有效的ip地址和掩码并进行分类统计
#include <iostream>
#include <string>
#include <vector>
#include <list>
using namespace std;
bool isValid(const string&str, vector<int> &data) {
for(int i = 0; i < str.size(); ++i) {
int j = i;
for(; j <= str.size(); j ++) {
if(str[j] == '.' || j == str.size()) {
string number(str.begin() + i, str.begin() + j);
if(number.empty())
return false;
int num = atoi(number.c_str());
data.push_back(num);
break;
}
}
i = j;
}
if(data.size() != 4)
return false;
for(int i = 0; i < data.size(); i ++) {
if(data[i] < 0 || data[i] > 255)
return false;
}
return true;
}
// 判断ip地址是否正确
bool ipValidHandle(const string &ip, vector<int> &data) {
return isValid(ip, data);
}
// 将整型转化为二进制表示 只取第八位
void dec2binay(int num, list<char> &mask_binary) {
for(int i = 0; i < 8; i ++) {
mask_binary.push_front((num & 1) + '0');
num = num >> 1;
}
}
// 判断子网掩码是否正确
bool maskValidHandle(const string &mask) {
vector<int>res;
list<char>mask_binary;
if(!isValid(mask, res))
return false;
// 将子网掩码 转化为二进制表示
for(int i = res.size() - 1; i >= 0; i --) {
dec2binay(res[i], mask_binary);
}
bool find1 = false, find0 = false;
for(list<char>::iterator it = mask_binary.begin(); it != mask_binary.end(); it ++) {
if(!find1 && *it == '1')
find1 = true;
else if(!find0 && *it == '0')
find0 = true;
else if(find0 && *it == '1')
return false;
}
if(find0 && find1)
return true;
return false;
}
int main () {
string line;
int ip_A = 0, ip_B = 0, ip_C = 0, ip_D = 0;
int ip_E = 0, ip_Err = 0, ip_private = 0;
while(cin >> line) {
for(int i = 0; i < line.size(); i ++) {
if(line[i] == '~') {
string ip(line.begin(), line.begin() + i);
string mask(line.begin() + i + 1, line.end());
vector<int>data;
if(ipValidHandle(ip, data) && maskValidHandle(mask)) {
if(data[0] >=1 && data[0] <= 126)
{
if(data[0] == 10)
ip_private++;
ip_A++;
}
else if(data[0] >=128 && data[0]<=191 )
{
if(data[0] == 172 && (data[1]>=16 && data[1]<=31))
ip_private++;
ip_B++;
}
else if(data[0] >=192 && data[0] <= 223)
{
if(data[0] == 192 && data[1]==168)
ip_private++;
ip_C++;
}
else if(data[0] >=224 && data[0] <= 239)
ip_D++;
else if(data[0] >=240 && data[0] <= 255)
ip_E++;
}
else
++ip_Err;
break;
}
}
}
cout << ip_A<<" "<<ip_B<<" "<<ip_C<<" "<<ip_D<<" "<<ip_E<<" "<<ip_Err<<" "<<ip_private<<endl;
return 0;
}
20.密码验证合格程序
#include <iostream>
#include <string>
using namespace std;
bool check(string str) {
int flag[4] = {0};
if(str.size() <= 8) return false;
for(int i = 0; i < str.size(); i ++) {
if(str[i] >= 'a' && str[i] <= 'z') flag[0] = 1;
else if(str[i] >= '0' && str[i] <= '9') flag[1] = 1;
else if(str[i] >= 'A' && str[i] <= 'Z') flag[2] = 1;
else flag[3] = 1;
}
if(flag[0] + flag[1] + flag[2] + flag[3] < 3) return false;
for(int i = 0; i < str.size(); i ++) {
for(int j = i + 3; j < str.size(); j ++) {
if(str[i] == str[j] && str[i + 1] == str[j + 1] && str[i + 2] == str[j + 2]) {
return false;
}
}
}
return true;
}
int main() {
string str;
while(cin >> str) {
if(check(str)) {
cout << "OK" << endl;
} else {
cout << "NG" << endl;
}
}
return 0;
}