提高篇第2-5課第一題

第一小題:

#include <stdio.h>
#include <stdlib.h>
//第一題,第一小題
void printstars(int m) //定義能輸出一行m個星號的函數
{
    int j;
    for (j=1; j<=m; ++j)
         printf("*");
}
int main( )
{
  int n=6; //n代表要輸出的行數
  int i;
  for(i=1; i<=n; ++i)
    {
        printstars(2*i-1);
        printf("\n");
    }
  return 0;
}

第二小題

#include <stdio.h>
#include <stdlib.h>
//第一題,第二小題
//在下面寫printchs函數的定義,功能是輸出一行若干個指定字符
void  printchs(int a,char ch)
{
    int i;
    for(i=1;i<=a;i++)
        putchar(ch);
}

int main( )
{
    int n=6; //n代表要輸出的行數
    int i;
    for(i=1; i<=n; ++i)
    {
        printchs(n-i,' ');
        printchs(2*i-1,'*') ;
        printf("\n");
    }
    return 0;
}

第三小題

#include <stdio.h>
#include <stdlib.h>
//第一題,第二小題
//在下面寫printchs函數的定義,功能是輸出一行若干個指定字符
void  printchs(int a,char ch)
{
    int i;
    for(i=1;i<=a;i++)
        putchar(ch);
}

int main( )
{
    int n=6; //n代表要輸出的行數
    int i;
    for(i=1; i<=n; ++i)
    {
        printchs(n-i,' ');
        printchs(2*i-1,64+i) ;//A的ascII碼值爲65
        printf("\n");
    }
    return 0;
}


發佈了92 篇原創文章 · 獲贊 0 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章