LoginSignup
0
0

Paiza Cランク獲得問題(Dランク相当) 二重ループメニュー

Posted at

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

目次

  • 指定された範囲・行数の数字の出力
  • 二次元配列の出力
  • 二次元配列の最大の要素
  • 階段の出力
  • 二重ループ:基本編 積の最大
  • 行列の転置
  • かけ算表
  • 素数の個数
  • スーパー鳩時計
  • 格子点
  • お金の支払い
  • 二重ループ:活用編 三角形の探索

指定された範囲・行数の数字の出力

問題文

using System;

class Program
{
    public static void Main()
    {
        string [] NK=Console.ReadLine().Split();
        int N=int.Parse(NK[0]);
        int K=int.Parse(NK[1]);
        for(int i=0; i<K;i++){
            for(int j=0; j<N;j++){
                if(j==N-1){
                    Console.WriteLine(j+1);
                }else{
                    Console.Write(j+1+" ");
                }
            }
        }
    }
}


二次元配列の出力

問題文

using System;
using System.Linq;

class Program
{
    public static void Main()
    {
        string [] NK=Console.ReadLine().Split();
        int N=int.Parse(NK[0]);
        int K=int.Parse(NK[1]);
        int [][]data=new int[N][];
        for(int i=0; i<N;i++)  {
            data[i]=Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        }


        for(int i=0; i<N;i++){
            for(int j=0; j<K;j++){
                if(j==K-1){
                    Console.WriteLine(data[i][j]);
                }else{
                    Console.Write(data[i][j]+" ");
                }
            }
        }
    }
}


二次元配列での要素の検索

問題文

using System;
using System.Linq;

class Program
{
    public static void Main()
    {
        string [] NK=Console.ReadLine().Split();
        int N=int.Parse(NK[0]);
        int K=int.Parse(NK[1]);
        int [][]data=new int[N][];
        for(int i=0; i<N;i++)  {
            data[i]=Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        }

        int row=0;
        int colum=0;

        for(int i=0; i<N;i++){
            for(int j=0; j<K;j++){
                if(data[i][j]==1){
                    row=i+1;
                    colum=j+1;
                    break;
                }
            }
        }
        Console.WriteLine(row+" "+colum);
    }
}




二次元配列の最大の要素

問題文

using System;
using System.Linq;

class Program
{
    public static void Main()
    {
        string [] NK=Console.ReadLine().Split();
        int N=int.Parse(NK[0]);
        int K=int.Parse(NK[1]);
        int []data=new int[N];
        int [] max=new int[N];
        for(int i=0; i<N;i++)  {
            data=Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
            max[i]=data.Max();
        }

        Console.WriteLine(max.Max());

    }
}


題意は無視しています。
次の和もほぼ同じです。

さまざまな長さの配列の和

問題文

using System;
using System.Linq;

class Program
{
    public static void Main()
    {
        int N=int.Parse(Console.ReadLine());
        for(int i=0; i<N;i++)  {
            int []data=Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
            int sum=0;
            for(int j=1; j<data.Length;j++){
                sum+=data[j];
            }
            Console.WriteLine(sum);
        }
    }
}


階段の出力

問題文

using System;

class Program
{
    public static void Main()
    {
        int N = int.Parse(Console.ReadLine());

        for (int i = 1; i <= N; i++){
            for (int j = 1; j <= i; j++){
                Console.Write(j);
                if (j < i){
                    Console.Write(" ");
                }
            }
            Console.WriteLine();
        }
    }
}


二重ループ:基本編 積の最大

問題文

using System;
using System.Linq;
class Program
{
    public static void Main()
    {
        string [] NK=Console.ReadLine().Split();
        int N=int.Parse(NK[0]);
        int K=int.Parse(NK[1]);
        int []A=Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        int []B=Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        int[][] calc = new int[N][]; 
        int []max=new int [N];
        for(int i = 0; i < N; i++){
            calc[i] = new int[K];
            for(int j = 0; j < K; j++){
                calc[i][j]=A[i]*B[j];
            }
            max[i]=calc[i].Max();
        }
        Console.WriteLine(max.Max());
    }
}


行列の転置

問題文

