The beauty of algorithm--1. Monte Carlo method for calculating pi

Source: Internet
Author: User

#include <iostream>#include<iomanip>using namespacestd;#defineSIZE 8intMainintargcChar**Argv[]) {    intmatrix[size][size] = {0}; inta[size][size] = {0 }; int*p =nullptr; P= &matrix[0][0]; //Initialize Matrix     for(intK =0; K < size*size; k++)    {        *p++ =k; }    //Print the original matrixcout <<"the original matrix is as Follows:"<<endl;  for(intk=0; k < size;k++)    {         for(inth =0; H < size;h++) {cout<< SETW (4) << * (* (matrix + k) +h); } cout<<endl; }    //z-shaped Orchestration    inti =0, j =0;//variable cannot be duplicated     for(intx =0; X < size;x++)    {         for(inty =0; Y < size;y++)        {            * (* (a + i) + j) = * (* (matrix + x) +y); if((i==size-1|| i==0) &&j%2==0)//move horizontally Right{j++; Continue; }            if((j==0|| j==size-1) &&i%2==1)//Move Down vertically{i++; Continue; }            if((i+j)%2==0)//top Right{i--; J++; }            Else if((i+j)%2==1)//Lower left{i++; J--; } }} cout<< Endl <<"The matrix after the z-shaped arrangement is as Follows:"<<endl;  for(inti =0; I < size;i++)    {         for(intj =0; J < size;j++) {cout<< SETW (4) << * (* (a + i) +j); } cout<<endl; }    return 0;}

Operation Result:

the original matrix is as Follows:0   1   2   3   4   5   6   7   8   9  Ten   one   a   -   -   the   -   -   -   +   -   +   a   at   -   -   -   -   -   in   -   to   +   -   the   *   $  Panax Notoginseng   -   the   +   a   the   +   -   $   $   -   -   the   -  Wuyi   the   -   wu   -   about   $   -   -   -   a   +   theThe matrix after the z-shaped arrangement is as Follows:0   1   5   6   -   the   -   -   2   4   7   -   -   -   in   the   3   8   a   -   -   -   a   +   9   one   -   -   to   +   -   -  Ten   +   at   +   the   $   the   wu   -   a   -   -   $  Wuyi   -   -   +   the  Panax Notoginseng   -   -   about   -   a   *   $   -   the   $   -   +   thePlease press any key to continue ...

The beauty of algorithm--1. Monte Carlo method for calculating pi

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.