这些是我看到的一些有趣的代码
/*
:Zu
iB@B@i iGM;
i@@B@@@B2 7B@B@B@.
@@@@B@B@B@u iM@B@B@B@BL
B@B@N OB@@@BY F@@@B@BMM@B@O
E@B@B .O@B@B@: :BB@@@B@0U7FB@B@
:@B@B. .@B@B@O YB@B@B@OSYJjuU@B@B
@B@B5 rB@B@B7 u@B@B@BNuYj1UFuuB@B@i
2@@@@ N@@@@M 5B@B@BBkjYU252511JMB@B1
@@B@i :@@@B@ J@B@@@MFLju515151FUJE@B@M
F@B@B @@@B@, iB@B@@B1YJ12F152F1511JPB@B@
@B@B: EB@@@. . .::::.... O@@B@BSLuU51F151F15152u2@B@B
L@@B@ 0B@B@ .:rLuk8MB@B@B@B@B@B@@@B@B@B@B@@@@@B@B@G@@@B@PuY2251F1F151F1F1Fu2B@B@.
B@@@Y @B@BB2MB@B@@@B@B@B@B@B@B@B@B@B@B@B@B@B@B@B@B@B@B@B@B@BMujU52F1F151525151511Y@@@Bi
@B@B @B@@@B@@@B@@@B@@@BBZNuu7ri:,,.. B@B@0kN88MB@BEYu252525151F252F1F15UJM@B@J
JB@B@ 0@B@@BE17i,. @B@BUvJYJLJjUj125151F152525151F1F55YO@@BX
B@B@v .B@B@jUU5252221212F2515251F15252F1F2JE@B@O
@B@B. @B@BFJF1F2F2F2F2515151F152F251F5515YEB@B@
.B@@@ B@B@ELU52F152F251525151515252F1F2F2JX@B@B
v@B@B vB@B@uj255F15151F2F15252515151F1S15JNB@B@
PB@BX @B@BBYu2F1F1F2F15152F2515151F152F1jP@B@B
B@B@v @B@BBjJu5251S25151F15251515151F15JNB@@B
BB@@: @B@B@XYL2UF151F15151F2F251F2F251j0@B@B
B@B@i @B@B@BPJJJU251111152525152121Uuv1B@B@B.
FB@B@B vB@B@B@ZSjjLujUu1U1u2u2uuJJLj2XZ@@@B@@:
@B@B@u v@@@B@B@MOXkuujuJjJuu55qEMB@@@B@B@B@Bi
:@B@B@ :P@B@B@@@B@B@B@B@B@B@@@B@B@B@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 UB@B@@@B: OB@@@5 j@B L@B@B@, iBB FMq1OB@
2B@B@MJ :@B@B@: Sj NFXF5i ,@u qB@ .Y. i@O Y@B@BZ.
.. :Ur
k :..,, r ii v: ir .L E ..,,:,iivv :.. ,F : u .:iu1
.u5BjL:i@@rB@ @k :Bi 7@Bu. uMiNBuL ,@SSuEB5qZUO8 :qN1uM@7777: :v@B i2X@vS@ Bv @ 7B7v7i:
.@ ,B UB v@: BX Li O O7u 2 O. 28 .Br,S :B. r7vvrE@7vjLL :B Yiv@:7@j BqiB 7B
:LuB27i8 uG .B@r..:ii,,F@: JO1@ 0;F B7@: Lr rOui k. M1::2@:ii@ ,BqF i7@,78 ON.. rB@Y7Z@
2 @r NY55E: r:@B77rBr :2@.v1ik .@., uM ,OG:Bq Br @5::2@,:iB, ,, @ :;vBiii @8ir vJY7 Mu
B 8E7 B 0O .@ @i :@B7 Mi 5B5: .P: i,BF j i2 5Yi:F@:ii8 .B7G 77J@rvJ: B7 @ P7 BPG
:@k@. @v7rO7 2B B B, i@ @ iJujLr@G77uJU .7r7iNBi;7rr uBL :.:B .. .@ B B BBJ
@i 1ZXqkEq0X0 7@P qk0B5 BE 7@ B B7 ZXF2U8@j11k0r YB.rSuuuGjUkX 1P @PXv@i X@:
.,. .
*/
#include
using namespace std;
#define INF (~(1<<31))
#define INFLL (~(1ll<<63))
#define pb push_back
#define mp make_pair
#define abs(a) ((a)>0?(a):-(a))
#define lalal puts("*******");
#define s1(x) scanf("%d",&x)
#define Rep(a,b,c) for(int a=(b);a<=(c);a++)
#define Per(a,b,c) for(int a=(b);a>=(c);a--)
#define pair1 first
#define pair2 second
typedef long long int LL ;
typedef unsigned long long int uLL ;
const LL MOD = 1e18;
const int N = 100000+5;
const double eps = 1e-8;
const double PI = acos(-1.0);
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
void fre(){
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
}
inline LL _gcd(LL a,LL b){return (b==0)?a:_gcd(b,a%b);}
inline LL _lcm(LL a,LL b){return (a/_gcd(a,b)*b); }
/***********************************************************************/
struct Complex{
double real, image;
Complex(double _real, double _image){
real = _real;
image = _image;
}
Complex(){}
};
Complex *AA = new Complex[N<<2],*BB = new Complex[N<<2];
Complex operator + (const Complex &c1, const Complex &c2){
return Complex(c1.real + c2.real, c1.image + c2.image);
}
Complex operator - (const Complex &c1, const Complex &c2){
return Complex(c1.real - c2.real, c1.image - c2.image);
}
Complex operator * (const Complex &c1, const Complex &c2){
return Complex(c1.real*c2.real - c1.image*c2.image, c1.real*c2.image + c1.image*c2.real);
}
int rev(int id, int len){
int ret = 0;
for(int i = 0; (1 << i) < len; i++){
ret <<= 1;
if(id & (1 << i)) ret |= 1;
}
return ret;
}
Complex A[N<<2];
void FFT(Complex *a, int len, int DFT)
{
for(int i = 0; i < len; i++)
A[rev(i, len)] = a[i];
for(int s = 1; (1 << s) <= len; s++)
{
int m = (1 << s);
Complex wm = Complex(cos(DFT*2*PI/m), sin(DFT*2*PI/m));
for(int k = 0; k < len; k += m)
{
Complex w = Complex(1, 0);
for(int j = 0; j < (m >> 1); j++)
{
Complex t = w*A[k + j + (m >> 1)];
Complex u = A[k + j];
A[k + j] = u + t;
A[k + j + (m >> 1)] = u - t;
w = w*wm;
}
}
}
if(DFT == -1) for(int i = 0; i < len; i++) A[i].real /= len, A[i].image /= len;
for(int i = 0; i < len; i++) a[i] = A[i];
return;
}
int a[N],b[N];
int ans[N<<2];
int main(){
int n,m;
while(~scanf("%d %d",&n,&m)){
int la = n+1,lb = m+1;
int sa,sb;
sa=sb=0;
while((1<while((1<int len = (1<<(max(sa,sb)+1));
for(int i=0;i"%d",&a[i]);
for(int i=0;i"%d",&b[i]);
for(int i=0;i1]):0),0);
BB[i]=Complex(((i1]):0),0);
}
FFT(AA,len,1);
FFT(BB,len,1);
for(int i=0;i*BB[i],ans[i]=0;
FFT(AA,len,-1);
for(int i=0;iint)(AA[i].real+0.5);
for(int i=la+lb-2;i>=0;--i) printf("%d%c",ans[i],i?' ':'\n');
}
return 0;
}
/**
1 1
3 1
1 9
*/
/*
_ooOoo_
o8888888o
88" . "88
(| -_- |)
O\ = /O
____/`---'\____
.' \\| |// `.
/ \\||| : |||// \
/ _||||| -:- |||||- \
| | \\\ - /// | |
| \_| ''\---/'' | |
\ .-\__ `-` ___/-. /
___`. .' /--.--\ `. . __
."" '< `.___\_<|>_/___.' >'"".
| | : `- \`.;`\ _ /`;.`/ - ` : | |
\ \ `-. \_ __\ /__ _/ .-` / /
======`-.____`-.___\_____/___.-`____.-'======
`=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
佛祖保佑 永无BUG
*/
排版好差
自己粘下来看吧
/* Number theory template :
Deals with various aspects of integer, division, modulo, etc.
*/
#include
namespace number {
const double PI = acos (-1);
/* Basic constants & functions :
long long inverse (const long long &x, const long long &mod) :
returns the inverse of x modulo mod.
i.e. x * inv (x) % mod = 1.
int fpm (int x, int n, int mod) :
returns x^n % mod. i.e. Fast Power with Modulo.
void euclid (const long long &a, const long long &b,
long long &x, long long &y) :
solves for ax + by = gcd (a, b).
long long gcd (const long long &a, const long long &b) :
solves for the greatest common divisor of a and b.
long long mul_mod (const long long &a, const long long &b, const long long &mod) :
returns a * b % mod.
long long llfpm (const long long &x, const long long &n, const long long &mod) :
returns x^n % mod.
*/
long long abs (const long long &x) { return x > 0 ? x : -x; }
long long inverse (const long long &x, const long long &mod) {
if (x == 1) return 1;
return (mod - mod / x) * inverse (mod % x, mod) % mod;
}
int fpm (int x, int n, int mod) {
register int ans = 1, mul = x;
while (n) {
if (n & 1) ans = int (1ll * ans * mul % mod);
mul = int (1ll * mul * mul % mod);
n >>= 1;
}
return ans;
}
void euclid (const long long &a, const long long &b,
long long &x, long long &y) {
if (b == 0) x = 1, y = 0;
else euclid (b, a % b, y, x), y -= a / b * x;
}
long long gcd (const long long &a, const long long &b) {
if (!b) return a;
long long x = a, y = b;
while (x > y ? (x = x % y) : (y = y % x));
return x + y;
}
long long mul_mod (const long long &a, const long long &b, const long long &mod) {
long long ans = 0, add = a, k = b;
while (k) {
if (k & 1) ans = (ans + add) % mod;
add = (add + add) % mod;
k >>= 1;
}
return ans;
}
long long llfpm (const long long &x, const long long &n, const long long &mod) {
long long ans = 1, mul = x, k = n;
while (k) {
if (k & 1) ans = mul_mod (ans, mul, mod);
mul = mul_mod (mul, mul, mod);
k >>= 1;
}
return ans;
}
/* Discrete Fourier transform :
int dft::init (int n) : initializes the transformation with dimension n.
void dft::solve (complex *a, int n, int f) :
transforms array a with dimension n to its image representation.
Transforms back when f = 1.
*/
template <int MAXN = 1000000>
struct dft {
typedef std::complex <double> complex;
static complex e[2][MAXN];
int init (int n) {
int len = 1;
for (; len <= 2 * n; len <<= 1);
for (int i = 0; i < len; i++) {
e[0][i] = complex (cos (2 * PI * i / len), sin (2 * PI * i / len));
e[1][i] = complex (cos (2 * PI * i / len), -sin (2 * PI * i / len));
}
return len;
}
void solve (complex *a, int n, int f) {
for (int i = 0, j = 0; i < n; i++) {
if (i > j) std::swap (a[i], a[j]);
for (int t = n >> 1; (j ^= t) < t; t >>= 1);
}
for (int i = 2; i <= n; i <<= 1)
for (int j = 0; j < n; j += i)
for (int k = 0; k < (i >> 1); k++) {
complex A = a[j + k];
complex B = e[f][n / i * k] * a[j + k + (i >> 1)];
a[j + k] = A + B;
a[j + k + (i >> 1)] = A - B;
}
if (f == 1) {
for (int i = 0; i < n; i++)
a[i] = complex (a[i].real () / n, a[i].imag ());
}
}
};
/* Number-theoretic transform :
void ntt::solve (int *a, int n, int f, int mod, int prt) :
transfers a[n] to its image representation.
Converts back if f = 1.
Requries specific mod and corresponding prt to work. (given in MOD and PRT)
int ntt::crt (int *a, int mod) :
fixes the results a from module 3 primes to a certain module mod.
*/
template <int MAXN = 1000000>
struct ntt {
void solve (int *a, int n, int f, int mod, int prt) {
for (register int i = 0, j = 0; i < n; i++) {
if (i > j) std::swap (a[i], a[j]);
for (register int t = n >> 1; (j ^= t) < t; t >>= 1);
}
for (register int i = 2; i <= n; i <<= 1) {
static int exp[MAXN];
exp[0] = 1;
exp[1] = fpm (prt, (mod - 1) / i, mod);
if (f == 1) exp[1] = fpm (exp[1], mod - 2, mod);
for (register int k = 2; k < (i >> 1); k++) {
exp[k] = int (1ll * exp[k - 1] * exp[1] % mod);
}
for (register int j = 0; j < n; j += i) {
for (register int k = 0; k < (i >> 1); k++) {
register int &pA = a[j + k], &pB = a[j + k + (i >> 1)];
register int A = pA, B = int (1ll * pB * exp[k] % mod);
pA = (A + B) % mod;
pB = (A - B + mod) % mod;
}
}
}
if (f == 1) {
register int rev = fpm (n, mod - 2, mod);
for (register int i = 0; i < n; i++) {
a[i] = int (1ll * a[i] * rev % mod);
}
}
}
int MOD[3] = {1045430273, 1051721729, 1053818881}, PRT[3] = {3, 6, 7};
int crt (int *a, int mod) {
static int inv[3][3];
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
inv[i][j] = (int) inverse (MOD[i], MOD[j]);
static int x[3];
for (int i = 0; i < 3; i++) {
x[i] = a[i];
for (int j = 0; j < i; j++) {
int t = (x[i] - x[j] + MOD[i]) % MOD[i];
if (t < 0) t += MOD[i];
x[i] = int (1LL * t * inv[j][i] % MOD[i]);
}
}
int sum = 1, ret = x[0] % mod;
for (int i = 1; i < 3; i ++) {
sum = int (1LL * sum * MOD[i - 1] % mod);
ret += int (1LL * x[i] * sum % mod);
if (ret >= mod) ret -= mod;
}
return ret;
}
};
/* Chinese remainder theroem :
bool crt::solve (const std::vector > &input,
std::pair &output) :
solves for an integer set x = output.first + k * output.second
that satisfies x % input[i].second = input[i].first.
Returns whether a solution exists.
*/
struct crt {
long long fix (const long long &a, const long long &b) {
return (a % b + b) % b;
}
bool solve (const std::vector <std::pair <long long, long long> > &input,
std::pair <long long, long long> &output) {
output = std::make_pair (1, 1);
for (int i = 0; i < (int) input.size (); ++i) {
long long number, useless;
euclid (output.second, input[i].second, number, useless);
long long divisor = gcd (output.second, input[i].second);
if ((input[i].first - output.first) % divisor) {
return false;
}
number *= (input[i].first - output.first) / divisor;
number = fix (number, input[i].second);
output.first += output.second * number;
output.second *= input[i].second / divisor;
output.first = fix (output.first, output.second);
}
return true;
}
};
/* Miller Rabin :
bool miller_rabin::solve (const long long &) :
tests whether a certain integer is prime.
*/
struct miller_rabin {
int BASE[12] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
bool check (const long long &prime, const long long &base) {
long long number = prime - 1;
for (; ~number & 1; number >>= 1);
long long result = llfpm (base, number, prime);
for (; number != prime - 1 && result != 1 && result != prime - 1; number <<= 1)
result = mul_mod (result, result, prime);
return result == prime - 1 || (number & 1) == 1;
}
bool solve (const long long &number) {
if (number < 2) return false;
if (number < 4) return true;
if (~number & 1) return false;
for (int i = 0; i < 12 && BASE[i] < number; ++i)
if (!check (number, BASE[i]))
return false;
return true;
}
};
/* Pollard Rho :
std::vector pollard_rho::solve (const long long &) :
factorizes an integer.
*/
struct pollard_rho {
miller_rabin is_prime;
const long long threshold = 13E9;
long long factorize (const long long &number, const long long &seed) {
long long x = rand() % (number - 1) + 1, y = x;
for (int head = 1, tail = 2; ; ) {
x = mul_mod (x, x, number);
x = (x + seed) % number;
if (x == y)
return number;
long long answer = gcd (abs (x - y), number);
if (answer > 1 && answer < number)
return answer;
if (++head == tail) {
y = x;
tail <<= 1;
}
}
}
void search (const long long &number, std::vector<long long> &divisor) {
if (number > 1) {
if (is_prime.solve (number))
divisor.push_back (number);
else {
long long factor = number;
for (; factor >= number;
factor = factorize (number, rand () % (number - 1) + 1));
search (number / factor, divisor);
search (factor, divisor);
}
}
}
std::vector <long long> solve (const long long &number) {
std::vector <long long> ans;
if (number > threshold)
search (number, ans);
else {
long long rem = number;
for (long long i = 2; i * i <= rem; ++i)
if (number % i)
++i;
else {
ans.push_back (i);
rem %= i;
}
}
return ans;
}
};
/* Adaptive Simpson's method :
double simpson::solve (double (*f) (double), double l, double r, double eps) :
integrates f over (l, r) with error eps.
*/
struct simpson {
double area (double (*f) (double), double l, double r) {
double m = l + (r - l) / 2;
return (f (l) + 4 * f (m) + f (r)) * (r - l) / 6;
}
double solve (double (*f) (double), double l, double r, double eps, double a) {
double m = l + (r - l) / 2;
double left = area (f, l, m), right = area (f, m, r);
if (fabs (left + right - a) <= 15 * eps) return left + right + (left + right - a) / 15.0;
return solve (f, l, m, eps / 2, left) + solve (f, m, r, eps / 2, right);
}
double solve (double (*f) (double), double l, double r, double eps) {
return solve (f, l, r, eps, area (f, l, r));
}
};
}
using namespace number;
typedef std::complex <double> complex;
template <int MAXN> std::complex <double> dft ::e[2][MAXN];
const int MAXN = 1000000;
dft <> m_dft;
int N, M;
complex A[MAXN], B[MAXN], C[MAXN];
int main () {
scanf ("%d%d", &N, &M);
for (int i = 0; i < N + 1; ++i) {
int tmp;
scanf ("%d", &tmp);
A[i] = complex (tmp, 0);
}
for (int i = 0; i < M + 1; ++i) {
int tmp;
scanf ("%d", &tmp);
B[i] = complex (tmp, 0);
}
int size = N + M + 1;
int len = m_dft.init (size);
m_dft.solve (A, len, 0);
m_dft.solve (B, len, 0);
for (int i = 0; i < len; ++i)
C[i] = A[i] * B[i];
m_dft.solve (C, len, 1);
for (int i = 0; i < N + M + 1; ++i)
printf ("%d%c", int (C[i].real () + .5), " \n"[i == size - 1]);
return 0;
}
超级强的模板
#include
#include
#include
#include
#define st(now) ((isvar[now])?var[st[now]]:st[now])
using namespace std;
int In,input[1000],len,dep=0,funsum,retu,PA,read=0;bool RETU;
int varsum[100000],var[3000000],funstart[1000],pa[1000],pasum[1000],wei[3000000][20],weisum[3000000];
string par[1000][100],code;
set<char> s1;//实词集合(控制、函数、变量)
set<char> s2;//表达式字符集合
set<char> s3;//数字集合
map<string,int> m1[100000];//变量映射
map<string,int> m2;//函数映射
void init()//初始化
{
In=2;
cin >> input[1] >> input[2];
code="int main(){ int a,b;cin >> a >> b;cout << a+b;}";
bool flag=1;
for(char ch='a';ch<='z';ch++) s1.insert(ch),s2.insert(ch);
for(char ch='A';ch<='Z';ch++) s1.insert(ch),s2.insert(ch);
for(char ch='0';ch<='9';ch++) s1.insert(ch),s2.insert(ch),s3.insert(ch);
s1.insert('_');s2.insert('_');
s2.insert('(');s2.insert(')');
s2.insert('+');s2.insert('-');s2.insert('*');s2.insert('/');s2.insert('%');
s2.insert('!');s2.insert('&');s2.insert('|');s2.insert('=');s2.insert('<');s2.insert('>');s2.insert('^');
s2.insert('[');s2.insert(']');
code.erase(0,1);
for(int i=0;i<=code.length();)
if((code[i]==' '||code[i]=='\n'||code[i]=='\t')&&((!s1.count(code[i-1])) || (!s1.count(code[i+1]))))
code.erase(i,1);
else i++;
varsum[0]=1;len=code.length();RETU=0;PA=0;
}
int level(int &now)//符号转成运算级
{
switch(code[now])
{
case'<':if(code[++now]=='=')return 6;
else {now--;return 8;}
case'>':if(code[++now]=='=')return 7;
else {now--;return 9;}
case'=':if(code[++now]=='=')return 4;
else {now--;return 0;}
case'!':now++;return 5;
case'|':now++;return 1;
case'&':now++;return 2;
case'^':return 3;
case'+':return 10;
case'-':return 11;
case'*':return 12;
case'/':return 13;
case'%':return 14;
}
}
bool dayu(int a,int b)
{
if(a==0) return 0;
if(a>=b) return 1;
if((a==4)&&(b==5))return 1;
if((a>=6)&&(a<=9)&&(b>=6)&&(b<=9)) return 1;
if((a==10)&&(b==11)) return 1;
if((a>=12)&&(a<=14)&&(b>=12)&&(b<=14)) return 1;
return 0;
}
int word(int now)//找下一个单词(变量 int 函数之类的)
{
int end;
for(end=now;s1.count(code[end]);end++);
return end;
}
int express(int now)//找下一个表达式
{
int end;
for(end=now;s2.count(code[end]);end++);
return end;
}
int pass(int now)
{
while(code[now]!=';') now++;
return now+1;
}
int block(int now)//跳过一段程序
{
if(code[now]=='{')
{
int dep=1;
for(now++;dep;now++) if(code[now]=='{') dep++;else if(code[now]=='}') dep--;
return now;
}
else
{
int next=word(now);
if((next-now==3 && code.substr(now,3)=="for")||(next-now==5 && code.substr(now,5)=="while"))
{
int p=next+1,deep=1;
while(deep)
{
if(code[p]=='(') deep++;
if(code[p]==')') deep--;
p++;
}
return block(p);
}
else
if(next-now==2 && code.substr(now,2)=="if")
{
int p=next+1,deep=1;
while(deep)
{
if(code[p]=='(') deep++;
if(code[p]==')') deep--;
p++;
}
p=block(p);
next=word(p);
if(next-p==4 && code.substr(p,4)=="else")
return block(next);
else return p;
}
else
{
int p=now;
while(code[p]!=';') p++;
return p+1;
}
}
}
int num(int &now)//得到最近的十进制数
{
int sum=0;
while(s3.count(code[now]))
sum=sum*10+code[now++]-'0';
return sum;
}
int getsize(int &now,int k)//得到数组的大小
{
int sum=1;
while(code[now]=='[')
{
now++;
int tem=num(now);
wei[k][++weisum[k]]=tem;
sum*=tem;
now++;
}
return sum;
}
int focus(int &next,int first)//得到所要的变量在数组中的位置
{
int calc(int &now);
int sum=0,SUM=1,i=0;
if(code[next]=='[')
{
while(code[next]=='[')
{
next++;i++;
sum+=SUM*calc(next);
SUM*=wei[first][i];
next++;
}
}
return sum;
}
int newfun(int beg,int end)//定义新函数
{
m2[code.substr(beg,end-beg)]=++funsum;int i=0;
for(int next=word(end+1);code[end++]!=')';end=next,next=word(end+1))
if(next-end!=3 || code.substr(end,3)!="int")
par[funsum][++i]=code.substr(end,next-end);
pasum[funsum]=i;
funstart[funsum]=end++;
for(int dep=1;dep;end++) if(code[end]=='{') dep++;else if(code[end]=='}') dep--;
return end;
}
int newvar(int beg)//定义新变量
{
int end;
for(end=beg;code[end]!=';';beg=end+1)
{
end=word(beg),m1[dep][code.substr(beg,end-beg)]=varsum[dep];
int from=varsum[dep];weisum[varsum[dep]]=0;
for(varsum[dep]+=getsize(end,varsum[dep]);from<=varsum[dep];from++) var[from]=0;
}
return end+1;
}
int newdep()//新的一层
{
varsum[dep+1]=varsum[dep];m1[dep+1]=m1[dep];dep++;
}
int calc(int &now)//表达式求值
{
int run(int now,bool isfun);
int fh[20],st[20];bool isvar[20];int top=0,pre[10],presum=0;
for(;code[now]!=';' && code[now]!=')' &&(code[now]!='<' || code[now+1]!='<')&&(code[now]!=']')&&(code[now]!=',');)
if(s3.count(code[now]))
{
st[++top]=num(now);
while(presum>0)
{
if(pre[presum]==1) st[top]=!st[top];
if(pre[presum]==2) st[top]=-st[top];
presum--;
}
isvar[top]=0;fh[top]=-1;
}
else
if(s1.count(code[now]))
if(m1[dep].count(code.substr(now,word(now)-now)))
{
int next=word(now),first=m1[dep][code.substr(now,word(now)-now)];
st[++top]=first+focus(next,first);isvar[top]=1;
while(presum>0)
{
st[top]=st(top);isvar[top]=0;
if(pre[presum]==1) st[top]=!st[top];
if(pre[presum]==2) st[top]=-st[top];
presum--;
}
fh[top]=-1;now=next;
}
else
{
int End=word(now),all=0,end=End+1;
while(code[end]!=')')
pa[++all]=calc(end),end+=code[end]==',';
PA=m2[code.substr(now,End-now)];retu=0;
run(funstart[PA],1);
st[++top]=retu;fh[top]=-1;isvar[top]=0;
while(presum>0)
{
if(pre[presum]==1) st[top]=!st[top];
if(pre[presum]==2) st[top]=-st[top];
presum--;
}
now=end+1;
}
else
if(code[now]=='(')
{
now++;
st[++top]=calc(now);
while(presum>0)
{
if(pre[presum]==1) st[top]=!st[top];
if(pre[presum]==2) st[top]=-st[top];
presum--;
}
isvar[top]=0;fh[top]=-1;now++;
}
else
if(!top || fh[top]!=-1)
while(code[now]=='!' || code[now]=='-' || code[now]=='+')
switch(code[now])
{
case'!':pre[++presum]=1;now++;break;
case'-':pre[++presum]=2;now++;break;
case'+':now++;break;
}
else
{
int newlevel=level(now);
while(top>1 && dayu(fh[top-1],newlevel))
switch(fh[--top])
{
case 0:var[st[top]]=st(top+1);st[top]=st(top+1);isvar[top]=0;break;
case 1:st[top]=st(top)||st(top+1);isvar[top]=0;break;
case 2:st[top]=st(top)&&st(top+1);isvar[top]=0;break;
case 3:st[top]=st(top)^st(top+1);isvar[top]=0;break;
case 4:st[top]=st(top)==st(top+1);isvar[top]=0;break;
case 5:st[top]=st(top)!=st(top+1);isvar[top]=0;break;
case 6:st[top]=st(top)<=st(top+1);isvar[top]=0;break;
case 7:st[top]=st(top)>=st(top+1);isvar[top]=0;break;
case 8:st[top]=st(top)1);isvar[top]=0;break;
case 9:st[top]=st(top)>st(top+1);isvar[top]=0;break;
case 10:st[top]=st(top)+st(top+1);isvar[top]=0;break;
case 11:st[top]=st(top)-st(top+1);isvar[top]=0;break;
case 12:st[top]=st(top)*st(top+1);isvar[top]=0;break;
case 13:st[top]=st(top)/st(top+1);isvar[top]=0;break;
case 14:st[top]=st(top)%st(top+1);isvar[top]=0;break;
}
fh[top]=newlevel;now++;
}
for(top--;top>0;top--)
switch(fh[top])
{
case 0:var[st[top]]=st(top+1);st[top]=st(top+1);isvar[top]=0;break;
case 1:st[top]=st(top)||st(top+1);isvar[top]=0;break;
case 2:st[top]=st(top)&&st(top+1);isvar[top]=0;break;
case 3:st[top]=st(top)^st(top+1);isvar[top]=0;break;
case 4:st[top]=st(top)==st(top+1);isvar[top]=0;break;
case 5:st[top]=st(top)!=st(top+1);isvar[top]=0;break;
case 6:st[top]=st(top)<=st(top+1);isvar[top]=0;break;
case 7:st[top]=st(top)>=st(top+1);isvar[top]=0;break;
case 8:st[top]=st(top)1);isvar[top]=0;break;
case 9:st[top]=st(top)>st(top+1);isvar[top]=0;break;
case 10:st[top]=st(top)+st(top+1);isvar[top]=0;break;
case 11:st[top]=st(top)-st(top+1);isvar[top]=0;break;
case 12:st[top]=st(top)*st(top+1);isvar[top]=0;break;
case 13:st[top]=st(top)/st(top+1);isvar[top]=0;break;
case 14:st[top]=st(top)%st(top+1);isvar[top]=0;break;
}
if(code[now]==';')
now++;
return st(1);
}
int sentence(int now)//执行语句
{
int run(int now,bool isfun);
int next=word(now);
if(code[now]=='{')
next=run(now,0);
else
if(next-now==2 && code.substr(now,2)=="if")
{
newdep();
next++;
int end=express(next);
if(calc(next))
{
next++;next=sentence(next);
int end=word(next);
if(end-next==4 && code.substr(next,4)=="else")
next=block(end);
}
else
{
next++;
next=block(next);
int end=word(next);
if(end-next==4 && code.substr(next,4)=="else")
next=sentence(end+(code[end]==' '));
}
dep--;
}
else
if(next-now==3 && code.substr(now,3)=="for")
{
newdep();
int judge;
judge=(code[next+1]==';')?next+2:sentence(next+1);int ju=judge;
if(RETU) return next;
int done=express(judge)+1;
int blo=done,deep=1;
while(deep)
{
if(code[blo]=='(') deep++;
if(code[blo]==')') deep--;
blo++;
}
while((code[ju]==';')?1:calc(ju))
{
ju=judge,sentence(blo);
if(RETU) return next;
sentence(done);
if(RETU) return next;
}
next=block(blo);
dep--;
}
else
if(next-now==5 && code.substr(now,5)=="while")
{
newdep();
int judge=next+1,ju=judge;
if(RETU) return next;
int blo=judge,deep=1;
while(deep)
{
if(code[blo]=='(') deep++;
if(code[blo]==')') deep--;
blo++;
}
while(calc(ju))
{
sentence(blo),ju=judge;
if(RETU) return next;
}
next=block(blo);
dep--;
}
else
if(next-now==3 && code.substr(now,3)=="cin")
{
int qian=next,hou=next+1;
while(code[qian]!=';')
{
qian=word(hou+1);
int first=m1[dep][code.substr(hou+1,qian-hou-1)];
var[first+focus(qian,first)]=input[++read];
hou=qian+1;
}
next=hou;
}
else
if(next-now==4 && code.substr(now,4)=="cout")
{
int qian=next,hou=next+1;
while(code[qian]!=';')
{
qian=hou+1;
while((code[qian]!='<' || code[qian+1]!='<' )&&(code[qian]!=';')) qian++;
if(qian-hou==5 && code.substr(hou+1,4)=="endl")
puts("");
else
hou++,printf("%d",calc(hou));
hou=qian+1;
}
next=hou;
}
else
if(next-now==7 && code.substr(now,7)=="putchar")
next++,printf("%c",calc(next)),next+=2;
else
if(next-now==3 && code.substr(now,3)=="int")
next=newvar(next+1);
else
if(next-now==6 && code.substr(now,6)=="return")
next++,retu=calc(next),RETU=1;
else
{
calc(now);
next=now;
}
return next;
}
int run(int now,bool isfun)//从某个{开始运行
{
newdep();now++;
if(PA)
for(int i=1;i<=pasum[PA];i++)
m1[dep][par[PA][i]]=varsum[dep],var[varsum[dep]]=pa[i],varsum[dep]++;
PA=0;
for(int next=now;code[next]!='}'&& !RETU;now=next)
next=sentence(now);
dep--;if(isfun)RETU=0;now++;
return now;
}
int main()
{
init();
for(int i=0,j;i//处理全局变量和函数
{
i=word(i)+1;j=word(i);//过滤int
if(code[j]=='(') j=newfun(i,j);
else j=newvar(i);
}
run(funstart[m2["main"]],1);
return 0;
}
牛逼的a+ b problem