牛客网练习
自学
琉璃色的星辉
这个作者很懒,什么都没留下…
展开
-
牛客网 考研真题 KY43 全排列
贴个代码方便复习,思路是每个元素轮流当本次全排列第一个元素,同时将待排序列变短,直至只剩下一个元素,输出结果。#include <iostream>#include <string>#include <algorithm>#include <vector>using namespace std;vector<string> result; //存储结果方便排序void swapa(string& ss, int i,原创 2021-05-06 22:50:40 · 133 阅读 · 0 评论 -
牛客网 考研真题 KY4 代理服务器
思路一道贪心的题目,贪心策略是要在所有代理服务器IP全部在访问服务器IP中出现过的情况下找到那个最后一个代理服务器IP的出现位置,作为这一次访问使用的IP,然后再把除刚使用过的最后那个IP外所有代理服务器IP设置为未出现过重复进行。注意如果只给一个代理IP而访问IP第一个就是此IP的话则无解。代码实现#include <iostream>#include <string>#include <vector>#include <map>using n原创 2021-04-14 21:47:43 · 268 阅读 · 0 评论 -
牛客网 考研真题 KY39 大整数的因子
思路虽然我有一篇文章摘抄了《王道机试指南》的大整数类,但我发现用它取余操作总是不对,无奈自己写了一个,思路不难,就是模拟正常列竖式除法。代码实现#include <iostream>using namespace std;int main() { int big[30]; //存放大整数 string input; //存放输入 while (cin >> input) { if (input == "-1") { break; }原创 2021-04-13 19:07:49 · 85 阅读 · 0 评论 -
牛客网 KY191 矩阵幂
#include <iostream>using namespace std;struct Matrix { int matrix[10][10]; int row, col; Matrix(int r, int c):row(r),col(c){}};Matrix Multiply(Matrix x, Matrix y) { Matrix answer(x.row, y.col); for (int i = 0; i < answer.row; ++i) {原创 2021-03-20 13:34:33 · 98 阅读 · 0 评论 -
牛客网 KY105 整除问题
参考下述解法:1.素数筛2.对n!质因子分解算法3.对a质因子分解算法int nFac[MAXN]={0};//记录n!的质因数分解后的结果,n!存在质因数i,则aFac[i]=指数int aFac[MAXN]={0};//记录a的质因数分解的结果,a存在质因数i,则aFac[i]=指数比较n!和a的相同质因数的指数相除的最小值为所求。例如:6!=24+32+5^110=21*51有公共质因数:2 指数相除为4/1=45 指数相除为1/1=1所以答案为1这里是将a质因子分解后在n原创 2021-03-05 19:38:09 · 199 阅读 · 0 评论 -
牛客网 KY3 约数的个数
约数即因子,质数,约数类题目先想到用所给数开根号来缩小循环次数,简化计算,对于本题,以sqrt(num)为界,若比它小有一个因子,则比它大一定存在另一个因子,此时结果加2,最后再判断sqrt(num)是否为其一个因子若是则结果加1,即可。程序代码#include <iostream>using namespace std;int main(){ int n; while (cin >> n) { for (int i = 0; i < n; ++i) {原创 2021-03-05 12:25:23 · 109 阅读 · 0 评论 -
牛客网 KY183 素数
#include <iostream>#include <vector>#include <cmath>using namespace std;int main(){ long long n; while (cin >> n) { vector<int> re; //所有素数存放在这里 for (int j = 2; j < n; ++j) { bool t = true; for (in原创 2021-03-04 19:47:31 · 169 阅读 · 2 评论 -
牛客网 KY235 进制转换2
#include <iostream>#include <cstring>#include <vector>#include <sstream>using namespace std;string mul(string a, int b) { //字符串乘法 int carry = 0; for (int i = a.size() - 1; i >= 0; --i) { int current = carry + (a原创 2021-03-04 16:27:28 · 149 阅读 · 2 评论 -
牛客网 KY26 10进制 VS 2进制
#include <iostream>#include <vector>using namespace std;string divide(string str, int x) { //字符串除法 int remainder = 0; for (int i = 0; i < str.size(); ++i) { int current = remainder * 10 + str[i] - '0'; str[i] = current / x + '原创 2021-03-03 16:07:13 · 116 阅读 · 2 评论 -
牛客网 KY30 进制转换
思路对输入字符串对2取模再除以2,对于字符串取模运算,转换为对字符串最低位数进行取模;字符串除法则是把字符串从高到低逐位除以除数,如果不能整除,那么保留它除以除数的余数,余数乘10后和低位相加后一起处理,由于有前置0,故应取首个非0位之后的字符。程序代码#include <iostream>#include <vector>using namespace std;int qu_yu(string a) { //取余 return (a[a.length() -原创 2021-03-03 12:09:57 · 89 阅读 · 0 评论 -
牛客网 KY187 二进制数
位运算#include <iostream>using namespace std;void calculate(int n) { int temp = n & 1; if (n == 0) return; calculate(n >> 1); cout << temp;}int main(){ int n; while (cin >> n) { calculate(n); cout << endl原创 2021-03-02 23:55:10 · 171 阅读 · 0 评论 -
牛客网 KY79 浮点数加法
#include <iostream>#include <string>using namespace std;string stringadd(string a, string b) {//先在整数部分补0使两数小数点在同一位置 int a1 = a.find('.'); int b1 = b.find('.'); string c1 = (a1 > b1) ? a : b; string c2 = (a1 > b1) ? b : a;原创 2021-02-28 15:35:30 · 109 阅读 · 0 评论 -
牛客网 KY46 单词替换
#include <iostream>#include <string>using namespace std;int main(){ string s; while (getline(cin, s)) { string a, b; getline(cin, a); getline(cin, b); int found = s.find(a); //找第一次出现位置 while (found != string::npos) { if (原创 2021-02-28 13:03:15 · 113 阅读 · 0 评论 -
牛客网 KY228 找位置
思路利用map将位置与字符对应,然后根据其出现顺序进行查找,其中关键一步是按照value去查找key值,将多次出现的字符位置进行记录后再输出。代码如下#include <iostream>#include <algorithm>#include <string>#include <vector>#include <map>using namespace std;int main(){ string input; whi原创 2021-02-26 00:01:25 · 173 阅读 · 0 评论 -
牛客网 KY117 奥运排序问题
最直观的解法就是四个排序全部进行一次,记录最好的一次输出,解法如下:#include <iostream>#include <algorithm>#include <string>using namespace std;class country {public: int gold; int medal; int population; int num; //国家编号 float gold_population; //金牌人口比原创 2021-02-21 00:10:16 · 120 阅读 · 0 评论 -
牛客网 KY2 成绩排序
这道题重载运算符就可以完成#include <iostream>#include <algorithm>using namespace std;class student {public: string name; int grade; int i; //记录输入序号 void studentinit(string a, int b,int c) { this->name = a; this->grade = b; this原创 2021-02-19 20:30:19 · 113 阅读 · 0 评论 -
牛客网 KY6 手机键盘
数据预处理,自己用二维数组模拟一个键盘#include <iostream>#include <string>using namespace std;int main(){ char keyboard[8][4] = { {'a','b','c','0'},{'d','e','f','0'},{'g','h','i','0'},{'j','k','l','0'},{'m','n','o','0'},{'p','q','r','s'},{'t','u','v','0'}原创 2021-02-08 22:05:48 · 4564 阅读 · 0 评论 -
牛客网 KY108 Day of Week
用当天的日子对应的星期来推断所给日子的星期#include <iostream>#include <string>#include <vector>using namespace std;bool isLeapYear(int year) { if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0) return true; else return false;}int mai原创 2021-02-07 20:47:28 · 135 阅读 · 0 评论 -
牛客网 KY258 日期累加 | KY250 日期类 | KY111 日期差值
依次根据月份判断天数,再根据天数修改月份,最后根据月份修改年份#include <iostream>#include <iomanip>#include <set>using namespace std;bool isLeapYear(int year) { if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0) return true; else return false;}原创 2021-02-06 21:29:38 · 119 阅读 · 0 评论 -
牛客网 KY222 打印日期
#include <iostream>#include <iomanip>#include <set>using namespace std;bool isLeapYear(int year) { if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0) return true; else return false;}void calculate(int a[15], int&am原创 2021-02-06 20:36:21 · 99 阅读 · 0 评论 -
牛客网 KY147 Hello World for U
考虑先将其写入二维数组后直接输出得到结果#include <iostream>#include <string>using namespace std;int main(){ string str; while (cin >> str) { char output[28][28]; for (int i = 0; i < 28; i++) { //初始化二维数组 for (int j = 0; j < 28; j++原创 2021-02-05 20:16:29 · 138 阅读 · 0 评论