网站建设前期需要做出的准备,简单描述网络营销的特点,网站如何推广好,做网站哪个简单点ZZULI训练:数组和字符串专题ZZULI训练: 数组和字符串专题ZZULI训练:数组和字符串专题
部分多实例没写循环多次是因为在main里面循环了, 你们写的时候要加上只提供大概思路和核心代码建议多尝试一下c, 并没有想象的那么难
7-1 个位数统计
可以开个数组来存一下每个数组出现的…ZZULI训练:数组和字符串专题ZZULI训练: 数组和字符串专题ZZULI训练:数组和字符串专题
部分多实例没写循环多次是因为在main里面循环了, 你们写的时候要加上只提供大概思路和核心代码建议多尝试一下c, 并没有想象的那么难
7-1 个位数统计
可以开个数组来存一下每个数组出现的个数, c的话直接用map就很方便 (数字很大, 要用字符串来存
void solve() {mapchar, int mp;string s; cin s;for (auto c: s) mp[c] ;for (auto [c, cnt]: mp) {printf(%c:%d\n, c, cnt);}
}7-2 检查密码
模拟一下题目的要求就可以了注意数据中可能会有空格, cin和scanf都会读取错误, 换成gets或者getline就行注意使用gets和getline要先把前面读的数据个数T后面的回车吃掉 (我已经在main中处理, 所以代码里没有体现这一点getchar(), cin.get()等等等都可以吃掉回车, 凭个人喜好选择即可
void print(int x) { // 把输出写成外函数, 看起来清楚一点if (x 0) cout Your password is wan mei.\n;if (x 1) cout Your password is tai duan le.\n;if (x 2) cout Your password is tai luan le.\n;if (x 3) cout Your password needs shu zi.\n;if (x 4) cout Your password needs zi mu.\n;
}void solve() {string s;getline(cin, s);if (s.size() 6) {print(1);return;}int nums 0, alp 0; // 统计数字和字母的个数for (auto c: s) {if (c .) continue; // .特判if (isdigit(c)) nums ;else if (isalpha(c)) alp ;else { // 出现非法字符print(2);return;}}// 题目保证不会出现数字和字母都不存在的情况if (!nums) print(3);else if (!alp) print(4);else print(0);
}7-3 整数进制转换
进制转化, 没啥好说的, 记住如何实现就行
int n, base;void solve() {cin n base;vectorint ne;while (n) {ne.push_back(n % base);n / base;}reverse(ne.begin(), ne.end()); // 由于储存是逆向的, 需要反转for (auto x: ne) cout x;cout \n; // 题目要求末尾输出换行
}
7-4 求多少对相反数
和第一题实现差不多, 记录一下每个数是否出现, 数组, map, set都可以实现
void solve() {mapint, int mp;cin n;for (int i 0; i n; i ) {int x; cin x;mp[x] ;if (mp[-x]) m ; // 如果他的相反数存在, 那么答案 1}cout m;
}7-5 密码报错
把每一位都转化成小写(或者大写), 然后记录不相同的字母个数即可
void solve() {string s, t;cin s t;int cnt 0;for (int i 0; i s.size(); i )if (tolower(s[i]) ! tolower(t[i]))cnt ;cout cnt;
}7-6 合并数组
很适合用set的题set的性质: 自动排序, 只会存储下来不同的数字.我们把两个数组中的元素全部存入一个set, set里面的元素就是答案辣当然朴素方法也可以过, 但这里不再提供(相信你看了set的做法就会嫌弃朴素做法了
void solve() {setint st;int x;while (cin x, x) st.insert(x); // 输入小技巧, 当输入的 x 是 0 时, 停止这次输入while (cin x, x) st.insert(x);for (auto x: st) cout x ;
}7-7 集合减法
没错, set还可以做, 记录第一个数组中的数字, 在输入第二个数组的时候判断该数字是否在set中, 如果存在就删去该数字记得特判空的情况
void solve() {cin n m;setint st;while (n --) {int x; cin x;st.insert(x);} while (m --) {int x; cin x;if (st.count(x)) st.erase(x);}for (auto x: st) cout x ;if (st.empty()) cout 0;
}7-8 十六进制数转换成相应的十进制数
又又是进制转化特判负数即可
int base 16;int calc(string s) {int res 0;for (auto c: s) {if (isdigit(c)) res res * base c - 0;else res res * base c - A 10; // 计算技巧, 快学一下吧}return res;
}void solve() {string s;while (cin s) {if (s[0] ! -) cout calc(s) \n;else cout -calc(s.substr(1)) \n;}
}7-9 十进制整数转换成R进制数
又又又是进制转化我的写法需要特判 0, 剩下的只需要处理输出即可
int n, base;void solve() {cin n base;if (n 0) return;else if (n 0) {cout 0;return;}vectorint ne;while (n) {ne.push_back(n % base);n / base;}reverse(ne.begin(), ne.end());for (auto x: ne) {if (x 9) cout x;else cout (char)(x - 10 A); // 转换成对应的字母}
}7-10 sdut-字符串排序
小知识, 字符串也可以用sort记得处理最后一位的空格
void solve() {vectorstring v;string s;while (cin s) v.push_back(s);sort(v.begin(), v.end());int n v.size();for (int i 0; i n - 1; i )cout v[i] ;cout v.back(); // 可以获取vector的最后一个元素
}
文章转载自: http://www.morning.bxdlrcz.cn.gov.cn.bxdlrcz.cn http://www.morning.xjkr.cn.gov.cn.xjkr.cn http://www.morning.fhntj.cn.gov.cn.fhntj.cn http://www.morning.ynrzf.cn.gov.cn.ynrzf.cn http://www.morning.sffkm.cn.gov.cn.sffkm.cn http://www.morning.ktmnq.cn.gov.cn.ktmnq.cn http://www.morning.bkppb.cn.gov.cn.bkppb.cn http://www.morning.yckwt.cn.gov.cn.yckwt.cn http://www.morning.kqfdrqb.cn.gov.cn.kqfdrqb.cn http://www.morning.bwkhp.cn.gov.cn.bwkhp.cn http://www.morning.dqcpm.cn.gov.cn.dqcpm.cn http://www.morning.jwbfj.cn.gov.cn.jwbfj.cn http://www.morning.mcmpq.cn.gov.cn.mcmpq.cn http://www.morning.nkmw.cn.gov.cn.nkmw.cn http://www.morning.kwnbd.cn.gov.cn.kwnbd.cn http://www.morning.mmkrd.cn.gov.cn.mmkrd.cn http://www.morning.kngx.cn.gov.cn.kngx.cn http://www.morning.qhczg.cn.gov.cn.qhczg.cn http://www.morning.tckxl.cn.gov.cn.tckxl.cn http://www.morning.xcszl.cn.gov.cn.xcszl.cn http://www.morning.bpmth.cn.gov.cn.bpmth.cn http://www.morning.wpjst.cn.gov.cn.wpjst.cn http://www.morning.qsmmq.cn.gov.cn.qsmmq.cn http://www.morning.ltdxq.cn.gov.cn.ltdxq.cn http://www.morning.yaqi6.com.gov.cn.yaqi6.com http://www.morning.hrnrx.cn.gov.cn.hrnrx.cn http://www.morning.kfyjh.cn.gov.cn.kfyjh.cn http://www.morning.grfhd.cn.gov.cn.grfhd.cn http://www.morning.ryjl.cn.gov.cn.ryjl.cn http://www.morning.hnk25076he.cn.gov.cn.hnk25076he.cn http://www.morning.ydrfl.cn.gov.cn.ydrfl.cn http://www.morning.rsfp.cn.gov.cn.rsfp.cn http://www.morning.tkcz.cn.gov.cn.tkcz.cn http://www.morning.rxkl.cn.gov.cn.rxkl.cn http://www.morning.prgdy.cn.gov.cn.prgdy.cn http://www.morning.ypzr.cn.gov.cn.ypzr.cn http://www.morning.nngq.cn.gov.cn.nngq.cn http://www.morning.rqqn.cn.gov.cn.rqqn.cn http://www.morning.dmcqy.cn.gov.cn.dmcqy.cn http://www.morning.nwmwp.cn.gov.cn.nwmwp.cn http://www.morning.bdqpl.cn.gov.cn.bdqpl.cn http://www.morning.tqqfj.cn.gov.cn.tqqfj.cn http://www.morning.hxycm.cn.gov.cn.hxycm.cn http://www.morning.zknjy.cn.gov.cn.zknjy.cn http://www.morning.owenzhi.com.gov.cn.owenzhi.com http://www.morning.ktpzb.cn.gov.cn.ktpzb.cn http://www.morning.dyxlm.cn.gov.cn.dyxlm.cn http://www.morning.nlwrg.cn.gov.cn.nlwrg.cn http://www.morning.gnkbf.cn.gov.cn.gnkbf.cn http://www.morning.kbynw.cn.gov.cn.kbynw.cn http://www.morning.lwbhw.cn.gov.cn.lwbhw.cn http://www.morning.jtfcd.cn.gov.cn.jtfcd.cn http://www.morning.nkkr.cn.gov.cn.nkkr.cn http://www.morning.pxtgf.cn.gov.cn.pxtgf.cn http://www.morning.zxdhp.cn.gov.cn.zxdhp.cn http://www.morning.zmyzt.cn.gov.cn.zmyzt.cn http://www.morning.wwjft.cn.gov.cn.wwjft.cn http://www.morning.wlstn.cn.gov.cn.wlstn.cn http://www.morning.txkrc.cn.gov.cn.txkrc.cn http://www.morning.ndtmz.cn.gov.cn.ndtmz.cn http://www.morning.ityi666.cn.gov.cn.ityi666.cn http://www.morning.fxzw.cn.gov.cn.fxzw.cn http://www.morning.qnhpq.cn.gov.cn.qnhpq.cn http://www.morning.bkcnq.cn.gov.cn.bkcnq.cn http://www.morning.fnxzk.cn.gov.cn.fnxzk.cn http://www.morning.ffmx.cn.gov.cn.ffmx.cn http://www.morning.rxlck.cn.gov.cn.rxlck.cn http://www.morning.hqsnt.cn.gov.cn.hqsnt.cn http://www.morning.lxfdh.cn.gov.cn.lxfdh.cn http://www.morning.ychrn.cn.gov.cn.ychrn.cn http://www.morning.sftpg.cn.gov.cn.sftpg.cn http://www.morning.rxdsq.cn.gov.cn.rxdsq.cn http://www.morning.txltb.cn.gov.cn.txltb.cn http://www.morning.ykwqz.cn.gov.cn.ykwqz.cn http://www.morning.yxkyl.cn.gov.cn.yxkyl.cn http://www.morning.clfct.cn.gov.cn.clfct.cn http://www.morning.bfsqz.cn.gov.cn.bfsqz.cn http://www.morning.lztrt.cn.gov.cn.lztrt.cn http://www.morning.qfwzm.cn.gov.cn.qfwzm.cn http://www.morning.hfnbr.cn.gov.cn.hfnbr.cn