Beauty of Programming

Source: Internet
Author: User

1.1 cpu usage Problems


[Cpp]
# Include <iostream>
# Include <ctime>
# Include <cmath>
# Include <Windows. h>
Using namespace std;
 
// Method 1
Void main ()
{
INT64 start = 0;
Int busy = 10;
Int idle = busy;
Cout <"CPU usage problems ";
While (true)
{
Start = GetTickCount ();
While (GetTickCount ()-start) <= busy );
Sleep (idle );
}
}
 
// Method 2
Int main ()
{
For (;;)
{
For (int I = 0; I <9600000; I ++ );
// For (int I = 0; I <21360000; I ++); // 4-core 2.67 Ghz
Sleep (10 );
}
Return 0;
}
 
// Zhengxuan Curve
Constdouble SPLIT = 0.01;
Const int COUNT = 200;
Const double PI = 3.14159265;
Const int INTERVAL = 300;
Void main ()
{
DWORD busy [COUNT], idle [COUNT];
Int half = INTERVAL/2;
Double radian = 0.0;
For (int I = 0; I <COUNT; I ++)
{
Busy [I] = DWORD (sin (PI * radian) * half + half );
Idle [I] = INTERVAL-busy [I];
Radian + = 0.01;
}
DWORD start = 0;
Int j = 0;
While (true)
{
Start = GetTickCount ();
J = j % COUNT;
While (GetTickCount ()-start) <= busy [j]);
Sleep (idle [j]);
J ++;
}
}

# Include <iostream>
# Include <ctime>
# Include <cmath>
# Include <Windows. h>
Using namespace std;

// Method 1
Void main ()
{
INT64 start = 0;
Int busy = 10;
Int idle = busy;
Cout <"CPU usage problems ";
While (true)
{
Start = GetTickCount ();
While (GetTickCount ()-start) <= busy );
Sleep (idle );
}
}

// Method 2
Int main ()
{
For (;;)
{
For (int I = 0; I <9600000; I ++ );
// For (int I = 0; I <21360000; I ++); // 4-core 2.67 Ghz
Sleep (10 );
}
Return 0;
}

// Zhengxuan Curve
Constdouble SPLIT = 0.01;
Const int COUNT = 200;
Const double PI = 3.14159265;
Const int INTERVAL = 300;
Void main ()
{
DWORD busy [COUNT], idle [COUNT];
Int half = INTERVAL/2;
Double radian = 0.0;
For (int I = 0; I <COUNT; I ++)
{
Busy [I] = DWORD (sin (PI * radian) * half + half );
Idle [I] = INTERVAL-busy [I];
Radian + = 0.01;
}
DWORD start = 0;
Int j = 0;
While (true)
{
Start = GetTickCount ();
J = j % COUNT;
While (GetTickCount ()-start) <= busy [j]);
Sleep (idle [j]);
J ++;
}
} Number of CPU core operation cycles


[Cpp]
# Include <iostream>
Using namespace std;
Inline _ int64 GetCPUTickCount ()
{
_ Asm
{
Rdtsc;
}
}
Void main ()
{
Cout <"Number of CPU core operation cycles" <GetCPUTickCount () <endl;
System ("pause ");
}

# Include <iostream>
Using namespace std;
Inline _ int64 GetCPUTickCount ()
{
_ Asm
{
Rdtsc;
}
}
Void main ()
{
Cout <"Number of CPU core operation cycles" <GetCPUTickCount () <endl;
System ("pause ");
}

 

1.2 generals

 

[Cpp]
# Include <iostream>
Using namespace std;
 
// Method 1
Struct {
Unsigned char a: 4;
Unsigned char B: 4;
} I;
Void main ()
{
For (I. a = 1; I. a <= 9; I. a ++)
For (I. B = 1; I. B <= 9; I. B ++)
If (I. a % 3! = I. B % 3)
Printf ("A = % d, B = % d \ n", I. a, I. B );
System ("pause ");
}
 
