Beauty of Programming

Source: Internet
Author: User

1.1 CPU usage Problems

# 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 problem "; 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 ++); // 2.67 GHz 4-core sleep (10);} return 0 ;} // const Double 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 running cycles

# Include <iostream> using namespace STD; inline _ int64 getcputickcount () {__ ASM {rdtsc ;}} void main () {cout <"Number of CPU core run cycles" <getcputickcount () <Endl; System ("pause ");}

1.2 generals

# Include <iostream> using namespace STD; // first method 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 // The value is half the length of the memory unit, in this question, it is 4bit # define fullmask 255 // This number represents a full-bit mask. In binary representation, it is 11111111. # Define lmask (fullmask 

1.12 elevator Scheduling

#include <iostream>using namespace std;#define N 6void 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];}elsebreak;}cout<<"nTargetFloor "<<nTargetFloor<<"\nnMinFloor "<<nMinFloor<<endl;system("pause");}

1.13 Nim two piles of stones

# 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 that input two piles of stones \ n "; cin> m> N; If (M = N) Nim = true; If (n> m) Swap (n, m ); if (n-M = (long) floor (N * A) Nim = false; else Nim = true; If (NIM) cout <"winning stone players first \ n"; elsecout <"winning stone players first \ n"; System ("pause ");}

2.7 Max public approx. Least public multiple

Http://blog.csdn.net/aoxiangzhiguanjun/article/details/8755260

2.13 Maximum Product of sub-array

# Include <iostream> # include <stdlib. h> # include <stdio. h> using namespace STD; // Maximum Product of the sub-array int maxproduct (int * a, int N) {int maxproduct = 1; // Max positive product at current position int minproduct = 1; // min negative product at current position int r = 1; // result, Max multiplication totally for (INT I = 0; I <n; I ++) {if (a [I]> 0) {maxproduct * = A [I]; minproduct = min (minproduct * A [I], 1);} else if (a [I] = 0) {maxproduct = 1; minproduct = 1;} else // A [I] <0 {int temp = maxproduct; maxproduct = max (minproduct * A [I], 1); minproduct = temp * A [I];} r = max (R, maxproduct);} return r ;} int main (INT argc, char * argv []) {int A [] = {1,-2,-1, 0, 5}; int result = maxproduct (A, 5 ); cout <result <Endl; System ("pause"); Return 0 ;}

2.14 obtain the largest sum of sub-Arrays

Given an array, all elements are integers (positive and negative), and they are used to find continuous elements and sum them into the largest sequence.

Http://blog.csdn.net/aoxiangzhiguanjun/article/details/8836702

3.2 telephone numbers correspond to English words and can be queried from the digital dictionary

# Include <iostream> using namespace STD; # define Tellen 3 void match (char * words) {char * word = "Yes yer"; if (strstr (word, words )) {printf ("words % s \ n", words) ;}} void main () {char word [Tellen + 1] = {0 }; // store each generated word char C [10] [10] = {"", "", "ABC", "def", "Ghi", "jkl ", "MnO", "pqrs", "TUV", "wxyz"}; // int total [10] = {, 3, 3, 3, 3, 4, 3, 4}; // number of letters in each number int number [Tellen] = {9, 3, 7}; // phone number int answer [10] = {0 }; // The Array records the offset (INDEX) of each letter in the character set that can be represented by its numeric key, initialized to 0int I, j = 0; while (true) {for (I = 0; I <Tellen; I ++) {If (number [I] = 1 | Number [I] = 0) // ignore the influence of spaces on break; else {printf ("% C", C [number [I] [answer [I]); word [I] = C [number [I] [answer [I] ;}} word [Tellen] = '\ 0'; match (Word ); // match printf ("\ n") in the data dictionary; int K = telLen-1; while (k> = 0) {If (answer [k] <total [number [k]-1) {answer [k] ++; break;} else {answer [k] = 0; k -- ;}} if (k <0) break;} system ("pause ");}

3.6 programming to determine whether two linked lists are intersecting

Http://blog.csdn.net/aoxiangzhiguanjun/article/details/8804403

3.8 Binary Tree Problems

Http://blog.csdn.net/aoxiangzhiguanjun/article/details/8904794

3.9 rebuilding a binary tree

