返回列表 發帖

510 星號輸出

1. 題目說明:
請依下列題意進行作答,使輸出值符合題意要求。

2. 設計說明:
請撰寫一程式,讓使用者輸入兩個正整數n、m,代表n*m矩陣。在矩陣內各別輸入0或1,若矩陣最外圍的輸入為1,則輸出符號「*」;若1的上下左右有其一為0,亦輸出符號「*」;其餘則以半形空格表示,最後將結果輸出在畫面上。

提示:若使用 Java 語言答題,請以「JP」開頭命名包含 main 靜態方法的 class,評測系統才能正確評分。

3. 輸入輸出:
輸入說明
兩個正整數n、m及n*m矩陣中的所有元素(只有0和1)

輸出說明
轉換後的結果

範例輸入
4 7
0 0 0 1 0 0 0
0 0 1 1 1 0 0
0 1 1 1 1 1 0
1 1 1 1 1 1 1

範例輸出
      *   
    *  *  
  *      *
*******


程式輸出擷圖
下圖中的 黃色點 為 空格


本帖隱藏的內容需要回復才可以瀏覽

  1. #include <bits/stdc++.h>

  2. using namespace std;
  3. int a,b;
  4. int main()
  5. {
  6.     cin>>a>>b;
  7.     int x[a][b];
  8.     for(int i=0;i<a;i++)
  9.     {
  10.         for(int j=0;j<b;j++)
  11.         {
  12.             cin>>x[i][j];
  13.         }
  14.     }
  15.     for(int i=0;i<a;i++)
  16.     {
  17.         for(int j=0;j<b;j++)
  18.         {
  19.             if(x[i][j]==0)
  20.             {
  21.                 cout<<" ";
  22.             }else if(i==0 or i==a-1 or j==0 or j==b-1 or x[i+1][j]==0 or x[i-1][j]==0 or x[i][j+1]==0 or x[i][j-1]==0)
  23.             {
  24.                 cout<<"*";
  25.             }else
  26.             {
  27.                 cout<<" ";
  28.             }
  29.         }
  30.         cout<<endl;
  31.     }

  32.     return 0;
  33. }
複製代碼

TOP

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int n,m;
  4. int main()
  5. {
  6.     cin>>n>>m;
  7.     int data[n][m];
  8.     for(int i=0;i<n;i++)
  9.         for(int j=0;j<m;j++)
  10.             cin>>data[i][j];
  11.     for(int i=0;i<n;i++)
  12.     {
  13.         for(int j=0;j<m;j++)
  14.         {
  15.             if(data[i][j]==0)
  16.                 cout<<' ';
  17.             else
  18.             {
  19.                 if(i==0 || i==n-1 || j==0 || j==m-1 || data[i-1][j]==0 || data[i+1][j]==0 || data[i][j-1]==0 || data[i][j+1]==0)
  20.                     cout<<'*';
  21.                 else
  22.                     cout<<' ';
  23.             }
  24.         }
  25.         cout<<endl;
  26.     }
  27.     return 0;
  28. }
複製代碼

TOP

  1. #include<bits/stdc++.h>
  2. using namespace std;

  3. int main()
  4. {
  5.     int n,m;
  6.     cin>>n>>m;
  7.     int data[n][m];
  8.     for(int i=0; i<n; i++)
  9.         for(int j=0; j<m; j++)
  10.             cin>>data[i][j];
  11.     for(int i=0; i<n; i++)
  12.     {
  13.         for(int j=0; j<m; j++)
  14.         {
  15.             if(data[i][j]==0)
  16.                 cout<<' ';
  17.             else
  18.             {
  19.                 if(i==0 or i==n-1 or j==0 or j==m-1 or data[i-1][j]==0 or data[i+1][j]==0 or data[i][j-1]==0 or data[i][j+1]==0)
  20.                     cout<<'*';
  21.                 else
  22.                     cout<<' ';
  23.             }
  24.         }
  25.         cout<<endl;
  26.     }
  27.     return 0;
  28. }
複製代碼

TOP

  1. #include<bits/stdc++.h>
  2. using namespace std;

  3. int main()
  4. {
  5.     int n,m;
  6.     cin>>n>>m;
  7.     int data[n][m];
  8.     for(int i=0;i<n;i++)
  9.     {
  10.         for(int j=0;j<m;j++)
  11.         {
  12.             cin>>data[i][j];
  13.         }
  14.     }
  15.     for(int i=0;i<n;i++)
  16.     {
  17.         for(int j=0;j<m;j++)
  18.         {
  19.             if(data[i][j]==0)
  20.             {
  21.                 cout<<' ';
  22.             }
  23.             else{
  24.                 if(i==0 || i==n-1 || j==0 || j==m-1 || data[i-1][j]==0 || data[i][j-1]==0 || data[i+1][j]==0 || data[i][j+1])
  25.                     cout<<'*';
  26.                 else
  27.                     cout<<' ';
  28.             }
  29.         }
  30.         cout<<endl;
  31.     }

  32.     return 0;
  33. }
