2014
1.进制转换
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include <bits/stdc++.h> using namespace std;int main () { int N;cin >> N; while (N--){ int x;cin >> x; vector<int >vi; while (x){ if (x%2 ==1 ) vi.push_back (1 ); else vi.push_back (0 ); x/=2 ; } for (int i = vi.size ()-1 ;i>=0 ;i--){ cout << vi[i]; } cout << "\n" ; } return 0 ; }
2.判断回文串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include <bits/stdc++.h> using namespace std;int main () { string s;cin >> s; int sz = s.size (); for (int i = 0 ;i<sz;i++){ if (s[i]!=s[sz-i-1 ]) { printf ("false" ); return 0 ; } } printf ("true" ); return 0 ; }
3.图书管理系统
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 #include <bits/stdc++.h> using namespace std;struct book { string name; int num; double price; int sale; }; vector<book> vb; map<string,book>lib;bool cmp (book a,book b) { return a.sale > b.sale; }int main () { book tmp; tmp.name = "jww" ; tmp.num = 10 ; tmp.sale = 0 ; lib["jww" ]=tmp; tmp.name = "wyl" ; tmp.num = 10 ; tmp.sale = 0 ; lib["wyl" ] = tmp; tmp.name = "ww" ; tmp.num = 10 ; tmp.sale = 20 ; lib["ww" ] = tmp; string s; while (cin >> s){ lib[s].num--; lib[s].sale++; } for (auto i = lib.begin ();i!=lib.end ();i++){ vb.push_back (i->second); } sort (vb.begin (),vb.end (),cmp); for (int i = 0 ;i<vb.size ();i++){ cout << vb[i].name <<"\n" ; } return 0 ; }
2015
1.矩阵对角线元素之和
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include <bits/stdc++.h> using namespace std;int main () { long long sum = 0 ; int n;cin >> n; for (int i = 0 ;i<n;i++){ for (int j = 0 ;;j<n;j++){ int x; cin >> x; if (i==j) sum += x; } } cout << x; return 0 ; }
2.统计字符个数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include <bits/stdc++.h> using namespace std;int main () { string s; int num=0 ,alp=0 ,other=0 ; getline (cin,s); for (int i = 0 ;i<s.size ();i++){ char t = s[i]; if (t>='0' && t<='9' ) num++; else if ((t>='A' && t<='Z' ) || (t>='a' && t <= 'z' )) alp++; else other++; } printf ("数字:%d, 字母:%d, 其他:%d" ,num,alp,other); return 0 ; }
3.单链表统计出现个数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include <bits/stdc++.h> using namespace std;struct node { int val; node* next; node (int x):val (x),next (NULL ){} };int main () { node* head = new node (1 ); head->next = new node (2 ); head->next->next = new node (3 ); head->next->next->next = new node (4 ); int x;cin >> x; node*p = head; int num = 0 ; while (p){ if (p->val==x) num++; p = p->next; } cout << num; return 0 ; }
4.候选人投票
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include <bits/stdc++.h> using namespace std;int main () { int n,m;cin >> n>>m; map<string, int > mp; while (n--){ string s;cin >> s; mp[s] = 0 ; } while (m--){ string s;cin >> s; mp[s] ++; } for (auto i = mp.begin ();i!=mp.end ();i++){ cout << i->first <<" " <<i->second <<"\n" ; } return 0 ; }
2016
1.剔除数字字符 保留剩余字符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <bits/stdc++.h> using namespace std;int main () { string s,temp;cin >> s; for (int i = 0 ;i<s.size ();i++){ if (s[i]>='0' && s[i]<='9' ); else temp += s[i]; } s = temp; return 0 ; }
2.统计各位数上等于x的数字数目
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include <bits/stdc++.h> using namespace std;int main () { int n;cin >> n; while (n--){ string s;int x; cin >> s >> x; if (x>=10 || x<0 ) { printf ("参数越界" ); continue ; } int num = 0 ; for (char t : s){ if (t == x + '0' ) num++; } cout << num <<"\n" ; } return 0 ; }
2017
1.最大区间不相容问题 给定区间范围 求出不相容的最大区间个数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include <bits/stdc++.h> using namespace std;typedef long long ll;struct area { ll left,right; };bool cmp (area a, area b) { if (a.right != b.right) return a.right < b.right; return a.left < b.left; }int main () { int N;cin >> N; vector<area> va; while (N--){ area t; cin >> t.left >> t.right; va.push_back (t); } sort (va.begin (),va.end (),cmp); ll l = -1e10 ; int sm = 0 ; for (int i = 0 ;i<va.size ();i++){ if (va[i].left > l ){ l = va[i].right; sm ++; } } cout << sm; }
2018
1.输入几行字符串,以#结尾,统计每个字符串0-9的个数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include <bits/stdc++.h> using namespace std;int main () { string s; while (getline (cin,s)){ map<char ,int > mp; for (char t: s){ if (t=='#' ) break ; mp[t]++; } for (char c = '0' ;c<='9' ;c++){ cout << c <<" " <<mp[c] <<"\n" ; } } return 0 ; }
2.成绩排名
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include <bits/stdc++.h> using namespace std;struct stu { int grade; string name; int pos; }; vector<stu>vs;bool cmp (stu a,stu b) { if (a.grade != b.grade) return a.grade > b.grade; return a.pos < b.pos; }int main () { int p = 1 ; int lim;cin >> lim; while (lim--){ stu t; cin >> t.name >> t.grade; t.pos = p++; vs.push_back (t); } sort (vs.begin (),vs.end (),cmp); for (int i = 0 ;i<vs.size ();i++){ cout << vs[i].name <<" " << vs[i].grade << "\n" ; } return 0 ; }
3.计算日期
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include <bits/stdc++.h> using namespace std;int main () { int y,m,d;cin >> y >> m >> d; int f=0 ; if (y%4 ==0 && y%100 !=0 ) f = 1 ; int mou[13 ]={0 ,31 ,28 ,31 ,30 ,31 ,30 ,31 ,31 ,30 ,31 ,30 ,31 }; int sum = d; for (int i = 1 ;i<m;i++) sum += mou[i]; if (m>2 && f) sum++; cout << sum; return 0 ; }
4.分组问题 n个球放m个盒子,求多少种装法
组合数3个性质
\(C_n^r = C_n^{n-r}\) 从n个元素 从n个元素中拿出r个,等价于从n个元素丢掉n-r个
\(C_n^r = C_{n-1}^{r}+C_{n-1}^{r-1}\) 帕斯卡公式,可以用DP思路证明,取或者不取第n个元素:
若取第n个元素,则在剩下的n-1个元素中选r-1个;
若不取第n个元素,则在剩下的n-1个元素中选r个
可以用于构造杨辉三角
\(C_n^0+C_n^1+C_n^2+...+C_n^n = 2^n\)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 #include <bits/stdc++.h> using namespace std;int main () { int n,m;cin >> n >> m; int dp[105 ][105 ]; for (int i = 0 ;i<100 ;i++){ dp[i][1 ] = 1 ; dp[0 ][i] = 1 ; } for (int i = 1 ;i<=n;i++){ for (int j = 1 ;j<=m;j++){ if (i>=j) { dp[i][j] = dp[i][j-1 ] + dp[i-j][j]; }else { dp[i][j] = dp[i][i]; } } } cout << dp[n][m]; return 0 ; }
https://www.luogu.com.cn/problem/P1025
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include <bits/stdc++.h> using namespace std;int main () { int n,m;cin >> n >> m; if (n>=m) n-=m; else { cout << 0 ; return 0 ; } int dp[305 ][105 ]; for (int i = 0 ;i<100 ;i++){ dp[i][1 ] = 1 ; dp[0 ][i] = 1 ; } for (int i = 1 ;i<=n;i++){ for (int j = 1 ;j<=m;j++){ if (i>=j) { dp[i][j] = dp[i][j-1 ] + dp[i-j][j]; }else { dp[i][j] = dp[i][i]; } } } cout << dp[n][m]; return 0 ; }
2019
1.覆盖点集 输出一个长方形左上角和右下角坐标 要求可以覆盖所有点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include <bits/stdc++.h> using namespace std;int main () { int ax=100000 ,ay=0 ,bx=0 ,by=100000 ; int x,y; while (cin >> x >> y){ if (x == 0 && y==0 ) break ; ax = min (ax,x); ay = max (ay,y); bx = max (bx,x); by = min (by,y); } cout << ax << " " <<ay<<"\n" ; cout << bx << " " <<by<<"\n" ; return 0 ; }
2.回文串分别使用递归和非递归实现检测回文串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include <bits/stdc++.h> using namespace std;bool isRes (string s) { if (s.size ()<=1 ) return true ; if (s[0 ]==s.back ()){ return isRes (s.substr (1 ,s.size ()-2 )); }else return false ; }int main () { string s;getline (cin,s); bool pb = isRes (s); cout << pb; return 0 ; }
3.逆序对数量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include <bits/stdc++.h> using namespace std;int main () { int n;cin >> n; vector<int > vi; for (int i = 0 ;i<n;i++){ int x;cin >> x; vi.push_back (x); } int num = 0 ; for (int i = 0 ;i<n;i++){ for (int j = i+1 ;j<n;j++) { if (vi[i]>vi[j]) num++; } } cout << num; return 0 ; }
2020
1.数字排列 找到abcd+cadb = 7856的abcd数字
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include <bits/stdc++.h> using namespace std;int main () { for (int a = 0 ;a<=9 ;a++) for (int b = 0 ;b<=9 ;b++) for (int c = 0 ;c<=9 ;c++) for (int d = 0 ;d<=9 ;d++){ if (a*1000 +b*100 +c*10 +d==c*1000 +a*100 +d*10 +b){ cout << a << b << c << d <<"\n" ; } } return 0 ; }
2.反序数 给定两个数字 若反转后的和与反转前的和也是反转关系则符合
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include <bits/stdc++.h> using namespace std;int itos (int a) { int s=0 ; while (a!=0 ){ int t = a%10 ; s = s*10 + t; a/=10 ; } return s; }int main () { int n;cin >> n; while (n--){ int a,b;cin >> a >> b; int c = a+b; a = itos (a); b = itos (b); int d = a+b; if (itos (d)==c) cout <<"yes\n" ; else cout <<"no\n" ; } return 0 ; }