Akashi Sekizaki
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # プログラミング基礎WS問題 ## 問題文(試験レベル問題) 整数型2次元配列$x[n][n]$($n$は定数)を正方行列と考える。 正方行列$x$と$n$のサイズを入力から読み取り、boolean型の変数$check$を用いて対角行列かどうか判定するプログラムを作成しなさい。このとき、$n$は3以上5以下とする。 正方行列$x[n][n]$(Nは定数)が対角行列であれば1を出力し、そうでなければ0を出力しなさい。ただし, 0≤行列の要素≤9と仮定してよいものとする。<br>なお、boolean型を用いる場合は<stdbool.h>を含める必要があり、boolean型は$true$の場合は1を、$false$の場合は0を出力する。<br>また、対角行列とは、正方行列$A$で$Aij=0(i≠j)$の行列のことをいう。 ## 入力 入力は以下の形式で標準入力から与えられる。 ``` N a b c d e f g h i ``` ## 出力 問題文の指示に従って対角行列か判定し、対角ならば1、対角でないならば0を出力せよ ## 入力例1 ``` 3 2 0 0 0 3 0 0 0 4 ``` ## 出力例1 ``` 1 ``` ## 入力例2 ``` 4 5 0 0 0 0 2 0 0 0 2 1 0 0 0 0 2 ``` ## 出力例2 ``` 0 ``` ## 入力例3 ``` 5 8 0 0 0 0 0 9 0 0 0 0 0 3 0 0 0 0 0 4 0 0 0 0 0 5 ``` ## 出力例3 ``` 1 ``` # 解答例 ``` #include <stdio.h> #include <stdbool.h> boolean型を用いるのに必要 int main(void) { /*行列の要素数の宣言*/ int n; scanf("%d", &n); //入力から行列のサイズnを読み取る int x[n][n]; //行列xを2重配列x[n][n]で定義 bool check = true; //boolean型の変数宣言(true==1,false==0) /*2重配列x[][]にn行、n列のそれぞれの要素を格納*/ for (int i = 0; i < n; i++) {  //iをn行まで繰り返す for (int j = 0; j < n; j++) {  //jをn列まで繰り返す scanf("%d", &x[i][j]); //行列の要素を読み取る /*対角以外の成分が0か否か*/ if(i != j){   if (x[i][j] != 0) {  //対角成分以外の場合   check = false;   //i≠jのとき、x[i][j]≠0ならばcheckにfalse代入   } } } } /*checkを出力*/ printf("%d", check);  //check==trueなら1を出力 //check==falseなら0を出力 return 0; } ``` ## 解説 ### 細分化 ![](https://i.imgur.com/IKsOws2.png) ### 手順 - 行列のサイズnの入力、行列xや変数checkの宣言 - 最初にキーボードから正の整数行列のサイズnを読み込む。これをnとする。 - 行列$x$を整数型で宣言するので、データ型はint型を持ち、<br>行列$x$は要素数nの2重配列$x[n][n]$として定義する。 - 対角化判定のためのboolean型変数check=trueを宣言する。 - • #include <stdbool.h>を入れる - 2重配列x[][]に行列xのn行、n列のそれぞれの要素を格納(行列の要素の入力) - 行を表す変数を整数型のi、列を表す変数を整数型のjとする。 - i、jともにn行までfor文でループさせ、行列$x$の要素を読み取る。 * 対角成分か否かで場合分け - i、jともにn行までfor文でループさせながら、以下の処理を行う。 - 対角成分が0のときはcheck==trueのまま - 対角以外の成分が0か否かで場合分け   - 対角以外の成分が1のときはcheckにfalseを代入   - 対角以外の成分が0のときはcheck==trueのまま * 出力 - checkの値を出力 - check==trueなら1を出力 - check==falseなら0を出力

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully