0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Paiza Cランク獲得問題(Dランク相当) 標準出力メニュー④ C#

Posted at

paizaのCランク獲得問題の解答がなかったので、備忘録として残します。
明らかに簡単な問題の場合は省略しますが、個人的に少しでも考えたコードを残していきたいと思います。
初心者ですので醜いコードを書きますが、温かい目で見守っていただけると嬉しいです。
また、より良い記述方法などありましたら、コメント等で教えていただけると嬉しいです。

目次

  • 数値を半角スペース埋めで出力
  • 数値をゼロ埋めで出力
  • 複数の数値を半角スペース埋めで出力
  • 数値を M けた半角スペース埋めで出力
  • 【出力幅を指定して出力】N 個の数値を M けた半角スペース埋めで出力
  • 文字列と N 個の整数の出力
  • 九九表を罫線入りで出力
  • ペアの数値の入った表を罫線入りで出力
  • 【出力形式を指定して出力】ペアの数値の入った表を罫線入りで出力 2

数値を半角スペース埋めで出力

問題文

using System;

class MainClass
{
    public static void Main()
    {
        string N= Console.ReadLine();
        
        Console.WriteLine("{0, 3}", N);
    }
}

あまりよくわかっていないです。

数値をゼロ埋めで出力

問題文

using System;
class Program
{
    static void Main()
    {
        string N =Console.ReadLine();
        Console.WriteLine(N.PadLeft(3, '0'));
    }
}

複数の数値を半角スペース埋めで出力

問題文

using System;
class Program
{
    static void Main()
    {
        int N =int.Parse(Console.ReadLine());
        for(int i=0;i<N;i++){
            string M= Console.ReadLine();
        
            Console.WriteLine("{0, 3}", M);
        }
    }
}

よくわかってないです

数値を M けた半角スペース埋めで出力

問題文

using System;

class Program
{
    static void Main()
    {
        string[] NM = Console.ReadLine().Split();
        string N = NM[0];
        int M = int.Parse(NM[1]);
        Console.WriteLine("{0," + M + "}", N);
    }
}

Mを""に入れると文字列判定になってしまいます。

【出力幅を指定して出力】N 個の数値を M けた半角スペース埋めで出力

問題文

using System;

class Program
{
    static void Main()
    {
        string[] NM = Console.ReadLine().Split();
        int N = int.Parse(NM[0]);
        int M = int.Parse(NM[1]);
        for(int i=0;i<N;i++){
            string A=Console.ReadLine();
            Console.WriteLine("{0," + M + "}", A);
        }
        
    }
}

文字列と N 個の整数の出力

問題文

using System;

class Program
{
    static void Main()
    {
        string[] NAB = Console.ReadLine().Split();
        int N= int.Parse(NAB[0]);
        int A= int.Parse(NAB[1]);
        int B= int.Parse(NAB[2]);
        for(int i=0;i<N;i++){
            if(i==N-1){
                Console.Write("("+A+", "+B+")");
            }else{
                Console.Write("("+A+", "+B+"), ");
            }
        }
    }
}

問題文

using System;

class Program
{
    static void Main()
    {
        for(int i=1;i<=9;i++){
            for(int j=1;j<=9;j++){
                string num=(i*j).ToString().PadLeft(2, ' ');
                if(j<9){
                    Console.Write(num+" | ");
                }else{
                    Console.WriteLine(num);
                }
            } 
            if(i<9){
                    Console.WriteLine(new string('=',(2 * 9 + 3 * (9 - 1))));
            }
        }
    }
}

本当によくわかっていません
PadLeft(2, ' ');を記述しないとエラーが出ます。
「数値を出力する際は 2 けたになるように」の条件を満たすために行っています。(多分)
=の数は
「数字が 9 個あってその数字を出力するのに 2 文字分(2桁?)使ってるのと、縦罫線 ( | ) が 8 個あってそれに 3 文字使っているから、それと同じだけ = を出してるだけですよ。」とのこと(知恵袋より

ペアの数値の入った表を罫線入りで出力

問題文

using System;

class Program
{
    static void Main()
    {
        string[] HWAB=Console.ReadLine().Split();
        int H= int.Parse(HWAB[0]);
        int W= int.Parse(HWAB[1]);
        int A= int.Parse(HWAB[2]);
        int B= int.Parse(HWAB[3]);
        
        for(int i=0;i<H;i++){
            for(int j=0;j<W;j++){
                string AB="("+A+", "+B+")";
                if(j<W-1){
                    Console.Write(AB+" | ");
                }else{
                    Console.WriteLine(AB);
                }
            } 
            if(i<H-1){
                Console.WriteLine(new string('=', (6 * W + 3 * (W - 1))));
            }
        }
    }
}

【出力形式を指定して出力】ペアの数値の入った表を罫線入りで出力 2

問題文

using System;

class Program
{
    static void Main()
    {
        string[] HWAB=Console.ReadLine().Split();
        int H= int.Parse(HWAB[0]);
        int W= int.Parse(HWAB[1]);
        string A= HWAB[2].PadLeft(9);
        string B= HWAB[3].PadLeft(9);
        
        for(int i=0;i<H;i++){
            for(int j=0;j<W;j++){

                string AB="("+A+", "+B+")";
                if(j<W-1){
                    Console.Write(AB+" | ");
                }else{
                    Console.WriteLine(AB);
                }
            } 
            if(i<H-1){
                Console.WriteLine(new string('=', ( 22* W + 3 * (W - 1))));
            }
        }
    }
}

22=9*2(数字)+3(記号)+1(半角スペース)です。
日を開けてやっていると段々そういうことかと分かってきた気がします。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?