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 | #include <iostream> #include <algorithm> #include <map> #include <cstdio> #include <cstdlib> using namespace std; int n,k,t,xl,yd,xr,yu; int x[1005], y[1005]; bool visited,stayed; inline bool checkCoord(int posx,int posy){ return xl<=posx&&posx<=xr&&yd<=posy&&posy<=yu; } void checkPerson(){ visited = stayed = false; int cnt=0; for(int i=0;i<t;i++){ bool flag=checkCoord(x[i],y[i]); if(!flag)cnt=0; else cnt++; if(cnt>0)visited=true; if(cnt>=k){ stayed=true; break; } } } int main() { ios::sync_with_stdio(false); cin>>n>>k>>t>>xl>>yd>>xr>>yu; int vis=0,stay=0; for(int i=0;i<n;i++){ for(int j=0;j<t;j++){ cin>>x[j]>>y[j]; } checkPerson(); vis+=visited; stay+=stayed; } cout<<vis<<"\n"<<stay<<"\n"; } |
分类目录归档:模拟
CSP 202009-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 | #include <iostream> #include <algorithm> #include <map> #include <cstdio> #include <cstdlib> using namespace std; int n,posx,posy; struct s{ int dist2; int id; bool operator < (const s& s2) const{ if(dist2!=s2.dist2)return dist2 < s2.dist2; return id<s2.id; } }arrS[1000]; inline int calDist(int posx,int posy, int x, int y){ return (posx-x)*(posx-x)+(posy-y)*(posy-y); } int main() { ios::sync_with_stdio(false); cin>>n>>posx>>posy; for(int i=1;i<=n;i++){ int x,y; cin>>x>>y; arrS[i].dist2=calDist(posx,posy,x,y); arrS[i].id=i; } sort(arrS+1,arrS+1+n); for(int i=1;i<=3;i++){ cout<<arrS[i].id<<"\n"; } } |
CSP 202012-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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | import copy file_template = { "isDirectory": False, "fileSize": 0, "childSize": 0, "childQuota": 0, "descendantQuota": 0, "descendants": {} } root_directory = { "isDirectory": True, "fileSize": 0, "childSize": 0, "childQuota": 0, "descendantQuota": 0, "descendants": {} } def filepath_resolver(filepath: str): fpArr = filepath.split('/') fpArr.pop(0) return fpArr def visit(filepath): if filepath == '/': return root_directory fpArr = filepath_resolver(filepath) fileIter = root_directory try: for file in fpArr: fileIter = fileIter["descendants"][file] except KeyError: return None return fileIter def checkQuotaLimit(fileIter, filesize, descendants = True, child = True): if descendants: if fileIter["descendantQuota"] != 0: # 检查后代文件配额 if fileIter["fileSize"] + filesize > fileIter["descendantQuota"]: return False if child: if fileIter["childQuota"] != 0: # 检查孩子文件配额 if fileIter["childSize"] + filesize > fileIter["childQuota"]: return False return True def create(filepath, filesize, isDirectory = False): dstIter = visit(filepath) update_filesize = filesize if dstIter is not None: update_filesize -= dstIter["fileSize"] fpArr = filepath_resolver(filepath) fileIter = root_directory for i in range(len(fpArr)): file = fpArr[i] if file not in fileIter["descendants"].keys(): # 无该目录/文件 curFile = copy.deepcopy(file_template) if i != len(fpArr) - 1: # 非末端文件 if not checkQuotaLimit(fileIter, update_filesize, True, False): return False curFile["isDirectory"] = True else: # 末端文件 if not checkQuotaLimit(fileIter, update_filesize): return False curFile["isDirectory"] = isDirectory curFile["fileSize"] = filesize fileIter["descendants"][file] = curFile else: # 存在该目录/文件 curFile = fileIter["descendants"][file] if i != len(fpArr) - 1: # 非末端文件 if not curFile["isDirectory"]: # 欲进入的新目录文件与普通文件重名 return False else: if not checkQuotaLimit(fileIter, update_filesize, True, False): return False else: # 末端文件 if curFile["isDirectory"] != isDirectory: # 欲创建的同名新文件与旧文件类型冲突 return False else: # 文件已存在,替换文件大小 if not checkQuotaLimit(fileIter, update_filesize): return False curFile["fileSize"] = filesize fileIter = fileIter["descendants"][file] fileIter = root_directory for i in range(len(fpArr)): file = fpArr[i] fileIter["fileSize"] += update_filesize if i == len(fpArr) - 1: fileIter["childSize"] += update_filesize fileIter = fileIter["descendants"][file] return True def remove(filepath): fpArr = filepath_resolver(filepath) dstIter = visit(filepath) if dstIter is None: return True fileIter = root_directory for file in fpArr: fileIter["fileSize"] -= dstIter["fileSize"] if fileIter["descendants"][file] == dstIter: if not dstIter["isDirectory"]: fileIter["childSize"] -= dstIter["fileSize"] break fileIter = fileIter["descendants"][file] fileIter["descendants"].pop(fpArr[-1]) return True def quota(filepath, child_quota, descendant_quota): dstIter = visit(filepath) if dstIter is None: return False if not dstIter["isDirectory"]: return False child_flag = child_quota == 0 or dstIter["childSize"] <= child_quota descendant_flag = descendant_quota == 0 or dstIter["fileSize"] <= descendant_quota if child_flag and descendant_flag: dstIter["childQuota"] = child_quota dstIter["descendantQuota"] = descendant_quota return True else: return False def main(): n = int(input()) for loop in range(n): cmd = input().split() result = False if cmd[0] == "C": result = create(cmd[1], int(cmd[2])) elif cmd[0] == "R": result = remove(cmd[1]) else: result = quota(cmd[1], int(cmd[2]), int(cmd[3])) if result: print("Y") else: print("N") # print(root_directory) if __name__ == '__main__': main() |
CSP 202012-1 期末预测之安全指数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include <iostream> #include <algorithm> #include <cstdio> #include <cstdlib> using namespace std; int main() { long long sum=0,n; long long w,score; ios::sync_with_stdio(false); cin>>n; while(n--){ cin>>w>>score; sum+=w*score; } cout<<max(0ll,sum); } |
Openjudge BJUTACM 19g系列代码
题目链接:
http://bjutacm.openjudge.cn/lianxi/?page=2
UVA1588 换抵挡装置 Kickdown
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 | #include<iostream> #include<string> #include<map> #include<algorithm> #include<cmath> #include<cstring> #include<typeinfo> using namespace std; int solve(string a, string b) { a.append(b.size(), '0'); for (int i = 0; i < a.size()-b.size()+1; i++) { bool flag = true; for (int j = 0; j < b.size(); j++) { if (a[i+j] + b[j] - '0' > '3') { flag = false; break; } } if (flag) return max(a.size()-b.size(), i + b.size()); } return a.size(); } int main() { ios::sync_with_stdio(false); cin.tie(0); string a, b; while (cin >> a >> b)cout<<min(solve(a, b),solve(b,a))<<'\n'; } |
UVA202 循环小数 Repeating Decimals
模拟除法,当出现余数第二次相同时可以记为一个循环节。
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 | #include<iostream> #include<string> #include<map> #include<cmath> #include<cstring> #include<typeinfo> using namespace std; void solve(int a, int b) { int OA = a; string strInt = to_string(a / b); a %= b; a *= 10; string strDec = ""; map<int, int> dict; while (dict.count(a) == 0) { dict[a] = strDec.size(); strDec += to_string(a / b); a %= b; a *= 10; } string strDec1 = strDec.substr(0, dict[a]); string strDec2 = strDec.substr(dict[a]); string strDec3 = ""; if (strDec2.size() > 50) { strDec3 = '(' + strDec2.substr(0, 50) + "...)"; } else { strDec3 = '(' + strDec2 + ")"; } cout << OA << "/" << b << " = " << strInt << "." << strDec1 << strDec3 << '\n'; cout << " " << strDec2.size() << " = number of digits in repeating cycle" << '\n'; cout << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(0); int a, b; while (cin >> a >> b)solve(a, b); } |
蓝桥杯 历届试题 小计算器
问题描述
模拟程序型计算器,依次输入指令,可能包含的指令有
1. 数字:’NUM X’,X为一个只包含大写字母和数字的字符串,表示一个当前进制的数
2. 运算指令:’ADD’,’SUB’,’MUL’,’DIV’,’MOD’,分别表示加减乘,除法取商,除法取余
3. 进制转换指令:’CHANGE K’,将当前进制转换为K进制(2≤K≤36)
4. 输出指令:’EQUAL’,以当前进制输出结果
5. 重置指令:’CLEAR’,清除当前数字
指令按照以下规则给出:
数字,运算指令不会连续给出,进制转换指令,输出指令,重置指令有可能连续给出
运算指令后出现的第一个数字,表示参与运算的数字。且在该运算指令和该数字中间不会出现运算指令和输出指令
重置指令后出现的第一个数字,表示基础值。且在重置指令和第一个数字中间不会出现运算指令和输出指令
进制转换指令可能出现在任何地方
运算过程中中间变量均为非负整数,且小于2^63。
以大写的’A’~’Z’表示10~35
输入格式
第1行:1个n,表示指令数量
第2..n+1行:每行给出一条指令。指令序列一定以’CLEAR’作为开始,并且满足指令规则
输出格式
依次给出每一次’EQUAL’得到的结果
样例输入
7
CLEAR
NUM 1024
CHANGE 2
ADD
NUM 100000
CHANGE 8
EQUAL
样例输出
2040
可以说样例给的挺恶心。会让人想不到,这玩意要考虑还是不考虑优先级!?最后在测试下是不需要考虑优先级的。直接挨着个的算即可。还有一个点是我进制转换写错了(竟然’A’->11,生无可恋脸)一直在调这里。。。。其他没啥了。
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | #include<iostream> #include<deque> #include<algorithm> #include<cassert> #include<cstdlib> #include<string> using namespace std; int n; int radix = 10; string radixS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; deque<long long> sll; deque<string> sop; long long value; inline long long getCorDecimal(char c) { if ('0' <= c && c <= '9')return c - '0'; if ('A' <= c && c <= 'Z')return c - 'A' + 10; assert(false); return -1; } long long Radix2Decimal(string s) { reverse(s.begin(), s.end()); long long cntBase = 1, result = 0; for (int i = 0; i<s.size(); i++) { result += getCorDecimal(s[i])*cntBase; cntBase *= radix; } return result; } string Decimal2Radix(long long v) { string s = ""; while (v) { s.push_back(radixS[v%radix]); v /= radix; } if (s == "")s = "0"; reverse(s.begin(), s.end()); return s; } bool calculate4Back() { if (sop.empty())return false; string & comm = sop.back(); if (comm == "ADD")value += sll.back(); else if (comm == "SUB")value = sll.back() - value; else if (comm == "MUL")value *= sll.back(); else if (comm == "DIV")value = sll.back() / value; else if (comm == "MOD")value = sll.back() % value; else assert(false); sop.pop_back(); sll.pop_back(); sll.push_back(value); return true; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) { string comm, radixV; cin >> comm; if (comm == "NUM") { cin >> radixV; value = Radix2Decimal(radixV); if (!calculate4Back())sll.push_back(value); //cout<<Decimal2Radix(sll.front())<<endl; } else if (comm == "ADD" || comm == "SUB" || comm == "MUL" || comm == "DIV" || comm == "MOD") { sop.push_back(comm); } else if (comm == "CHANGE") { cin >> radix; } else if (comm == "EQUAL") { cout << Decimal2Radix(sll.front()) << endl; } else if (comm == "CLEAR") { sll.clear(); sop.clear(); } } } |
洛谷 P1022 计算器的改良
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 | #define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<cstdio> #include<cstring> #include<string> using namespace std; int ratio = 0, constant = 0; #define IfLetter if (ch <= 'Z'&&ch >= 'A' || ch <= 'z'&&ch >= 'a') #define IfNumber if (ch >= '0'&&ch <= '9' || ch=='+' || ch=='-') int main() { char letter; char ch; int num = 0; bool flag = true; while ((ch = getchar()) != '\n') { IfNumber{ // number(maybe with variable) ungetc(ch, stdin); int re = scanf("%d", &num); //specially prepared for the testcase form like "-a" if (re == 0) { getchar(); ratio -= (flag ? 1 : -1); } ch = getchar(); IfLetter{ ratio += num * (flag ? 1 : -1); letter = ch; } else { constant += num * (flag ? 1 : -1); ungetc(ch, stdin); } num = 0; } else IfLetter{ //pure variable without ratio letter = ch; ratio+=(flag ? 1 : -1); } else if (ch == '=') { flag = false; } } printf("%c=%.3lf", letter, -1.0*constant / ratio); } |
洛谷 P1058 立体图
虽然写的时间很长,但是AC之后随便画图可是很好玩的哦。。。
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | #include<iostream> #include<string> #include<cstring> #include<algorithm> #include<functional> #define ll long long #define pii pair<int,int> #define PINF 0x7fffffff #define NINF 0x80000000 using namespace std; int m, n, am, an; int arr[55][55]; char output[500][500]; int calM(int m, int mh, int tm) { //每行挑最大的执行一次 return (tm - m + 1) * 2 + max(1 + 3 * mh, 1 + 2 * (m - 1)); } inline int calN(int m, int n) { //整体执行一次 return 1 + 4 * n + 2 * m; } inline void printLine(int x, int y) { output[x][y] = output[x][y + 4] = '+'; output[x][y + 1] = output[x][y + 2] = output[x][y + 3] = '-'; } inline void printRow(int x, int y) { output[x][y] = output[x + 2][y - 2] = '+'; output[x + 1][y - 1] = '/'; } inline void printHeight(int x, int y) { output[x][y] = output[x + 3][y] = '+'; output[x + 1][y] = output[x + 2][y] = '|'; } inline void printSpace(int x, int y) { output[x + 1][y] = output[x + 1][y + 1] = output[x + 1][y + 2] = ' '; output[x + 3][y - 1] = output[x + 3][y] = output[x + 3][y + 1] = ' '; output[x + 4][y - 1] = output[x + 4][y] = output[x + 4][y + 1] = ' '; output[x + 2][y + 3] = output[x + 3][y + 3] = ' '; } inline void printCube(int x, int y) { //左上角坐标 printLine(x, y); printLine(x + 2, y - 2); printLine(x + 5, y - 2); printRow(x, y); printRow(x, y + 4); printRow(x + 3, y + 4); printHeight(x + 2, y - 2); printHeight(x + 2, y + 2); printHeight(x, y + 4); printSpace(x, y); } inline void printCube2(int l, int r, int h) { printCube(am - 3 * h - 2 * (m-l+1), 1+2 * (m - l + 1) + (r - 1) * 4); } inline void printAns() { for (int i = 1; i <= am; i++) { cout << output[i] + 1 << endl; } cout << endl; } int main() { cin >> m >> n; an = calN(m, n); for (int i = 1; i <= m; i++) { int mmax = 0; for (int j = 1; j <= n; j++) { cin >> arr[i][j]; mmax = max(mmax, arr[i][j]); } am = max(am, calM(i, mmax, m)); } for (int i = 1; i <= am; i++) { for (int j = 1; j <= an; j++) { output[i][j] = '.'; } } for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { for (int k = 1; k <= arr[i][j]; k++) { printCube2(i, j, k); //printAns(); } } } printAns(); } |
提示:把79行的注释符号去掉可以看到图形生成全过程哦;
写了个好玩的小程序,随机生成数组中的高度哦。。。
各位OIer可以随便试试。。。
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | #include<iostream> #include<string> #include<cstring> #include<algorithm> #include<functional> #include<ctime> #define ll long long #define pii pair<int,int> #define PINF 0x7fffffff #define NINF 0x80000000 using namespace std; int m, n, am, an; int arr[55][55]; char output[500][500]; int calM(int m, int mh, int tm) { //每行挑最大的执行一次 return (tm - m + 1) * 2 + max(1 + 3 * mh, 1 + 2 * (m - 1)); } inline int calN(int m, int n) { //整体执行一次 return 1 + 4 * n + 2 * m; } inline void printLine(int x, int y) { output[x][y] = output[x][y + 4] = '+'; output[x][y + 1] = output[x][y + 2] = output[x][y + 3] = '-'; } inline void printRow(int x, int y) { output[x][y] = output[x + 2][y - 2] = '+'; output[x + 1][y - 1] = '/'; } inline void printHeight(int x, int y) { output[x][y] = output[x + 3][y] = '+'; output[x + 1][y] = output[x + 2][y] = '|'; } inline void printSpace(int x, int y) { output[x + 1][y] = output[x + 1][y + 1] = output[x + 1][y + 2] = ' '; output[x + 3][y - 1] = output[x + 3][y] = output[x + 3][y + 1] = ' '; output[x + 4][y - 1] = output[x + 4][y] = output[x + 4][y + 1] = ' '; output[x + 2][y + 3] = output[x + 3][y + 3] = ' '; } inline void printCube(int x, int y) { //左上角坐标 printLine(x, y); printLine(x + 2, y - 2); printLine(x + 5, y - 2); printRow(x, y); printRow(x, y + 4); printRow(x + 3, y + 4); printHeight(x + 2, y - 2); printHeight(x + 2, y + 2); printHeight(x, y + 4); printSpace(x, y); } inline void printCube2(int l, int r, int h) { printCube(am - 3 * h - 2 * (m - l + 1), 1 + 2 * (m - l + 1) + (r - 1) * 4); } inline void printArray() { cout << "Printing each element of the array:" << endl; for (int i = 1; i <= m; i++) { for (int j = 1; j <= m; j++) { cout << arr[i][j] << " "; } cout << endl; } } inline void printAns() { for (int i = 1; i <= am; i++) { cout << output[i] + 1 << endl; } cout << endl; } int main() { srand((unsigned)time(NULL)); cout << "Please input the side length "n" of the cube array (n<=50):"; int len; cin >> len; m = n = len; an = calN(m, n); cout << "Please input the max height "mh" of cubes in each array item (1<=mh<=100):"; int mh; cin >> mh; cout << "Do you want to show the whole process of filling?(Answer with 0[No] or 1[Yes]):"; bool flag; cin >> flag; for (int i = 1; i <= m; i++) { int mmax = 0; for (int j = 1; j <= n; j++) { arr[i][j] = rand() % mh + 1; mmax = max(mmax, arr[i][j]); } am = max(am, calM(i, mmax, m)); } for (int i = 1; i <= am; i++) { for (int j = 1; j <= an; j++) { output[i][j] = '.'; } } cout << "Automatically generated an array,printed in the following.Press ENTER to continue." << endl; printArray(); cin.get(); cin.get(); for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { for (int k = 1; k <= arr[i][j]; k++) { if (flag)cout << "Printing cube on postion(" << i << "," << j << "," << k << ")." << endl; printCube2(i, j, k); if (flag)printAns(); } } } cout << "Printing Final Result:" << endl; printAns(); } |