Better understanding of how nested for loop

A matrix, diamond print

// loop has been based on <= represent you, if> = how 

How to avoid code duplication problem question mark    MARKDOWN processing


An outer layer for controlling the line number

An inner layer for controlling the column number

#include<stdio.h>
int main()
{
    int i,j,k,n;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            printf("%d%d ",i,j);
        }
        printf("\n");
    }
}

 

 

* Number of controls for the inner output

#include<stdio.h>
int main()
{
    int i,j,k,n;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=(2*i-1);j++)
        {
            printf("*");
        }
        printf("\n");
    }
}


 

Triple loop

The second number of spaces heavy cycle control 

From more to less 4321

#include<stdio.h>
int main()
{
    int i,j,k,n;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        for(k=1;k<=n-i;k++)            //循环中一直是以<=表示吗 ,如果>=会如何 
        {
            printf(" ");
        }
        for(j=1;j<=(2*i-1);j++)
        {
            printf("*");
        }
        printf("\n");
    }
}

 

 


Rhombic symmetry

Changing a first end value FOR fewer rows

The second FOR

k <= i so that the spaces 1-4

At least FOR third triangular changed by the first plurality FOR final value of

 

#include<stdio.h>
int main()
{
    int i,j,k,n;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        for(k=1;k<=n-i;k++)            //循环中一直是以<=表示吗 ,如果>=会如何 
        {
            printf(" ");
        }
        for(j=1;j<=(2*i-1);j++)
        {
            printf("*");
        }
        printf("\n");
    }
    for(i=1;i<=n-1;i++)
    {
        for(k=1;k<=i;k++)            
        {
            printf(" ");
        }
        for(j=1;j<=(2*(n-i)-1);j++)
        {
            printf("*");
        }
        printf("\n");
    }
}


Hollowing out 

Select statement hollowed increase

#include<stdio.h>
int main()
{
    int i,j,k,n;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        for(k=1;k<=n-i;k++)            //循环中一直是以<=表示吗 ,如果>=会如何 
        {
            printf(" ");
        }
        for(j=1;j<=(2*i-1);j++)
        {
            if(j==1||j==(2*i-1))
            printf("*");
            else
            printf(" ");
        }
        printf("\n");
    }
    for(i=1;i<=n-1;i++)
    {
        for(k=1;k<=i;k++)            
        {
            printf(" ");
        }
        for(j=1;j<=(2*(n-i)-1);j++)
        {
            if(j==1||j==(2*(n-i)-1))
            printf("*");
            else
            printf(" ");
        }
        printf("\n");
    }
}

Second, print 9 * 9 multiplication table

#include<stdio.h>
int main()
{
	int i,j,k,n;
	for(i=1;i<=9;i++)
	{

		for(j=1;j<=i;j++)
		{
			
			k=i*j;
			printf("%d*%d=%d\t",i,j,k);
		}
		printf("\n");
	}
}

 \ T mean transverse skip to the next tab position

 

Third, resolve primes are several ways to compare efficiency

https://blog.csdn.net/net_assassin/article/details/8960572

Method One: traversing thought

Prime number N is in addition to 1 and it does not have any number of factors, it requires a prime number it is easy to think of from 2 to N-1 to try addition, if divisible that it is not a prime number, this time is then determined that a number that is, following this 

#include<stdio.h>
int main()     //打印2-100间素数 
{
    int i,j;
    i=0;
    j=0;
    for(i=2;i<=100;i++)
    {
        for(j=2;j<=i-1;j++)  //因子只可能从2到n-1 
        {
            if(i%j==0)
            {
                break;
            }
        }
        if (j==i)
        {
            printf("素数=%d\n",i);
        }
    }
    return 0;
}


 

Method two: In addition to decoupling 2  

One possible method to do it, but to try a little too much a factor. We think that if there are N in addition to its own factor, this factor must be smaller than that equal to N / 2 (1 * N = N, 2 * (N / 2) = N, if the factor a / 2 is greater than N, and so another factor it);? and if this is a factor N must be odd to find in there. The Code

The second is the need to traverse these numbers one by one, when a mathematical divided by half the number of its less are not divisible, then this number for more than half of it is not divisible by the number.

Directly remove even number, because it is impossible to even number is a prime number, it is determined whether each prime number is prime to

 

#include<stdio.h>
int main()     //打印3-100间素数 
{
	int i,j;
	i=0;
	j=0;
	for(i=3;i<=100;i+=2)
	{
		for(j=3;j<=i/2;j++)  //素数肯定不是偶数 
		{
			if(i%j==0) //余数为0,可整除一定不是素数 
			{
				break;
			}
		}
		if (j>i/2)//同因子 
		{
			printf("素数=%d\n",i);
		}
	}
	return 0;
}

 

 

 

 

Method three: sqrt can try to factor N / 2, it can be the square root of N to try? I do it? Factors are in pairs. For example, a factor of 100: 1 and 100,2 and 50,4 and 25, 5 and 20, 10 and 10. You did not see it? Paired factor, which must be smaller than a square root equal to 100, 100 other than or equal to the square root. The Code

上述判断方法,明显存在效率极低的问题。对于每个数n,其实并不需要从2判断到n-1,我们知道,一个数若可以进行因数分解,那么分解时得到的两个数一定是一个小于等于sqrt(n),一个大于等于sqrt(n),据此,上述代码中并不需要遍历到n-1,遍历到sqrt(n)即可,因为若sqrt(n)左侧找不到约数,那么右侧也一定找不到约数。
 

https://blog.csdn.net/LZXloveGYD/article/details/71340090

这样做可以减少循环次数,素数是因子为1和本身, 如果数c不是素数,则还有其他因子,其中的因子,假如为a,b.其中必有一个大于sqrt(c) ,一个小于sqrt(c) 。所以m必有一个小于或等于其平方根的因数,那么验证素数时就只需要验证到其平方根就可以了。即一个合数一定含有小于它平方根的质因子。

https://blog.csdn.net/huang_miao_xin/article/details/51331710

#include<stdio.h>
#include<math.h>
int main()     //打印2-100间素数 
{
	int i,j;
	i=0;
	j=0;
	for(i=2;i<=100;i++)
	{
		for(j=2;j<=sqrt(i);j++)  //因子
		{
			if(i%j==0) //余数为0,可整除一定不是素数 
			{
				break;
			}
		}
		if (j>sqrt(i))//同因子 
		{
			printf("素数=%d\n",i);
		}
	}
	return 0;
}

方法 ???? i/j            i*i 等同于sqrt https://www.cnblogs.com/ant-xu/p/11172207.html

#include <stdio.h>
int main ()
{
   /* 局部变量定义 */
   int i, j;
   
   for(i=2; i<100; i++) {
      for(j=2; j <= (i/j); j++)
        if(!(i%j)) break; // 如果找到,则不是质数
      if(j > (i/j)) printf("%d 是质数\n", i);
   }
 
   return 0;
}

发布了57 篇原创文章 · 获赞 27 · 访问量 1万+

Guess you like

Origin blog.csdn.net/ao_mike/article/details/102756326