Because the simulation didn't want to write, now I think it's better to write it, so I started this chapter sequence

Simulation is: how to say the problem stem, how to write the code.

Simulation is the general solution to all problems, because there is no problem in solving problems according to the meaning of the problem.

It's just the difference between code sizes

If the simulation level is not high, it is not recommended to write code with the mentality of "not writing thousands of lines of simulation".

The "A+B Problem" we mentioned earlier is a kind of simulation.

Let's look at some examples.

(the level of Si Yan is limited. It's all about water.

The following questions are from Luogu

P2669 gold coin

NOIP2015 PJ

#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> using namespace std; int main(){ int s,d; int ans(0);//Another way of writing assignment cin>>s; d=s; //When n gold coins are received every day for N consecutive days, the knight will receive N+1 gold coins every day for N+1 consecutive days. //Start simulation for(int i=1;i<=d;i++){//Enumeration days if(s>i){ s-=i;//Calculated by time period ans+=i*i; } else {//Last period of time ans+=s*i; break; } } cout<<ans<<endl; return 0; }

P1008 three strikes

NOIP1998 PJ

It's not very difficult to think, but it's really complicated to think one by one

I wrote the code when I started. hana was an array name randomly selected at that time. The comments were also typed at that time

#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> using namespace std; int i,j,l,n,m,x,y,z,d,e,f,g,h,k,v,hana[9]; int main() { for(i=1; i<=3; i++)//The minimum number must not exceed 300 for(j=1; j<=9; j++) for(l=1; l<=9; l++) { x=100*i+10*j+l; y=x*2; z=x*3; /* a=x/100; b=(x%100)/10; c=x%10;*/ //Can not d=y/100; e=(y%100)/10; f=y%10; g=z/100; h=(z%100)/10; k=z%10; hana[0]=i; hana[1]=j; hana[2]=l; hana[3]=d; hana[4]=e; hana[5]=f; hana[6]=g; hana[7]=h; hana[8]=k; for(m=0; m<=7; m++)//Enumerate the last two numbers for(n=m+1; n<=8; n++) { if(hana[m]==hana[n]||hana[m]==0||hana[n]==0) //If (Hana [M]! = Hana [n]) {if (E! = 0 & & F! = 0 & & H! = 0 & & K! = 0 & & Z < 999) is not allowed, otherwise, if the cycle only goes once, it will judge down and repeat the numbers v=1;//Tag variables prevent 0 and equal numbers } if(v!=1&&z<=999)//*After 2, * 3, zero may appear in the tens and ones of Y and Z; cannot be equal or unequal continuously; control Z < 1000 cout<<x<<" "<<y<<" "<<z<<endl; v=0;//It is better to define the tag as a local variable and clear it when it is used up. It can also be placed after hana[8]=k } return 0; }

- P2615 magic magic square

NOIP2015 TG

#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<cctype> #define maxn 50 #define MAXN 2000 using namespace std; int n,lin[MAXN],col[MAXN],table[maxn][maxn]; inline int read(int &x){//Quick read, this version cannot be used for negative numbers char c=0; x=0; while(!isdigit(c)) c=getchar(); while(isdigit(c)) x=x*10+c-'0',c=getchar(); } inline int write(int x){//Write fast if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); } int main(){ read(n); //Fill in 1 first. table[1][n/2+1]=1;//table records whether there are numbers in a row or a column lin[1]=1;//lin[i] record the number I in the line col[1]=n/2+1;//col[i] record the number I in the column for(int k=2;k<=n*n;k++){//Judge and fill in from 2 if(lin[k-1]==1&&col[k-1]!=n){ //If (K − 1) is in the first row but not in the last column, fill in K in the last row and (K − 1) is in the right column of the column //Now I'm lazy table[n][col[k-1]+1]=k; lin[k]=n; col[k]=col[k-1]+1; } else if(lin[k-1]!=1&&col[k-1]==n){ table[lin[k-1]-1][1]=k; lin[k]=lin[k-1]-1; col[k]=1; } else if(lin[k-1]==1&&col[k-1]==n){ table[2][n]=k; lin[k]=2; col[k]=n; } else if(lin[k-1]!=1&&col[k-1]!=n){ if(table[lin[k-1]-1][col[k-1]+1]==0){ table[lin[k-1]-1][col[k-1]+1]=k; lin[k]=lin[k-1]-1; col[k]=col[k-1]+1; } else{ table[lin[k-1]+1][col[k-1]]=k; lin[k]=lin[k-1]+1; col[k]=col[k-1]; } } } for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ write(table[i][j]); putchar(' '); } printf("\n"); } return 0; }

After all, I'm a vegetable chicken