Algorithm tips [modulo extraction tips]

Source: Internet
Author: User

My notes are a little hairy. If you can understand them, just read them.

Write a method to achieve the loop effect without the if statement: int I = 0;

// 1-6 cycles. Int fun (INT max) {static int I = 0; I = I % Max; I ++; return I;} int main () {fun (6 );}

// Traditional statement: 1-6 cycles. Int fun (INT max) {static int I = 0; if (I> MAX) I = 0; I ++; return I ;}

Modulo Application

// The specified number of changes within a period of time. The change cycle is the same. Int n_time = 3; for (float time = 0; time <1; time ++ = 0.02) {// segment float slice = 1.0f/n_time; // 0-slice 3 times. Float M = fmodf (time, slice); If (M> slice/2) {printf ("% F \ n", m );} else {printf ("-% F \ n", m);} // |__ _ __|}// features: // The result obtained by time % slice is never greater than that obtained by slice. It will only loop in the cycle of 0-slice. // slice/2, change count 1 * cycle count 3 // slice/3, and change twice each time, it will change 6 times. // Slice/N, and the number of changes: (1/slice) * (n-1)

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.