// Method 2
# Define HALF_BITS_LENGTH 4
// This value is half the length of the memory unit. In this question, it is 4 bits.
# Define FULLMASK 255
// This number indicates a mask of all bits. In binary representation, it is 11111111.
# Define LMASK (FULLMASK <HALF_BITS_LENGTH)
// This macro indicates the mask of the Left bits. In the binary representation, it is 11110000.
# Define RMASK (FULLMASK> HALF_BITS_LENGTH)
// This number indicates the mask of the right bits. In the binary representation, it indicates 00001111.
# Define RSET (B, n) (B = (LMASK & B) ^ n ))
// Set this macro to n on the Right of B
# Define LSET (B, n) (B = (RMASK & B) ^ (n <HALF_BITS_LENGTH )))
// Set this macro to n on the left of B.
# Define RGET (B) (RMASK & B)
// The macro gets the value to the right of B.
# Define LGET (B) (LMASK & B)> HALF_BITS_LENGTH)
// The macro gets the value on the left of B.
# Define GRIDW 3
// This number indicates the row width of the moving range.
# Include <stdio. h>
# Define HALF_BITS_LENGTH 4
# Define FULLMASK 255
# Define LMASK (FULLMASK <HALF_BITS_LENGTH)
# Define RMASK (FULLMASK> HALF_BITS_LENGTH)
# Define RSET (B, n) (B = (LMASK & B) ^ n ))
# Define LSET (B, n) (B = (RMASK & B) ^ (n <HALF_BITS_LENGTH )))
# Define RGET (B) (RMASK & B)
# Define LGET (B) (LMASK & B)> HALF_BITS_LENGTH)
# Define GRIDW 3
Int main ()
{
Unsigned char B;
For (LSET (B, 1); LGET (B) <= GRIDW * GRIDW; LSET (B, (LGET (B) + 1 )))
For (RSET (B, 1); RGET (B) <= GRIDW * GRIDW; RSET (B, (RGET (B) + 1 )))
If (LGET (B) % GRIDW! = RGET (B) % GRIDW)
Printf ("A = % d, B = % d \ n", LGET (B), RGET (B ));
System ("pause ");
Return 0;
}

# Include <iostream>
Using namespace std;

// Method 1
Struct {
Unsigned char a: 4;
Unsigned char B: 4;
} I;
Void main ()
{
For (I. a = 1; I. a <= 9; I. a ++)
For (I. B = 1; I. B <= 9; I. B ++)
If (I. a % 3! = I. B % 3)
Printf ("A = % d, B = % d \ n", I. a, I. B );
System ("pause ");
}

// Method 2
# Define HALF_BITS_LENGTH 4
// This value is half the length of the memory unit. In this question, it is 4 bits.
# Define FULLMASK 255
// This number indicates a mask of all bits. In binary representation, it is 11111111.
# Define LMASK (FULLMASK <HALF_BITS_LENGTH)
// This macro indicates the mask of the Left bits. In the binary representation, it is 11110000.
# Define RMASK (FULLMASK> HALF_BITS_LENGTH)
// This number indicates the mask of the right bits. In the binary representation, it indicates 00001111.
# Define RSET (B, n) (B = (LMASK & B) ^ n ))
// Set this macro to n on the Right of B
# Define LSET (B, n) (B = (RMASK & B) ^ (n <HALF_BITS_LENGTH )))
// Set this macro to n on the left of B.
# Define RGET (B) (RMASK & B)
// The macro gets the value to the right of B.
# Define LGET (B) (LMASK & B)> HALF_BITS_LENGTH)
// The macro gets the value on the left of B.
# Define GRIDW 3
// This number indicates the row width of the moving range.
# Include <stdio. h>
# Define HALF_BITS_LENGTH 4
# Define FULLMASK 255
# Define LMASK (FULLMASK <HALF_BITS_LENGTH)
# Define RMASK (FULLMASK> HALF_BITS_LENGTH)
# Define RSET (B, n) (B = (LMASK & B) ^ n ))
# Define LSET (B, n) (B = (RMASK & B) ^ (n <HALF_BITS_LENGTH )))
# Define RGET (B) (RMASK & B)
# Define LGET (B) (LMASK & B)> HALF_BITS_LENGTH)
# Define GRIDW 3
Int main ()
{
Unsigned char B;
For (LSET (B, 1); LGET (B) <= GRIDW * GRIDW; LSET (B, (LGET (B) + 1 )))
For (RSET (B, 1); RGET (B) <= GRIDW * GRIDW; RSET (B, (RGET (B) + 1 )))
If (LGET (B) % GRIDW! = RGET (B) % GRIDW)
Printf ("A = % d, B = % d \ n", LGET (B), RGET (B ));
System ("pause ");
Return 0;
}


