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ランク相当)ループメニュー1 ②

Posted at

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

目次

  • 数列の反転
  • 九九の表示 1
  • 九九の表示 2
  • 2 の累乗を表示
  • FizzBuzz
  • 数列の最大値
  • 数列の何番目にある?
  • 数列の中に何個ある?

数列の反転

問題文

using System;
using System.Linq;
class Program
{
    static void Main()
    {
        int N = int.Parse(Console.ReadLine());
        int[] num = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        for (int i = N-1; i >=0 ; i--) {
            Console.WriteLine(num[i]);        
        }
    }
}

九九の表示 1

問題文

using System;
class Program
{
    static void Main()
    {
        for(int i = 1; i < 10; i++) {
            if (i == 9)
            {
                Console.Write(i * 8 );
            }
            else
            {
                Console.Write(i * 8+" ");
            }
        }
    }
}

九九の表示 2

問題文

using System;
class Program
{
    static void Main()
    {
        int N =int.Parse(Console.ReadLine());
        for(int i = 0; i < 9; i++)
        {
            if(i == 8)
            {
                Console.Write(N * (i + 1));
            }
            else
            {
                Console.Write(N * (i + 1) + " ");
            }
            
        }
    }
}

2 の累乗を表示

問題文

using System;

class Program
{
    static void Main()
    {
        int N=int.Parse(Console.ReadLine());
        for(int i=1;i<=N;i++){
            double powResult=Math.Pow(2,i);
            Console.WriteLine(powResult);
        }
    }
}

FizzBuzz

問題文

using System;

class Program
{
    static void Main()
    {
        
        for(int i=1;i<=100;i++){
            if(i%3 ==0 && i%5 ==0){
                Console.WriteLine("FizzBuzz");
            }else if(i%3 ==0){
                Console.WriteLine("Fizz");
            }else if(i%5 ==0){
                Console.WriteLine("Buzz");
            }else{
                Console.WriteLine(i);
            }
        }
    }
}

数列の最大値

問題文

using System;
using System.Linq;

class Program

{
    static void Main()
    {
        int N=int.Parse(Console.ReadLine());
        int[] a = Console.ReadLine().Split().Select(int.Parse).ToArray();
        Console.WriteLine(a.Max());
        
    }
}

最小値も同じです。
文字列の配列から整数の配列を生成するためにSelect(int.Parse)を使用しています。この記述の部分でLinqが必要です。
題意に沿っていないので模範解答ではないと思います。

数列の何番目にある?

問題文

using System;
class Program

{
    static void Main()
    {
        int N=int.Parse(Console.ReadLine());
        string[] a = Console.ReadLine().Split();
        int result=Array.IndexOf(a,"1")+1;
        Console.WriteLine(result);
    }
}

IndexOfメソッドを用いました。
配列で受け取っているので、Arrayが必要です。

数列の中に何個ある?

問題文

using System;
class Program
{
    static void Main()
    {
        int N = int.Parse(Console.ReadLine());
        string[] input = Console.ReadLine().Split();
        
        int count = 0;
        for (int i = 0; i < N; i++)
        {
            int num = int.Parse(input[i]);
            if (num == 1)
            {
                count++;
            }
        }
        
        Console.WriteLine(count);
    }
}

Linqを用いる手法もそのままネットに転がっていました。
何をしているのか分からなかったので、やめました。

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?