2022年6月30日 星期四

巴斯卡三角形

 //https://openhome.cc/zh-tw/algorithm/basics/pascal-triangle/

#include <stdio.h>

int combi(int r, int n){

    int p = 1;

    int i;

    for(i = 1; i <= n; i++) {

        p = p * (r - i + 1) / i;

    }

    return p;

}


int main() {

int HEIGHT;

scanf("%d",&HEIGHT);

    int r;

    for(r = 0; r < HEIGHT; r++) {

        char format[5];            

        sprintf(format, "%%%ds", (HEIGHT - r) * 3);

        printf(format, "");

        int n;

        for(n = 0; n <= r; n++) {

            printf("%6d", combi(r, n));

        }

        printf("\n");

    }

    return 0;

遞迴函數介紹(以費式數列為例)

 /*

 Fibonacci Sequence

 費式數列 :1 1 2 3 5 8 13 24 34 55 89 ....?

 第n項     1 2 3 4 5 6  7  8  9 10 11 ....n

 規則:

 第1項為1

 第2項為1

 第3項=第2項(前項)+第1項(前前項)  

 第4項=第3項+第2項 

 ...

 第n項(第3項為前2項之和)=第n-1項+第n-2項 

*/

#include <stdio.h>

#include <stdlib.h>

int F(int n)

{

if(n==1 || n==2) 

   return 1;

else 

   return F(n-1)+F(n-2);

   /*n=4

      F(3) +F(2)

   /     

     F(2)+F(1)

       1 +  1

   */

}

int main(int argc, char *argv[])

{

int n;

scanf("%d",&n);

int ans=F(n);

printf("%d\n",ans);

return 0;

}

點心+ 飲料 總共需要多少錢

 #include <stdio.h>

#include <stdlib.h>

int main(int argc, char *argv[])

{

int N;

scanf("%d",&N);

while(N--)//輸入3 321   0(不成立就跳出loop) 

{

int a,b;

scanf("%d %d",&a,&b);

printf("%d\n",(a+b)*2);

}

return 0;

}

一定中大樂透

 #include <stdio.h>

#include <stdlib.h>

int main(int argc, char *argv[])

{

while(1)

{

int N,M;

scanf("%d %d",&N,&M);

if(N==0 && M==0)

break;

if((N%2==0 && M%2==0) || (N%2==1 && M%2==1))

{

printf("Win\n");

}

else 

{

printf("Loss\n");

}

}

return 0;

}

打印半金字塔數字https://www.educba.com/number-patterns-in-c/

 

#include<stdio.h>

#include<conio.h>

int main()

{

int n, i, j;

printf("Enter the number of rows: ");

scanf("%d",&n);

for(i = 1; i <= n; i++)//跑n遍 i跑1遍j跑1遍

{

for(j = 1; j <= i; j++)//

{

   printf("%d",i);//印外loop的數字 1 22 333 4444

}

printf("\n");

}

return 0;

}

打印半金字塔數字

 


#include<stdio.h>
#include<conio.h>
int main()
{
int n, i, j;
printf("Enter the number of rows: ");
scanf("%d",&n);
for(i = 1; i <= n; i++)//外loop優先順序先 
{
for(j = 1; j <= i; j++)//左邊不用印空白 
{
   printf("%d",j);
}
printf("\n");
}
    return 0;
}

打印數字金字塔圖案

 

#include<stdio.h>

#include<conio.h>

int main()

{

int n, i, j;

printf("Enter the number of rows: ");

scanf("%d",&n);

for(i = 1; i <= n; i++)//1~n列 

{

  for(j = n; j >= i; j--)//每一列連續印n個空白 3 2 1個空白 列每增加1列空白就少一格

  {

    printf(" ");

  }

  for(j = 1; j <= i; j++)//印數字 

  {

  printf("%d ",j);//印完數字要加空白 

  }

printf("\n");

}

    return 0;

}

algorithm

 #include <iostream> #include <string.h> using namespace std; int main(int argc, char** argv)  { for(int j=2;j<=100;j++)//j...