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ランク相当) 配列メニュー ②

Posted at

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

目次

  • 配列に含まれている? 1
  • 配列に含まれている? 3
  • 何番目にある? 1
  • 何個ある? 1
  • 何個ある? 2
  • 配列の書き換え
  • 2 変数の入れ替え
  • 配列の要素の入れ替え
  • 部分配列
  • 配列の連結

配列に含まれている? 1

問題文

using System;
using System.Linq;
class Program

{
    static void Main()
    {
        int []array= new int []{10, 13, 21, 1, 6, 51, 10, 8, 15, 6};
        Boolean result=array.Contains(6);
        if(result){
            Console.WriteLine("Yes");
        }else{
            Console.WriteLine("No");
        }
    }
}

色々やり方はあると思います
次の2もほぼ同じです

配列に含まれている? 3

問題文

using System;
using System.Linq;
class Program

{
    static void Main()
    {
        string [] NM=Console.ReadLine().Split();
        int N=int.Parse(NM[0]);
        int M=int.Parse(NM[1]);
        int [] a =Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        Boolean result=a.Contains(M);
        if(result){
            Console.WriteLine("Yes");
        }else{
            Console.WriteLine("No");
        }
    }
}

何番目にある? 1

問題文

using System;
class Program

{
    static void Main()
    {
         int []array= new int []{1, 10, 2, 9, 3, 8, 4, 7, 5, 6};
         int result =Array.IndexOf(array,8);
         Console.WriteLine(result+1);
    }
}

何個ある? 1

問題文

using System;
class Program

{
    static void Main()
    {
        int []array= new int []{1, 2, 2, 1, 2, 1, 2, 1, 1 ,1};
        int num=1;
        int count=0;
        foreach(int a in array){
            if(a==num){
                count+=1;
            }
        }
        Console.WriteLine(count);
    }
}

何個ある? 2

問題文

using System;
class Program

{
    static void Main()
    {
        int []array= new int []{1, 2, 5, 1, 4, 3, 2, 5, 1, 4};
        int N=int.Parse(Console.ReadLine());
        int count=0;
        foreach(int a in array){
            if(a==N){
                count+=1;
            }
        }
        Console.WriteLine(count);
    }
}

【配列の検索】何個ある? Boss

問題文

using System;
using System.Linq;
class Program

{
    static void Main()
    {
        string [] NM=Console.ReadLine().Split();
        int N=int.Parse(NM[0]);
        int M=int.Parse(NM[1]);
        int [] array=Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        int count=0;
        foreach(int a in array){
            if(a==N){
                count+=1;
            }
        }
        Console.WriteLine(count);
    }
}

配列の書き換え

問題文

using System;
using System.Linq;

class Program
{
    static void Main()
    {
        int[] ABN = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        int A=ABN[0];
        int B=ABN[1];
        int N=ABN[2];

        int[] a = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();

        for(int i=0;i<N;i++){
            if(a[i]==A){
                Console.WriteLine(B);
            }else{
                Console.WriteLine(a[i]);
            }
        }
    }
}

2 変数の入れ替え

問題文

using System;
using System.Linq;

class Program
{
    static void Main()
    {
        int[] AB = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        int A=AB[0];
        int B=AB[1];
        int tmp=A;
        A=B;
        B=tmp;
        Console.WriteLine(A+" "+B);
    }
}

配列の要素の入れ替え

問題文

using System;
using System.Linq;

class Program
{
    static void Main()
    {
        int[] ABN = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        int A=ABN[0];
        int B=ABN[1];
        int N=ABN[2];
        int tmp;

        int[] a = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();

        tmp= a[A-1];
        a[A-1]=a[B-1];
        a[B-1]=tmp;

        for(int i=0;i<N;i++){
            Console.WriteLine(a[i]);
        }
    }
}

部分配列

問題文

using System;
using System.Linq;

class Program
{
    static void Main()
    {
        int[] ABN = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        int A=ABN[0];
        int B=ABN[1];
        int N=ABN[2];
        

        int[] a = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();

        for(int i=A-1;i<B;i++){
            Console.WriteLine(a[i]);
        }
        
    }
}

配列の連結

問題文

using System;
using System.Linq;

class Program
{
    static void Main()
    {
        int[] NM = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        
        int[] N = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        int[] M = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();

        for(int i=0;i<N.Length;i++){
            Console.WriteLine(N[i]);
        }
        for(int i=0;i<M.Length;i++){
            Console.WriteLine(M[i]);
        }
        
    }
}

確実に趣旨に沿っていません。
listで受け取ってaddallを使用すると楽なのかな

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?