
模拟就是把题目意思转化为代码即自然语言翻译为编程语言1链接https://www.luogu.com.cn/problem/P10672题目3解析模拟分类讨论tip:一定要好好读题其实思路就是题目4代码#includeiostream #includecmath using namespace std; int main(){ int n;cin n; //按指数递减 for(int i n;i0;i--){ int num; cin num; //先去不要的 if(num 0) continue; //符号 //首位-不管-要加 if(i n num 0) cout -; //其他--,0没有 else if(i ! n){ if(num 0) cout ; else cout -; } //数字 int cnt abs(num); if(cnt ! 1 || i 0) cout cnt; //未知量 if(i 1) cout x; else if(i ! 0 ) cout x^ i; } return 0; }P5731 【深基5.习6】蛇形方阵1链接https://www.luogu.com.cn/problem/P57312题目3解析way1:遍历我们可以发现规律旋转具有周期性所以直接4循环用left bottom left right来不断缩边界way2:方向向量矩阵题通法其实我更喜欢叫坐标法通用步骤1定义dx[],dy[] -(0,1)上 (0,-1)下 (1,0)左, (-1,0)右2规则step1沿一个方向走ddl:越界step2用方向向量求下一个方向正确位置循环ddl:完成4代码way1: 遍历#includeiostream using namespace std; int a[10][10]; int main(){ int n; cin n; int top 1,bottom n,left 1,right n; int num 1; while(num n*n){ for(int i left;i right num n*n;i) a[top][i] num;top; for(int i top;i bottom num n*n;i) a[i][right] num; right--; for(int i right;i left num n*n ;i--) a[bottom][i] num;bottom--; for(int i bottom;i top num n*n;i--) a[i][left] num;left; } for(int i 1;i n;i){ for(int j 1;j n;j){ printf(%3d,a[i][j]); } cout endl; } return 0; }way2:方向向量矩阵题通法其实我更喜欢叫坐标法错误示例下一位的位置没有确定#includeiostream using namespace std; int a[10][10];//地图 //按变化顺序定义右下左上 int dx[]{0,1,0,-1}; int dy[]{1,0,-1,0}; int main(){ int n; cin n; int x 1,y 1;//当前z坐标 int cnt 1;//当前的数 int pos 1;//当前数到下一数方向(右下左上) while(cnt n * n){ //存当前位置 a[x][y] cnt; //解决越界 if(x1 1 || x1 n || y1 1 || y1 n || a[x1][y1]){ pos (pos1)%4; } //检验完毕赋值 x xdx[pos],y ydy[pos]; cnt; } for(int i 1;i n;i){ for(int j 1;j n;j){ cout a[i][j] ; } cout endl; } return 0; }正确代码#includeiostream using namespace std; int a[10][10];//地图 //按变化顺序定义右下左上 int dx[]{0,1,0,-1}; int dy[]{1,0,-1,0}; int main(){ int n; cin n; int x 1,y 1;//当前z坐标 int cnt 1;//当前的数 int pos 0;//当前数到下一数方向(右下左上) while(cnt n * n){ //存当前位置 a[x][y] cnt; //判断下一位 //先试验 int xx x dx[pos]; int yy y dy[pos]; //解决越界 if(xx 1 || xx n || yy 1 || yy n || a[xx][yy]){ pos (pos1)%4; xx x dx[pos],yy y dy[pos]; } //检验完毕赋值 x xx,y yy; cnt; } for(int i 1;i n;i){ for(int j 1;j n;j){ printf(%3d,a[i][j]); } cout endl; } return 0; }为什么越界有a[xx][yy]?举 n3 的例子 当程序走到数字9坐标x2,y2此时方向 pos2向左。算预测xx2, yy1xx,yy没有越界但是a[2][1]8已经填过数字如果不判断a[xx][yy]程序还会往左走就会覆盖已经写好的8矩阵直接错乱。全局数组初始全部是 0。a[xx][yy]为真 这个位置≠0 已经填过数要转弯。P1098 [NOIP 2007 提高组] 字符串展开1链接https://www.luogu.com.cn/problem/P10982题目3解析按题意直接写4代码错误代码这是我自己写的代码#includeiostream #includecctype #includealgorithm using namespace std; int main(){ int p1,p2,p3; cin p1 p2 p3; string s; cin s; int l 0; for(int i 1;i (int)s.size()-1;i){ if(s[i] -){ if((s[i-1]as[i1]zs[i1]s[i-1])||(s[i-1]0 s[i1]9s[i1]s[i-1])){ for(int j l;j i-1;j) cout s[j]; l i1; string a ; if(p1 1){ char m s[i-1]1; while(m s[i1]){ for(int k 1;k p2;k){ a m; } m 1; } } else if(p1 2){ char m toupper(s[i-1]1); while(m toupper(s[i1])){ for(int k 1;k p2;k){ a m; } m 1; } } else if(p1 3){ char m s[i-1]1; while(m s[i1]){ for(int k 1;k p2;k){ a *; m; } } } if(p3 2) reverse(a.begin(),a.end()); cout a ; } } } for(int i l;i s.size();i) cout s[i]; return 0; }大家知道错在哪吗-因为数字可能是两位甚至更多修改加上字符串拼接#include iostream #include string #include cctype #include algorithm using namespace std; int main() { int p1, p2, p3; cin p1 p2 p3; string s; cin s; int l 0; for (int i 1; i (int)s.size() - 1; i) { if (s[i] -) { if ((s[i - 1] a s[i 1] z s[i 1] s[i - 1]) || (s[i - 1] 0 s[i 1] 9 s[i 1] s[i - 1])) { for (int j l; j i - 1; j) { cout s[j]; } l i 1; string a ; if (p1 1) { char m s[i - 1] 1; while (m s[i 1]) { for (int k 1; k p2; k) { a m; } m 1; } } else if (p1 2) { char m toupper(s[i - 1] 1); while (m toupper(s[i 1])) { for (int k 1; k p2; k) { a m; } m 1; } } else if (p1 3) { char m s[i - 1] 1; while (m s[i 1]) { for (int k 1; k p2; k) { a *; } m; } } if (p3 2) reverse(a.begin(), a.end()); cout a; } } } for (int i l; i (int)s.size(); i) cout s[i]; return 0; }正常写法存字符串里#include iostream #include algorithm #include string using namespace std; int p1, p2, p3, n; string s; string ret; bool isdig(char ch) { return ch 0 ch 9; } bool islet(char ch) { return ch a ch z; } void add(char left, char right) { string t; for(char ch left 1; ch right; ch) { char tmp ch; if(p1 2 islet(tmp)) tmp - 32; else if(p1 3) tmp *; for(int i 0; i p2; i) { t tmp; } } if(p3 2) reverse(t.begin(), t.end()); ret t; } int main() { cin p1 p2 p3 s; n (int)s.size(); for(int i 0; i n; i) { char ch s[i]; if(s[i] ! -) { ret ch; } else { if(i 0 || i n - 1) { ret ch; } else { char left s[i-1]; char right s[i1]; if( (isdig(left) isdig(right) right left) || (islet(left) islet(right) right left) ) { add(left, right); } else { ret ch; } } } } cout ret endl; return 0; }