1.12 elevator Scheduling


[Cpp]
# Include <iostream>
Using namespace std;
# Define N 6
Void main ()
{
Int nPerson [N] = {55,66, 77,88, 99,44 };
Int N1 = 0, N2 = 0, N3 = 0;
Int nTargetFloor = 0, nMinFloor = 0, I;
 
For (I = 1, N1 = 0, N2 = nPerson [0], N3 = 0; I <N; I ++)
{
N3 + = nPerson [I];
NMinFloor + = nPerson [I + 1] * I;
}
For (I = 1; I <N; I ++)
{
If (N1 + N2 <N3)
{
NTargetFloor = I + 1;
NMinFloor + = (N1 + N2-N3 );
N1 + = N2;
N2 = nPerson [I];
N3-= nPerson [I];
}
Else
Break;
}
Cout <"nTargetFloor" <nTargetFloor <"\ nnMinFloor" <nMinFloor <endl;
System ("pause ");
}

# Include <iostream>
Using namespace std;
# Define N 6
Void main ()
{
Int nPerson [N] = {55,66, 77,88, 99,44 };
Int N1 = 0, N2 = 0, N3 = 0;
Int nTargetFloor = 0, nMinFloor = 0, I;

For (I = 1, N1 = 0, N2 = nPerson [0], N3 = 0; I <N; I ++)
{
N3 + = nPerson [I];
NMinFloor + = nPerson [I + 1] * I;
}
For (I = 1; I <N; I ++)
{
If (N1 + N2 <N3)
{
NTargetFloor = I + 1;
NMinFloor + = (N1 + N2-N3 );
N1 + = N2;
N2 = nPerson [I];
N3-= nPerson [I];
}
Else
Break;
}
Cout <"nTargetFloor" <nTargetFloor <"\ nnMinFloor" <nMinFloor <endl;
System ("pause ");
}

 

1.13 NIM two piles of stones


[Cpp]
# Include <iostream>
# Include <cmath>
Using namespace std;
# Define swap (x, y) (x) ^ = (y), (y) ^ = (x), (x) ^ = (y ))
Void main ()
{
Double a, B;
A = (1 + sqrt (5.0)/2;
B = (3 + sqrt (5.0)/2;
Int m, n;
Bool nim = false;
Cout <"number of books with two piles of stones entered \ n ";
Cin> m> n;
If (m = n)
Nim = true;
If (n> m)
Swap (n, m );
If (n-m = (long) floor (n * ))
Nim = false;
Else
Nim = true;
If (nim)
Cout <"winning stone players first \ n ";
Else
Cout <"winning stone players win first \ n ";
System ("pause ");
}

# Include <iostream>
# Include <cmath>
Using namespace std;
# Define swap (x, y) (x) ^ = (y), (y) ^ = (x), (x) ^ = (y ))
Void main ()
{
Double a, B;
A = (1 + sqrt (5.0)/2;
B = (3 + sqrt (5.0)/2;
Int m, n;
Bool nim = false;
Cout <"number of books with two piles of stones entered \ n ";
Cin> m> n;
If (m = n)
Nim = true;
If (n> m)
Swap (n, m );
If (n-m = (long) floor (n * ))
Nim = false;
Else
Nim = true;
If (nim)
Cout <"winning stone players first \ n ";
Else
Cout <"winning stone players win first \ n ";
System ("pause ");
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.