运行期(runtime)判断
下面的程序可以在运行期判断 endianess:
int IsBigEndian (void) { static const int v =1; return *(char*)&v?0:1; } |
熟读而精思,循序而渐进,厚积而薄发。
运行期(runtime)判断
下面的程序可以在运行期判断 endianess:
int IsBigEndian (void) { static const int v =1; return *(char*)&v?0:1; } |
Description
A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, ..., xm > another sequence Z = < z1, z2, ..., zk > is a subsequence of X if there exists a strictly increasing sequence < i1, i2, ..., ik > of indices of X such that for all j = 1,2,…,k, xij = zj. For example, Z = < a, b, f, c > is a subsequence of X = < a, b, c, f, b, c > with index sequence < 1, 2, 4, 6 >. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y.
Introduction
A good way to get into an argument with a computer programmer is to attempt to explain why the language they are using is not as good as the one you are using. Most of the programmers I know are positively religious over their Operating System (see my other article), their development language and finally their text editor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | bool fun(unsigned n) { assert(n >0); //乘方不为正 if (!(n & (n-1))) //n的二进制只有一个1,即n==2^m; { int offset = 0;//"1"位的偏移量 while (n != 1) { n>>=1; offset++; } if (offset && !(offset&1))//偏移量为偶数,即 n == 2^(2*m),亦即n == 4^m; { return true; } } return false; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include <iostream> using namespace std; //! declaration and implementation of class Matrix class Matrix { public: Matrix(int n = 2); Matrix(const Matrix& rf); int& operator [][](int x, int y);//! operator[][] int& operator [][](int x, int y) const;//! operator[][]const Matrix& operator *=(Matrix& rt);//! operator *= private: int *p_Matrix;//! pointer to the 2D array int m_n;//! dimension of the matrix }; |
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 | ORG 8000H ; position the localtion of the beginning of the commands LJMP START ; jump to the user's commands ORG 8100H ; position the localtion of the program START: MOV R0, #08H ; times of right or left shifts MOV A, #0FEH ; set LED which is lighten JNB P3.2, LOOP_L ; watch the switch which controls the shift direction LOOP_R: RR A ; branch 1: right shift MOV P1, A LCALL DELAY ; delay so that we can notice the flickering DJNZ R0, LOOP_R SJMP START LOOP_L: RL A ; branch 1: right shift MOV P1, A LCALL DELAY DJNZ R0, LOOP_L SJMP START DELAY: PUSH 06H ; subProgram impliments delaying PUSH 07H MOV R7, #0FFH LABEL1: MOV R6, #0FFH LABEL2: DJNZ R6, $ DJNZ R7, LABEL1 POP 07H POP 06H RET END |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include <iostream> #include <cmath> using namespace std; double Sqrt(double x); int main() { double x; cin>>x; cout<<Sqrt(x)<<endl; return 0; } double Sqrt(double x) { const double eps = 1E-6; double x0 = x / 2, x1 = (x0 + x / x0) / 2; while(fabs(x1 - x0) > eps) { x0 = x1; x1 = (x0 + x / x0) / 2; } return x0; } |
1 2 3 4 5 6 | /*
*Filename: tri_Diagonal
*Description a solution to the tri-diagonal linear formulation
*author: HouFenglin@物理0605/DUT
*Time: 13/04/2009
*/ |
/*
*Filename: Gauss_Elimination.cpp
*Description a solution to the linear formulations
*author: HouFenglin@物理0605/DUT
*Time: 13/04/2009
*/ |