0
1

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.

Javaメモ(標準入力/関数系)

Last updated at Posted at 2022-11-29

Java標準入出力

前提

Scanner sc = new Scanner(System.in);

整数(Int)の入力

a
int a = sc.nextInt();

整数(Int)の入力

b c
int b = sc.nextInt();
int c = sc.nextInt();

整数(Int)の入力 H×W

H W
a1,1 a1,2 a1,3 ... a1,w
.
.
.
ah,1 ah,2 ah,3 ... ah,w

int H = sc.nextInt();
int W = sc.nextInt();
int [][] A = new int[H][W];
for (int i=0;i<H;i++){
    for (int j=0;j<W;j++){
        A[i][j] = Integer.parseInt(sc.next());
    }
}

整数(Long)の入力

A B C
long A = sc.nextLong();
long B = sc.nextLong();
long C = sc.nextLong();

整数(Long)の3×Q 入力

l1 r1 v1
l2 r2 v2
l3 r3 v3
...
long [] L = new long[Q];
long [] R = new long[Q];
long [] V = new long[Q];
        
for (int i=0;i<Q;i++){
    L[i] = Long.parseLong(sc.next());
    R[i] = Long.parseLong(sc.next());
    V[i] = Long.parseLong(sc.next());
}

整数(Int)縦にN個入力

N
a_1 a_2 a_3 a_4
...
int N = sc.nextInt();
int[] a = new int[N];
for (int i=0; i<N; i++){
    a[i] = Integer.parseInt(sc.next());
}

整数(Int)縦にN個入力

N
a_1
a_2
a_3
a_4
...
int N = sc.nextInt();
int[] a = new int[N];
for (int i = 0; i < N; i++) {
  a[i] = sc.nextInt();
}

文字列の入力

hogehoge
String s = sc.next();

関数

最大公約数 gcd

static int gcd(int a, int b){
        if(b == 0){
            return a;
        }
        return gcd(b,a % b);
    }

最小公倍数 lcm

static int lcm (int a, int b) {
	int temp;
	long c = a;
	c *= b;
	while((temp = a%b)!=0) {
		a = b;
		b = temp;
	}
	return (int)(c/b);
}

◯進数

// ansを8進数にする
Long.parseLong(ans,8)

Permutation、順列

public static ArrayList<ArrayList<Integer>> permute(int[] arr) {
        ArrayList<ArrayList<Integer>> l = new ArrayList<>();
        permuteHelper(l, new ArrayList<>(), arr);
        return l;
    }
 
private static void permuteHelper(ArrayList<ArrayList<Integer>> list, ArrayList<Integer> resultList, int [] arr){
        // Base case
        if(resultList.size() == arr.length){
            list.add(new ArrayList<>(resultList));
        }
        else{
            for(int i = 0; i < arr.length; i++){
 
                if(resultList.contains(arr[i]))
                {
                    // If element already exists in the list then skip
                    continue;
                }
                // Choose element
                resultList.add(arr[i]);
                // Explore
                permuteHelper(list, resultList, arr);
                // Unchoose element
                resultList.remove(resultList.size() - 1);
            }
        }
    }

a^bの値を、mod(10^9+7)で割った値

final static long mod = 1000000007;
static long binpower(long a, long b){
        long ans = 1;
        while(b!=0){
            if (b%2==1){
                ans = ans * a % mod;
            }
            a = a * a % mod;
            b/=2;
        }
        return ans;
    }

参考:https://github.com/E869120/kyopro_educational_90/blob/main/sol/069.cpp

log2○

public static double log2(int x) {
        return Math.log(x) / Math.log(2);
    }

参考:https://www.techiedelight.com/ja/calculate-log-base-2-integer-java/

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?