# Include <stdio. h> # include <stdlib. h> # include <string. h> typedef struct node {char chvalue; struct node * lchild; struct node * rchild;} node; // rebuilt Binary Tree void rebuild (char * ppreorder, char * pinorder, node ** proot, int ntreelen) {int nleftlen, nrightlen; char * pleftend; node * P; // check if (! Ppreorder |! Pinorder |! Proot) return; If (! (P = (node *) malloc (sizeof (node) return; P-> chvalue = * ppreorder; P-> lchild = p-> rchild = NULL; * proot = P; If (ntreelen = 1) return; // divide the Left and Right subquantities pleftend = pinorder; while (* pleftend! = * Ppreorder) pleftend ++; nleftlen = (INT) (pleftend-pinorder); nrightlen = ntreelen-nleftlen-1; if (nleftlen) rebuild (ppreorder + 1, pinorder, & (p-> lchild), nleftlen); If (nrightlen) rebuild (ppreorder + nleftlen + 1, pinorder + nleftlen + 1, & (p-> rchild ), nrightlen);} // post-order traversal void postorder (node * P) {If (p) {postorder (p-> lchild); postorder (p-> rchild ); printf ("% C", p-> chvalue) ;}} int main (void) {char preo Rder [32], inorder [32]; node * ptree; // enter the first and middle order sequences while (scanf ("% S % s", preorder, inorder )! = EOF) // abdcef dbaecf {rebuild (preorder, inorder, & ptree, strlen (preorder); postorder (ptree); printf ("\ n ");} return 0 ;}

4.9 Data independence Construction

# Include <iostream> # include <cstdlib> using namespace STD;/* problem: To construct a 9*9 square matrix, players must be in each square, fill in any number ranging from 1 to 9 so that the numbers in each column, row, and 3x3 matrix of the entire board are not repeated. First, we use a deep priority search to generate a feasible solution, and then randomly delete a certain number of numbers to generate a Sudoku. */# Define Len 9 # define clear (a) memset (a), 0, sizeof (A) int level [] = {30, 37, 45 }; int grid [Len + 1] [Len + 1]; int value [Len + 1]; void next (Int & X, Int & Y) {x ++; if (x> 9) {x = 1; y ++ ;}// select the next valid state int picknextvalidvalue (int x, int y, int cur) {clear (value); int I, j; for (I = 1; I <Y; I ++) value [grid [I] [x] = 1; for (j = 1; j <X; j ++) value [grid [y] [J] = 1; int u = (x-1)/3*3 + 1; int v = (Y-1)/3*3 + 1; for (I = V; I <v + 3; I ++) for (j = u; j <u + 3; j ++) {value [grid [I] [J] = 1 ;}for (I = cur + 1; I <= Len & value [I]; I ++); return I;} void pre (Int & X, Int & Y) {X --; if (x <1) {x = 9; y -- ;}} int times = 0; int main () {int X, Y, I, j; X = y = 1; // Iterative Algorithm for deep search while (true) {times ++; // If (y = Len & X = Len) {for (I = 1; I <= Len; I ++) {for (j = 1; j <= Len; j ++) cout <grid [I] [J] <"; cout <Endl;} cout <Times <Endl; break; // pre (x, y ); // times = 0;} // If (y = 0) break if the failure result is met; // change the status grid [y] [x] = picknextvalidvalue (x, y, grid [y] [x]); If (grid [y] [x]> Len) {// restore status grid [y] [x] = 0; Pre (X, y);} else // further search for next (x, y);} for (I = 1; I <= level [2]; I ++) {int ind = rand () % (LEN * Len); grid [ind/Len + 1] [ind % Len] = 0 ;}for (I = 1; I <= Len; I ++) {for (j = 1; j <= Len; j ++) cout <grid [I] [J] <""; cout <Endl;} system ("pause ");}

4.10 digital puzzles and replies

# Include <iostream> # include <string> using namespace STD; // Title: Great Buddha Temple * I = great Buddha Temple. each letter represents a different number. int main () {bool flag; bool isused [10]; int number, revert_number, T, V; For (number = 0; number <100000; number ++) {flag = true; memset (isused, 0, sizeof (isused); t = number; revert_number = 0; For (INT I = 0; I <5; I ++) {v = T % 10; revert_number = revert_number * 10 + V; T/= 10; If (isused [v]) Flag = false; elseisused [v] = 1 ;} if (flag & (revert_number % Number = = 0) {v = revert_number/number; If (v <10 &&! Isused [v]) cout <number <"" <v <"" <revert_number <Endl ;}} system ("pause"); Return 0 ;}

The beauty of Programming

For some of the above Code, refer to the beauty of programming, and refer to the network, while others are compiled by yourself.

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.