using System;
using System.Linq;
class Program
{
    public static void Main()
    {
        string [] NK=Console.ReadLine().Split();
        int N=int.Parse(NK[0]);
        int K=int.Parse(NK[1]);
        int[,] A=new int [N,K];
        for(int i=0;i<N;i++)   {
            int []row = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
            for(int j=0;j<K;j++){
                A[i,j] = row[j];
            }
        }
        int[,] AT=new int [K,N];
        for(int i=0;i<K;i++) {
            for(int j=0;j<N;j++){
                AT[i,j]=A[j,i];
            }
        }

        for(int i=0;i<K;i++){
            for(int j=0;j<N;j++){
                Console.Write(AT[i, j]);
                if(j<N-1){
                    Console.Write(" ");
                }
            }
            Console.WriteLine();
        }
    }
}


かけ算表

問題文

using System;
using System.Linq;
class Program
{
    public static void Main()
    {
        int N =int.Parse(Console.ReadLine());
        int []A=Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        int [,]calc=new int[N,N];
        for(int i=0; i<N; i++){
            for(int j=0; j<N; j++) {
                calc[i,j] = A[i]*A[j];
            }
        }
        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
                Console.Write(calc[i, j]);
                if(j<N-1){
                    Console.Write(" ");
                }
            }
            Console.WriteLine();
        }
    }
}


素数の個数

問題文

using System;

class Program
{
    public static void Main()
    {
        int N =int.Parse(Console.ReadLine());
        int count=0;
        for(int i=2; i<=N;i++){
            bool isPrime=true;
            for(int j=2; j <= Math.Sqrt(i); j++){
                if (i % j == 0){
                    isPrime=false;
                    break;
                }
            }
            if (isPrime){
                count++;
            }
        }
        Console.WriteLine(count);
    }
}


log2

問題文

using System;

class Program
{
    public static void Main()
    {
        int N =int.Parse(Console.ReadLine());
        int count=0;
        for (int i = 2; i <= N; i *= 2){
            count += N / i;
        }
        Console.WriteLine(count);
    }
}


スーパー鳩時計

問題文

using System;

class Program
{
    public static void Main()
    {
        for(int i = 0; i < 24; i++){
            for(int j = 0; j < 60; j++){
                int sum=i+j;
                if(sum==0||(sum%3==0&&sum%5==0)){
                    Console.WriteLine("FIZZBUZZ");
                }else if(sum%3==0){
                    Console.WriteLine("FIZZ");
                }else if(sum%5==0){
                    Console.WriteLine("BUZZ");
                }else{
                    Console.WriteLine();
                }
            }
        }
    }
}


格子点

問題文

using System;
class Program
{
    public static void Main()
    {
        int max=0;
        for(int i = 1; i < 99; i++){
            for(int j = 1; j < 100-i; j++){
                double x=Math.Pow(i,3);
                double y=Math.Pow(j,3);
                if(x+y<100000 && max<i*j){
                    max=i*j;
                }
            }
        }
        Console.WriteLine(max);
    }
}


お金の支払い

問題文

using System;
using System.Linq;

class Program
{
    public static void Main()
    {
        string []XYZ=Console.ReadLine().Split();
        int X=int.Parse(XYZ[0]);
        int Y=int.Parse(XYZ[1]);
        int Z=int.Parse(XYZ[2]);
        
        int ans=Z;
        for(int x=0;x*X<=Z;x++){
            for(int y=0;y*Y+x*X<=Z;y++){
                if(x * X + y * Y <= Z){
                    int one=Z-x*X-y*Y;
                    if(x + y + one < ans){
                        ans=x + y + one;
                    }
                }
            }
        }
        Console.WriteLine(ans);
    }
}


二重ループ:活用編 三角形の探索

問題文

using System;
using System.Linq;

class Program
{
    public static void Main()
    {
        int N=int.Parse(Console.ReadLine());
        bool found=false;
        for(int a=1;a<N;a++){
            for(int b=1;b<N-a;b++){
                int c=N-a-b;
                if(a*a==b*b+c*c){
                    found=true;
                    break;
                }
            }
        }
        if(found){
                    Console.WriteLine("YES");
                }else{
                    Console.WriteLine("NO");
                }
    }
}


0
0
2

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