1
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 3 years have passed since last update.

C# ~文字列~ Cランクレベルアップメニュー

Posted at

システムエンジニアの研修の1つに「paizaBランク問題をクリア」があるのでその目標を達成するまでの過程を備忘録として残します。

今後業務に参加していく際に記述方法を忘れてしまった未来の自分に向けて、メモを残すと同時にこの記事がC#を学習している方、同じ箇所で躓いている方の参考になればと思っています。
ですので内容に誤りがある場合やより良い記述をご存知の方はコメントで共有していただけると嬉しいです。

それでは本編に入ります。
今回はPaizaラーニング、レベルアップ問題集「文字列」の問題をC#で回答したときの内容になります。

STEP: 1 整数と文字列

問題文:https://paiza.jp/works/mondai/c_rank_level_up_problems/c_rank_string_step1

paiza.cs
using System;
class Program
{
    static void Main()
    {
        var line = int.Parse(Console.ReadLine());
        for(var i = 0; i < line; i++){
            string number = Console.ReadLine();
            Console.WriteLine(number.Length);
        }
    }
}

STEP: 2 部分文字列

問題文:https://paiza.jp/works/mondai/c_rank_level_up_problems/c_rank_string_step2

paiza.cs
using System;
class Program
{
    static void Main()
    {
        // 自分の得意な言語で
        // Let's チャレンジ!!
        string line = Console.ReadLine();
        string line2 = Console.ReadLine();
        if(line2.Contains(line)){
            Console.WriteLine("YES");
        }else{
            Console.WriteLine("NO");
        }
        
    }
}

STEP: 3 数字の文字列操作(基本)

問題文:https://paiza.jp/works/mondai/c_rank_level_up_problems/c_rank_string_step3

paiza.cs
using System;
class Program
{
    static void Main()
    {
        var line = Console.ReadLine();
        int[] lineArray = new int[4];
        for(var i = 0; i < line.Length; i++){
            lineArray[i] = int.Parse(line[i].ToString());
        }
        int a = lineArray[0] + lineArray[3];
        int b = lineArray[1] + lineArray[2];
        Console.WriteLine(a.ToString() + b.ToString());
    }
}

STEP: 4 数字の文字列操作(0埋め)

問題文:https://paiza.jp/works/mondai/c_rank_level_up_problems/c_rank_string_step4

paiza.cs
using System;
class Program
{
    static void Main()
    {
        var line = Console.ReadLine();
        string lineLength = line.Length.ToString();
        
        string result = "first";
        if(lineLength == "1"){
            result = "00" + line;
        } else if(lineLength == "2") {
            result = "0" + line;
        } else {
            result = line;
        }
        Console.WriteLine(result);
    }
}

STEP: 5 数字の文字列操作(時刻1)

問題文:https://paiza.jp/works/mondai/c_rank_level_up_problems/c_rank_string_step5

paiza.cs
using System;
class Program
{
    static void Main()
    {
        var line = Console.ReadLine();
        string[] HH_MM = line.Split(':');
        string HH = HH_MM[0];
        string MM = HH_MM[1];
        if(HH[0] == '0'){
            HH = HH[1].ToString();
        }
        if(MM[0] == '0'){
            MM = MM[1].ToString();
        }
        
        Console.WriteLine(HH);
        Console.WriteLine(MM);
    }
}

STEP: 6 数字の文字列操作(時刻2)

問題文:https://paiza.jp/works/mondai/c_rank_level_up_problems/c_rank_string_step6

paiza.cs
using System;
class Program
{
    static void Main()
    {
        var line = Console.ReadLine();
        string[] HH_MM = line.Split(':');
        
        int HH = int.Parse(HH_MM[0]);
        int MM = int.Parse(HH_MM[1]);
        string HHString = HH.ToString();
        string MMString = MM.ToString();
        
        if(MM < 30){
            MM += 30;
            if(HH.ToString().Length == 1){
                HHString = "0" + HH.ToString(); 
            }
            MMString = MM.ToString();
        } else {
            MM -= 30;
            HH += 1;
            if(HH.ToString().Length == 1){
                HHString = "0" + HH.ToString();
            } else {
                HHString = HH.ToString();
            }
            if(MM.ToString().Length == 1){
                MMString = "0" + MM.ToString();
            } else {
                MMString = MM.ToString();
            }
            
        }
        Console.WriteLine(HHString + ":" + MMString);
    }
}  

FINAL問題 文字列

問題文:https://paiza.jp/works/mondai/c_rank_level_up_problems/c_rank_string_boss

paiza.cs
using System;
class Program
{
    static void Main()
    {
        // 自分の得意な言語で
        // Let's チャレンジ!!
        var N = int.Parse(Console.ReadLine());
        string[] timeArray = new string [3];
        string[] startTimeArray = new string [2];
        int startHH = 0;
        int startMM = 0;
        int addHH = 0;
        int addMM = 0;
        int HH = 0;
        int MM = 0;
        string stringHH = HH.ToString();
        string stringMM = MM.ToString();
        
        for(var i = 0; i < N ; i ++){
            timeArray = Console.ReadLine().Split(' ');
            //工事開始時刻
            startTimeArray = timeArray[0].Split(':');
            startHH = int.Parse(startTimeArray[0]);
            startMM = int.Parse(startTimeArray[1]);
            // 工事時間(HH:時 MM:分)24時以上60分以上になる場合の処理
            addHH = int.Parse(timeArray[1]);
            addMM = int.Parse(timeArray[2]);
            MM = startMM + addMM;
            if(MM >= 60){
                MM -= 60;
                startHH += 1;
            }
            HH = startHH + addHH;
            if(HH >= 24){
                HH -= 24;
            }
            //2桁出力になるような処理            
            if(HH.ToString().Length == 1){
                stringHH = "0" + HH.ToString();
            } else {
                stringHH = HH.ToString();
            }
            if(MM.ToString().Length == 1){
                stringMM = "0" + MM.ToString();
            } else {
                stringMM = MM.ToString();
            }
            //工事終了時間            
            Console.WriteLine(stringHH + ":" + stringMM);
        }
        
    }
}
1
0
1

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
1
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?