C ++の期末安定レビュー無敵の知識ポイント2(グラフィックの印刷(ループ)、古典的な問題)

3.グラフィックの印刷(サイクル)

グラフィックを印刷するためにforループを使用すると、2つのポイントのみを覚えておく必要があります1.外側のループ列と内側のループ行を覚えて
おいてください
2.内側のループの法則を見つけてください

三角形を印刷する

   *
  * *
 * * *
* * * *

三角形の左側のスペースは規則的であることがわかります。これらのスペースを0に変更します。

000*
00* *
0* * *
* * * *

したがって、グラフを2つの部分に分解できます。1つは出力「0」を3、2、1、0の順に減らし、もう1つは出力「*」を1、2、3、4の順に増やします。

for(int i =1;i<=4;i++)
{
    
        
   //打印空格
   for(int j=1;j<=4-i;j++)
   {
    
     
   	cout<<" " ;
   }
   //打印三角形
   for(int m =1;m<=i;m++)
   {
    
      
   	cout<<"*";
   }
   cout<<endl;
}

プリントダイヤモンド

   *                    000*                  
  * *                   00* *
 * * *                  0* * *
* * * *         =       * * * *
 * * *                  0* * *
  * *                   00* *
   *                    000*  
//打印上面的上面的三角形
for(int i =1;i <=4;i++){
    
    
			for(int q=1;q<5-i;q=q+1)
			{
    
    
				cout<<" ";
			}
			for(int j =1;j<=i;j=j+1)
			{
    
    
		    cout<<"*";
			}
		    cout<<endl;
		}
//打印下面的倒三角形
		for(int i =1;i <=3;i++){
    
    
			for(int q=1;q<=i;q=q+1)
			{
    
    
			cout<<" ";
			}
			for(int j =3;j>=i;j=j-1)
			{
    
    
			cout<<"* ";
			}
			cout<<endl;
		}

中空のダイヤモンドを印刷する

   *                    000*                  
  * *                   00* *
 *   *                  0* 0 *
*     *         =       * 0 0 *
 *   *                  0* 0 *
  * *                   00* *
   *                    000*  
for(int i =1;i <=4;i++) //打印上半部分三角形
{
    
    
			for(int q=1;q<5-i;q=q+1)
			{
    
    
				cout<<" ";
			}
	        for(int j =1;j<=i;j=j+1)	//分别在第3行的第2个,第4行的第2个,第3个变空格
			{
    
    
		
				if(i==3&&j==2)
				{
    
    
				cout<<"  ";
				}
				else if(i==4&&j ==2)
				{
    
    
				cout<<"  ";
				}
				else if(i ==4&&j==3)
				{
    
    
					cout<<"  ";
				}
				else
				{
    
    
	         	cout<<"* ";
				}
			}
			cout<<endl;
			//s=s-1;
}
		//打印下半部分三角形
		for(int i =1;i <=3;i++)
		{
    
    
			for(int q=1;q<=i;q=q+1)
			{
    
    
				cout<<"  ";
			}
			for(int j =3;j>=i;j=j-1)
			{
    
    //把下半部分三角形的第1行的第2个变空格
		        if(i==1&&j==2)
				{
    
    
				cout<<"  ";
				}
				else
				{
    
    
				cout<<"* ";
				}
			}
			cout<<endl;
		}

4つの古典的な問題

(1)うるう年判断問題

うるう年:4で割り切れるが、100で割り切れない、または400で割り切れる
if(y%4 == 0 && y%100!= 0 || y%400 == 0)

(2)韓信の兵士の命令

#include<iostream>
using namespace std;
int main()
{
    
    
    int a=0,b=0,c=0,s=0;
    cin>>a>>b>>c;
    for(int i=10;i<=100;i++)
    {
    
    
        if(i%3==a&&i%5==b&&i%7==c)
           {
    
    
               cout<<n<<endl;
               s=1;
           }
    }
    if(s==0)
    {
    
    
        cout<<"不存在这样的数";
    }
    return 0;
}

(3)水仙の数

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    
    
	int n;
	cin >> n;
	while(n)
	{
    
    
		int G,S,B;
		B=n/100;
		G=n%10;
		S=(n%100-ge)/10;
		if(pow(G,3)+pow(S,3)+pow(B,3)==n)
			cout << "Yes" << endl;
		else
			cout << "No" << endl;
		cin >> n; 
	}
	return 0;
}
 

(4)デジタルブラックホール問題

詳細については、デジタルブラックホールに特化した他のブログを参照してください

(5)古典的関数の問題

a 判断素数

int prime(int f)
{
    
    
	int i;
	for(i=2;i<f;i++)
	if(f%i==0)
	return 0;
	else 
	return 1;
}

b階乗

long Fac(int n)
{
    
    
	if (n == 0)
		return 1;
	else
		return n * Fac(n - 1);
 
}

cバブルソートとソート関数のソート。
バブリングとソートについては他の2つのブログを参照してください。d0
から255までの2進数を出力します。

#include <iostream>
using namespace std;
int main()
{
    
    
	for(int a=0;a<2;a++)
	{
    
    
		for(int b=0;b<2;b++)
		{
    
    
			for(int c=0;c<2;c++)
			{
    
    
				for(int d=0;d<2;d++)
				{
    
    
					for(int e=0;e<2;e++)
					{
    
    
						for(int q=0;q<2;q++)
						{
    
    
							for(int s=0;s<2;s++)
							{
    
    
								for(int f=0;f<2;f++)
								{
    
    
									cout<<a<<b<<c<<d<<e<<q<<s<<f<<endl;
								}
							}
							
						}
					}
				}
			}
		}
	}
	
	
	return 0;	
}

#include  <iostream>
#include <bitset>
using namespace std;
int main()
{
    
    
    bitset<8>b;
    for(int i = 0; i < 256; i++)
    {
    
    
        b = i;
        cout<<b<<endl;
    }
    return 0;
}

おすすめ

転載: blog.csdn.net/qq_51808107/article/details/111409539
おすすめ