博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[HW] OJ记录20题之四
阅读量:6819 次
发布时间:2019-06-26

本文共 13606 字,大约阅读时间需要 45 分钟。

1 表示数字

#include 
#include
using namespace std;int main(){ string str; while (cin>>str) { string res = ""; for (int i = 0; i < str.size(); i++) { if (str[i] >= '0' && str[i] <= '9') { if (i == 0 || str[i - 1] < '0' || str[i - 1] > '9') { res += '*'; } res += str[i]; if (i == str.size() - 1 || (str[i + 1] < '0' || str[i + 1] > '9')) { res += '*'; } } else { res += str[i]; } } cout<
<

2 查找二进制整数中1的个数

#include 
using namespace std;int main(){ int n; while (cin>>n) { int cnt = 0; while (n) { ++cnt; n &= n - 1; } cout<
<

3 字符串通配符

#include 
#include
#include
#include
using namespace std;void fun(string pat, string dest){ int patLen = pat.size(); int destLen = dest.size(); vector
> dp(patLen + 1, vector
(destLen + 1, 0)); dp[0][0] = 1; for (int i = 1; i <= patLen; i++) { for (int j = 1; j <= destLen; j++) { if (pat[i - 1] == '?' || abs(pat[i - 1] - dest[j - 1]) == 0 || abs(pat[i - 1] - dest[j - 1]) == 32) { dp[i][j] = dp[i - 1][j - 1]; } else if (pat[i - 1] == '*') { dp[i][j] = dp[i - 1][j - 1] || dp[i][j - 1] || dp[i - 1][j]; } } } if (dp[patLen][destLen]) { cout<<"true"<
>pat>>str) { fun(pat, str); } return 0;}

4 百钱买百鸡问题

#include 
using namespace std;int main(){ int n; cin>>n; for (int i = 0; i <= 100 / 5; i++) { for (int j = 0; j <= 100 / 3; j++) { int k = 100 - i - j; if (5 * i + 3 * j + k / 3.0 == 100) { cout<
<<" "<
<<" "<
<

5 尼科彻斯定理

#include 
using namespace std;int main(){ int n; while (cin>>n) { int num = n * n - n + 1; cout<

6 计算字符串的相似度

#include 
#include
#include
using namespace std;int fun(string str1, string str2){ int len1 = str1.size(); int len2 = str2.size(); vector
> dp(len1 + 1, vector
(len2 + 1, 0)); for (int i = 0; i <= len2; i++) { dp[0][i] = i; } for (int i = 0; i <= len1; i++) { dp[i][0] = i; } for (int i = 1; i <= len1; i++) { for (int j = 1; j <= len2; j++) { dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + 1; dp[i][j] = min(dp[i][j], dp[i -1][j - 1] + (str1[i - 1] == str2[j - 1] ? 0 : 1)); } } return dp[len1][len2];}int main(){ string str1, str2; while (cin>>str1>>str2) { int res = fun(str1, str2); cout<<"1/"<
<

7 求最大连续bit数

#include 
using namespace std;int main(){ int n; while (cin>>n) { int cnt = 0; int res = 0; for (int i = 0; i < 8; i++) { if (n & 0x1 << i) { ++cnt; } else { res = max(res, cnt); cnt = 0; } } res = max(res, cnt); cout<
<

8 合并表记录

#include 
#include
using namespace std;//合并表记录int main(){ int n; while (cin>>n) { vector
> src(n, vector
(2, 0)); for (int i = 0; i < n; i++) { cin>>src[i][0]>>src[i][1]; } vector
> dest; for (int i = 0; i < n; i++) { bool b = false; for (int j = 0; j < dest.size(); j++) { if (dest[j][0] == src[i][0]) { dest[j][1] += src[i][1]; b = true; } } if (!b) { dest.push_back(src[i]); } } for (int i = 0; i < dest.size(); i++) { cout<
<
<
<

9 密码强度等级

#include 
#include
using namespace std;int main(){ string str; while (getline(cin, str)) { int len = str.size(); int small = 0; int big = 0; int number = 0; int mark = 0; for (int i = 0; i < len; i++) { if (str[i] >= 'a' && str[i] <= 'z') { small++; } else if (str[i] >= 'A' && str[i] <= 'Z') { big++; } else if (str[i] >= '0' && str[i] <= '9') { number++; } else if ((str[i] >= 0x21 && str[i] <= 0x2F) || (str[i] >= 0x3A && str[i] <= 0x40) || (str[i] >= 0x5B && str[i] <= 0x60) || (str[i] >= 0x7B && str[i] <= 0x7E)) { mark++; } } int score = 0; if (len <= 4) { score += 5; } else if (len <= 7) { score += 10; } else { score += 25; } if (big != 0 && small != 0) { score += 20; } else if (big != 0 || small != 0) { score += 10; } if (number == 1) { score += 10; } else if (number > 1) { score += 20; } if (mark == 1) { score += 10; } else if (mark > 1) { score += 25; } if (score >= 90) { cout<<"VERY_SECURE"<
= 80) { cout<<"SECURE"<
= 70) { cout<<"VERY_STRONG"<
= 60) { cout<<"STRONG"<
= 50) { cout<<"AVERAGE"<
= 25) { cout<<"WEAK"<

10 201301 JAVA题目0-1级

#include 
#include
using namespace std;void fun(vector
num, int sum, int pos, bool& stat){ if (pos < 0) { return; } if (num[pos] == sum) { stat = true; } fun(num, sum - num[pos], pos - 1, stat); fun(num, sum, pos - 1, stat);}int main(){ int n; while (cin>>n) { vector
num; int sum = 0; int sum3 = 0; int sum5 = 0; int temp; for (int i = 0; i < n; i++) { cin>>temp; sum += temp; if (temp % 5 == 0) { sum5 += temp; } else if (temp % 3 == 0) { sum3 += temp; } else { num.push_back(temp); } } if (sum % 2 != 0 || sum3 > sum / 2 || sum5 > sum / 2) { cout<<"false"<

11 计票统计

#include 
#include
using namespace std;int main(){ int can; while (cin>>can) { vector
arr(can, 0); vector
res(can + 1, 0); for (int i = 0; i < can; i++) { cin>>arr[i]; } int vote; cin>>vote; char ch; for (int i = 0; i < vote; i++) { cin>>ch; bool b = false; for (int j = 0; j < can; j++) { if (arr[j] == ch) { res[j]++; b = true; } } if (!b) { res[can]++; } } for (int i = 0; i < can; i++) { cout<
<<" : "<
<

12 等差数列

#include 
using namespace std;int main(){ int n; while (cin>>n) { cout<<1.5 * n * n + 0.5 * n<

13 求解立方根

#include 
#include
#include
using namespace std;double fun(double d){ if (d < 0) { return -fun(-d); } double left = 0; double right = d; while (left < right) { double mid = (left + right) / 2; if (mid * mid * mid < d - 1e-6) { left = mid; } else if (mid * mid * mid > d + 1e-6) { right = mid; } else { return mid; } } return left;}int main(){ double d; while (cin>>d) { printf("%.1f\n", fun(d)); } return 0;}

14 求最小公倍数

#include 
using namespace std;int yue(int n, int m){ if (n < m) { swap(n, m); } int a = n % m; while (a) { n = m; m = a; a = n % m; } return m;}int bei(int n, int m){ return n * m / (yue(n, m));}int main(){ int n, m; while (cin>>n>>m) { cout<

15 明明的随机数

#include 
#include
#include
using namespace std;int main(){ int n; while (cin>>n) { vector
num(n, 0); for (int i = 0; i < n; i++) { cin>>num[i]; } sort(num.begin(), num.end()); int i = 0; for (int j = 1; j < num.size(); j++) { if (num[j] != num[i]) { num[++i] = num[j]; } } for (int j = 0; j <= i; j++) { cout<
<

16 数字颠倒

#include 
#include
using namespace std;int main(){ string str; while (getline(cin, str)) { for (int i = 0, j = str.size() - 1; i < j; i++, j--) { char ch = str[i]; str[i] = str[j]; str[j] = ch; } cout<
<

17 查找兄弟单词

#include 
#include
#include
#include
#include
using namespace std;bool isBrother(string str1, string str2){ if (str1.compare(str2) == 0 || str1.size() != str2.size()) { return false; } int b[26] = {
0}; for (int i = 0; i < str1.size(); i++) { b[str1[i] - 'a']++; } for (int i = 0; i < str2.size(); i++) { b[str2[i] - 'a']--; } for (int i = 0; i < 26; i++) { if (b[i] != 0) { return false; } } return true;}int main(){ int n; while (cin>>n) { vector
words(n, ""); for (int i = 0; i < n; i++) { cin>>words[i]; } vector
> brothers; for (int i = 0; i < n; i++) { vector
bro; for (int j = 0; j < n; j++) { if (j == i) { continue; } if (isBrother(words[i], words[j])) { bro.push_back(j); } } brothers.push_back(bro); } //sort for (int i = 0; i < n; i++) { int len = brothers[i].size(); for (int j = 0; j < len - 1; j++) { for (int k = 0; k < len - 1 - j; k++) { if (words[k].compare(words[k + 1]) > 0) { swap(brothers[i][k], brothers[i][k + 1]); } } } } string str; cin>>str; int index; cin>>index; for (int i = 0; i < n; i++) { if (str.compare(words[i]) == 0) { cout<
<
<

18 素数伴侣

#include 
#include
#include
#include
#include
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) { vector
num(n , 0); for (int i = 0; i < n; i++) { cin>>num[i]; } vector
dp(n + 1, 0); for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { if (isPrime(num[i - 1] + num[j - 1])) { dp[i] = max(dp[i], dp[i - 1] + dp[j - 1] - dp[j] + 1); } else { dp[i] = max(dp[i], dp[i - 1]); } } } cout<
<

19 字符串合并处理

#include 
#include
#include
using namespace std;char map[16] ={
'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};char fun(char ch){ int n; if (ch >= '0' && ch <= '9') { n = ch - '0'; } else if (ch >= 'a' && ch <= 'f') { n = ch - 'a' + 10; } else if (ch >= 'A' && ch <= 'F') { n = ch - 'A' + 10; } n = (n & 1) << 3 | (n & 1 << 1) << 1 | (n & 1 << 2) >> 1 | (n & 1 << 3) >> 3; return map[n];}int main(){ string str1, str2; while (cin>>str1>>str2) { string str = str1 + str2; for (int i = 0; i < str.size() - 1; i++) { for (int j = 0; j < str.size() - 2 - i; j++) { if (str[j] > str[j + 2]) { swap(str[j], str[j + 2]); } } } for (int i = 0; i < str.size(); i++) { if (str[i] >= '0' && str[i] <= '9' || str[i] >= 'a' && str[i] <= 'f' || str[i] >= 'A' && str[i] <= 'F') { str[i] = fun(str[i]); } } cout<
<

20 计算日期到天数转换

#include 
using namespace std;int m[12] = {
31,28,31,30,31,30,31,31,30,31,30,31};bool fun(int year){ if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { return true; } return false;}int main(){ int year, month, day; while (cin>>year>>month>>day) { int sum = 0; for (int i = 0; i < month - 1; i++) { sum += m[i]; } sum += day; if (fun(year) && sum > 59) { sum++; } cout<
<

转载地址:http://iglzl.baihongyu.com/

你可能感兴趣的文章
自定义栈类型,具有找到站内最小元素的min函数 ,且min(),pop(),push()函数的时间复杂度为O(1)...
查看>>
redhat 5下源码安装nginx服务
查看>>
python优缺点分析及python种类,编码-课堂笔记及课后总结
查看>>
request和session作用域的意义
查看>>
大数据环境下互联网行业数据仓库/数据平台的架构之漫谈-续【转】
查看>>
SQLServer的最大连接数 超时时间已到 但是尚未从池中获取连接
查看>>
webpack学习笔记
查看>>
windows2008 RDP修改默认端口
查看>>
Nginx配置信息损毁又无备份时如何恢复
查看>>
libc.so.6: cannot open shared object file: No such file or diretory
查看>>
mysql oracle静默 一键安装脚本
查看>>
LINQ分页工具
查看>>
一张图让你明白IOS中bounds和frame的区别
查看>>
tex tab example
查看>>
week8
查看>>
mybatis 中文文档
查看>>
Docker学习笔记_进入正在运行的Docker容器
查看>>
Python tkinter 副窗体置顶和取消置顶
查看>>
head first 设计模式 02 观察者模式
查看>>
包学会之浅入浅出Vue.js:升学篇
查看>>