複製代碼

TOP

本帖最後由 蔡沛倢 於 2024-8-30 19:03 編輯
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int b,c;

  4. int main()
  5. {
  6.     cin>>b>>c;
  7.     int a[b][c];
  8.     for(int i=0;i<b;i++)
  9.     {
  10.         for(int j=0;j<c;j++)
  11.         {
  12.             cin>>a[i][j];
  13.         }
  14.     }
  15.     for(int i=0;i<b;i++)
  16.     {
  17.         for(int j=0;j<c;j++)
  18.         {
  19.             if(a[i][j]==0)
  20.             {
  21.                 cout<<" ";
  22.             }
  23.             else
  24.             {
  25.                  if(i==0 || i==b-1 || j==0 || j==c-1 || a[i-1][j]==0 || a[i+1][j]==0 || a[i][j-1]==0 || a[i][j+1]==0)
  26.                  {
  27.                     cout<<'*';
  28.                  }
  29.                  else
  30.                  {
  31.                     cout<<' ';
  32.                  }

  33.             }

  34.         }
  35.         cout<<endl;
  36.     }
  37.     return 0;
  38. }
複製代碼

TOP

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int n, m;
  4. int main()
  5. {
  6.     cin>>n>>m;
  7.     int data[n][m];
  8.     for(int i=0; i<n; i++)
  9.         for(int j=0; j<m; j++)
  10.             cin>>data[i][j];
  11.     for(int i=0; i<n; i++)
  12.     {
  13.         for(int j=0; j<m; j++)
  14.         {
  15.             if(data[i][j]==0)
  16.                 cout<<' ';
  17.             else
  18.             {
  19.                 if(i==0 || i==n-1 || j==0 || j==m-1 || data[i-1][j]==0 || data[i+1][j]==0 || data[i][j-1]==0 || data[i][j+1]==0)
  20.                     cout<<'*';
  21.                 else
  22.                     cout<<' ';
  23.             }
  24.         }
  25.         cout<<endl;
  26.     }
  27.     return 0;
  28. }
複製代碼

TOP

  1. #include <bits/stdc++.h>

  2. using namespace std;
  3. int a,b;
  4. int main()
  5. {
  6.     cin>>a>>b;
  7.     int x[a][b];
  8.     for(int i=0;i<a;i++)
  9.     {
  10.         for(int j=0;j<b;j++)
  11.         {
  12.             cin>>x[i][j];
  13.         }
  14.     }
  15.     for(int i=0;i<a;i++)
  16.     {
  17.         for(int j=0;j<b;j++)
  18.         {
  19.             if(x[i][j]==0)
  20.             {
  21.                 cout<<" ";
  22.             }else if(i==0 or i==a-1 or j==0 or j==b-1 or x[i+1][j]==0 or x[i-1][j]==0 or x[i][j+1]==0 or x[i][j-1]==0)
  23.             {
  24.                 cout<<"*";
  25.             }else
  26.             {
  27.                 cout<<" ";
  28.             }
  29.         }
  30.         cout<<endl;
  31.     }

  32.     return 0;
  33. }
複製代碼

TOP

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5.     int n,m;
  6.     cin>>n>>m;
  7.     int mat[n][m];
  8.     for(int i=0;i<n;i++)
  9.         for(int j=0;j<m;j++)
  10.             cin>>mat[i][j];
  11.     for(int i=0;i<n;i++){
  12.         for(int j=0;j<m;j++){
  13.             if(mat[i][j]==0)
  14.                 cout<<' ';
  15.             else if(i==0||i==n-1||j==0||j==m-1||mat[i-1][j]==0||mat[i+1][j]==0||mat[i][j-1]==0||mat[i][j+1]==0)
  16.                 cout<<'*';
  17.             else
  18.                 cout<<' ';
  19.         }
  20.         cout<<endl;
  21.     }
  22.     return 0;
  23. }
複製代碼

TOP

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int n, m;
  4. int main()
  5. {
  6.     cin>>n>>m;
  7.     int data[n][m];
  8.     for(int i=0; i<n; i++)
  9.         for(int j=0; j<m; j++)
  10.             cin>>data[i][j];
  11.     for(int i=0; i<n; i++)
  12.     {
  13.         for(int j=0; j<m; j++)
  14.         {
  15.             if(data[i][j]==0)
  16.                 cout<<' ';
  17.             else
  18.             {
  19.                 if(i==0 || i==n-1 || j==0 || j==m-1 || data[i-1][j]==0 || data[i+1][j]==0 || data[i][j-1]==0 || data[i][j+1]==0)
  20.                     cout<<'*';
  21.                 else
  22.                     cout<<' ';
  23.             }
  24.         }
  25.         cout<<endl;
  26.     }
  27.     return 0;
  28. }
複製代碼

TOP

返回列表