import java.util.Scanner;
public class Main{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int n;
		double[] data1 =new double[10];
		double[] data2 =new double[10];
		double[] ans =new double[10];
		double D=0,D2=0,D3=0,D4=Double.MIN_VALUE;

		n = sc.nextInt();
		for(int i=0;i<n;i++)data1[i]=sc.nextDouble();
		for(int i=0;i<n;i++)data2[i]=sc.nextDouble();
		for(int i=0;i<n;i++)ans[i] = data1[i]-data2[i];
		for(int i=0;i<n;i++){
			D +=Math.abs(ans[i]);
			D2 +=Math.pow(Math.abs(ans[i]),2);
			D3 +=Math.pow(Math.abs(ans[i]),3);
			if(Math.abs(ans[i])>D4)D4=Math.abs(ans[i]);

		}
		System.out.println(D);
		System.out.println(Math.sqrt(D2));
		System.out.println(Math.cbrt(D3));
		System.out.println(D4);

		sc.close();

	}

}

MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Distance II

Your task is to calculate the distance between two $n$ dimensional vectors $x = \{x_1, x_2, ..., x_n\}$ and $y = \{y_1, y_2, ..., y_n\}$.

The Minkowski's distance defined below is a metric which is a generalization of both the Manhattan distance and the Euclidean distance.

\[
D_{xy} = (\sum_{i=1}^n |x_i - y_i|^p)^{\frac{1}{p}}
\]

It can be the Manhattan distance

\[
D_{xy} = |x_1 - y_1| + |x_2 - y_2| + ... + |x_n - y_n|
\]

where $p = 1 $.

It can be the Euclidean distance

\[
D_{xy} = \sqrt{(|x_1 - y_1|)^{2} + (|x_2 - y_2|)^{2} + ... + (|x_n - y_n|)^{2}}
\]

where $p = 2 $.

Also, it can be the Chebyshev distance

\[
D_{xy} = max_{i=1}^n (|x_i - y_i|)
\]

where $p = \infty$

Write a program which reads two $n$ dimensional vectors $x$ and $y$, and calculates Minkowski's distance where $p = 1, 2, 3, \infty$ respectively.

Input

In the first line, an integer $n$ is given. In the second and third line, $x = \{x_1, x_2, ... x_n\}$ and $y = \{y_1, y_2, ... y_n\}$ are given respectively. The elements in $x$ and $y$ are given in integers.

Output

Print the distance where $p = 1, 2, 3$ and $\infty$ in a line respectively.
The output should not contain an absolute error greater than 10-5.

Constraints

$1 \leq n \leq 100$

$0 \leq x_i, y_i \leq 1000$

Sample Input

3
1 2 3
2 0 4

Sample Output

4.000000
2.449490
2.154435
2.000000


import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws Exception {
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] line = br.readLine().split(" ");

        int[] data = new int[line.length];
        for (int i = 0; i < data.length; i++) {
        	data[i] = Integer.parseInt(line[i]);
		}

        Dice dice = new Dice(data);

        char[] moveData = br.readLine().toCharArray();
        for (int i = 0; i < moveData.length; i++) {
        	dice.move(moveData[i]);
        }

        System.out.println(dice.getTop());
    }
}

class Dice {
	private int top;
	private int bottom;
	private int left;
	private int right;
	private int front;
	private int back;

	Dice(int[] data) {
		this.top    = data[1 - 1];
		this.bottom = data[6 - 1];
		this.left   = data[4 - 1];
		this.right  = data[3 - 1];
		this.front  = data[2 - 1];
		this.back   = data[5 - 1];
	}

	public void move(char m) {
		int temp = -1;
		switch (m) {
		case 'N':
			temp = this.top;
			this.top    = this.front;
			this.front  = this.bottom;
			this.bottom = this.back;
			this.back   = temp;

			break;

		case 'E':
			temp = this.top;
			this.top    = this.left;
			this.left   = this.bottom;
			this.bottom = this.right;
			this.right  = temp;

			break;

		case 'W':
			temp = this.top;
			this.top    = this.right;
			this.right  = this.bottom;
			this.bottom = this.left;
			this.left   = temp;

			break;

		case 'S':
			temp = this.top;
			this.top    = this.back;
			this.back   = this.bottom;
			this.bottom = this.front;
			this.front  = temp;

			break;
		}
	}

	public int getTop() {
		return this.top;
	}
}

Global OOD problem IDs are disjoint from train, validation, and in-distribution test problem IDs.

MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Dice I

Write a program to simulate rolling a dice, which can be constructed by the following net.

As shown in the figures, each face is identified by a different label from 1 to 6.

Write a program which reads integers assigned to each face identified by the label and a sequence of commands to roll the dice, and prints the integer on the top face. At the initial state, the dice is located as shown in the above figures.

Input

In the first line, six integers assigned to faces are given in ascending order of their corresponding labels.

In the second line, a string which represents a sequence of commands, is given. The command is one of 'E', 'N', 'S' and 'W' representing four directions shown in the above figures.

Output

Print the integer which appears on the top face after the simulation.

Constraints

$0 \leq $ the integer assigned to a face $ \leq 100$

$0 \leq $ the length of the command $\leq 100$

Sample Input 1

1 2 4 8 16 32
SE

Sample Output 1

8

You are given a dice where 1, 2, 4, 8, 16 are 32 are assigned to a face labeled by 1, 2, ..., 6 respectively. After you roll the dice to the direction S and then to the direction E, you can see 8 on the top face.

Sample Input 2

1 2 4 8 16 32
EESWN

Sample Output 2

32

Submitted source-code file size, in bytes.

Execution CPU time reported by official Project CodeNet metadata, in milliseconds.

Peak memory usage reported by official Project CodeNet metadata, in kilobytes.


import java.util.Random;
import java.util.Scanner;

public class Main
{
	public static void main(String[] args)
	{
		Scanner scanner = new Scanner(System.in);

		int[] n = new int[6];
		for(int i=0; i<6; i++)
		{
			n[i] = scanner.nextInt();
		}

		int q = scanner.nextInt();

		for(int i=0; i<q; i++)
		{
			int[] in = new int[2];
			in[0] = scanner.nextInt();
			in[1] = scanner.nextInt();

			for(;;)
			{
				Random random = new Random();
				int d = random.nextInt(4);

				int[] buf = new int[6];
				for(int j=0; j<6; j++)
				{
					buf[j] = n[j];
				}

				switch(d)
				{
					case 0:
						n[0] = buf[4];
						n[1] = buf[0];
						n[5] = buf[1];
						n[4] = buf[5];
						break;
					case 1:
						n[0] = buf[1];
						n[1] = buf[5];
						n[5] = buf[4];
						n[4] = buf[0];
						break;
					case 2:
						n[0] = buf[2];
						n[2] = buf[5];
						n[5] = buf[3];
						n[3] = buf[0];
						break;
					case 3:
						n[0] = buf[3];
						n[3] = buf[5];
						n[5] = buf[2];
						n[2] = buf[0];
						break;
				}
				if(in[0]==n[0] && in[1]==n[1])
				{
					System.out.println(n[2]);
					break;
				}
			}
		}
	}
}

MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Dice II

Construct a dice from a given sequence of integers in the same way as Dice I.

You are given integers on the top face and the front face after the dice was rolled in the same way as Dice I. Write a program to print an integer on the right side face.

Input

In the first line, six integers assigned to faces are given in ascending order of their corresponding labels.
In the second line, the number of questions $q$ is given.

In the following $q$ lines, $q$ questions are given. Each question consists of two integers on the top face and the front face respectively.

Output

For each question, print the integer on the right side face.

Constraints

$0 \leq $ the integer assigned to a face $ \leq 100$

The integers are all different

$1 \leq q \leq 24$

Sample Input

1 2 3 4 5 6
3
6 5
1 3
3 2

Sample Output

3
5
6


import java.util.*;

public class Main{

	private static final Scanner scan = new Scanner(System.in);

	public static void main(String[] args){
		List<Integer> list = new ArrayList<Integer>();
		int[] d = new int[6];
		for(int i = 0; i < 6; i++){
			d[i] = scan.nextInt();
		}
		Dice dice = new Dice(d);
		int q = scan.nextInt();
		for(int i = 0; i < q; i++){
			int n = scan.nextInt();
			int m = scan.nextInt();
			dice.setTop(n);
			dice.setSouth(m);
			list.add(dice.show(Dice.EAST));
		}

		for(int n : list){
			System.out.println(n);
		}
	}
}

class Dice{
	int[] d_ = new int[6];
	public static final int
		TOP = 0, SOUTH = 1, EAST = 2,
		WEST = 3, NORTH = 4, BOTTOM = 5;

	public Dice(int[] d){
		for(int i = 0; i < 6; i++){
			d_[i] = d[i];
		}
	}

	public void setTop(int num){
		if(num == d_[EAST]){
			this.rollWest();
		} else if(num == d_[WEST]){
			this.rollEast();
		} else if(num == d_[SOUTH]){
			this.rollNorth();
		} else if(num == d_[NORTH]){
			this.rollSouth();
		} else if(num == d_[BOTTOM]){
			this.rollEast();
			this.rollEast();
		}
	}

	public void setSouth(int num){
		if(num == d_[EAST]){
			this.rollRight();
			this.rollRight();
			this.rollRight();
		} else if(num == d_[WEST]){
			this.rollRight();
		} else if(num == d_[NORTH]){
			this.rollRight();
			this.rollRight();
		}
	}

	public int show(int side){
		return d_[side];
	}

	public void rollEast(){
		int work = d_[TOP];
		d_[TOP] = d_[WEST];
		d_[WEST] = d_[BOTTOM];
		d_[BOTTOM] = d_[EAST];
		d_[EAST] = work;
	}

	public void rollWest(){
		int work = d_[TOP];
		d_[TOP] = d_[EAST];
		d_[EAST] = d_[BOTTOM];
		d_[BOTTOM] = d_[WEST];
		d_[WEST] = work;
	}

	public void rollSouth(){
		int work = d_[TOP];
		d_[TOP] = d_[NORTH];
		d_[NORTH] = d_[BOTTOM];
		d_[BOTTOM] = d_[SOUTH];
		d_[SOUTH] = work;
	}

	public void rollNorth(){
		int work = d_[TOP];
		d_[TOP] = d_[SOUTH];
		d_[SOUTH] = d_[BOTTOM];
		d_[BOTTOM] = d_[NORTH];
		d_[NORTH] = work;
	}

	public void rollRight(){
		int work = d_[SOUTH];
		d_[SOUTH] = d_[WEST];
		d_[WEST] = d_[NORTH];
		d_[NORTH] = d_[EAST];
		d_[EAST] = work;
	}
}


import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		Dice d = new Dice();
		for(int i = 0; i < 6; i++) {
			d.side[i] = sc.nextInt();
		}
		int q = sc.nextInt();
		for(int i = 0; i < q; i++) {
			int a = sc.nextInt();
			int b = sc.nextInt();
			d.getRightSide(a, b);
		}

		sc.close();
	}
}

class Dice {
	int[] side = new int[6];

	public void getUpperSide() {
		System.out.println(this.side[0]);
	}
	public void getRightSide(int a, int b) {
		int[][] mtx = {{0, 3, 5, 2, 4, 0}, {4, 0, 1, 6, 0, 3},
{2, 6, 0, 0, 1, 5}, {5, 1, 0, 0, 6, 2}, {3, 0, 6, 1, 0, 4}, {0, 4, 2, 5, 3, 2, 0}};
		int aI = -1;
		int bI = -1;
		for(int i = 0; i < 6; i++) {
			if(a==this.side[i]) {
				aI = i;
			}
			if(b==this.side[i]) {
				bI = i;
			}
			if(aI>=0&&bI>=0) break;
		}
		System.out.println(this.side[mtx[aI][bI] - 1]);
	}

	public void rollE() {
		int w = this.side[5];
		this.side[5] = this.side[2];
		this.side[2] = this.side[0];
		this.side[0] = this.side[3];
		this.side[3] = w;
	}
	public void rollW() {
		int w = this.side[5];
		this.side[5] = this.side[3];
		this.side[3] = this.side[0];
		this.side[0] = this.side[2];
		this.side[2] = w;
	}
	public void rollN() {
		int w = this.side[5];
		this.side[5] = this.side[4];
		this.side[4] = this.side[0];
		this.side[0] = this.side[1];
		this.side[1] = w;
	}
	public void rollS() {
		int w = this.side[5];
		this.side[5] = this.side[1];
		this.side[1] = this.side[0];
		this.side[0] = this.side[4];
		this.side[4] = w;
	}
}


// ???????????? ??¢
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO ?????????????????????????????????????????????

		Scanner scan = new Scanner(System.in);

		// ???????????????2?????????
		Dise3 dise = new Dise3();
		// ?????¢?????°??????????????????
		for(int i = 0; i < 6; i++) {
			dise._values[i] = scan.nextInt();
		}
		Dise3 dise2 = new Dise3();
		// ?????¢?????°??????????????????
		for(int i = 0; i < 6; i++) {
			dise2._values[i] = scan.nextInt();
		}

		// ??????????????????????????????????´????????????°
		boolean result = false;

		// ?¢????
		for(int i2 = 0; i2 <= 6; i2++) {
			// ?????¢????¢????
			if (dise._values[Dise3.ValueType.Front.getId()] == dise2._values[Dise3.ValueType.Front.getId()]) {
				// ?????¢????¢????
				result = topVarueCheck(dise, dise2, result);
			}
			else {
				dise2.MoveSouth();
			}
			if (result == true) break;
			// 4?????????????????±??¨?????????????????????????????¢????????????
			if (i2 == 3) {
				dise2.MoveEast();
				dise2.MoveSouth();
			}
		}
		if (result == true) System.out.println("Yes");
		else System.out.println("No");
	}

	private static boolean topVarueCheck(Dise3 dise, Dise3 dise2, boolean result) {
		// ?????¢????????£????????????????????¢??????????????§??±?????????
		for(int i3 = 0; i3 < 4; i3++) {
			if (dise._values[Dise3.ValueType.Top.getId()] == dise2._values[Dise3.ValueType.Top.getId()]) {
				// ????????¢????????£????????????
				if ( (dise._values[Dise3.ValueType.Right.getId()] == dise2._values[Dise3.ValueType.Right.getId()]) &&
					 (dise._values[Dise3.ValueType.Left.getId()] == dise2._values[Dise3.ValueType.Left.getId()]) &&
					 (dise._values[Dise3.ValueType.Back.getId()] == dise2._values[Dise3.ValueType.Back.getId()]) &&
					 (dise._values[Dise3.ValueType.Bottom.getId()] == dise2._values[Dise3.ValueType.Bottom.getId()]) ) {
					result = true;
					break;
				}
				// 6??¢????????´????????????????????°???????????????
			}
			else dise2.MoveEast();
		}
		return result;
	}
}

// ??????????????????
class Dise3 {

	public int _values[] = new int[6];

	public void Dice(int values[]) {
    	for (int i = 0; i < values.length; i++)
    	this._values[i] = values[i];
	}

    public enum ValueType {
        Top(0),
        Front(1),
        Right(2),
        Left(3),
        Back(4),
        Bottom(5);
        private final int id;
        private ValueType (final int id) {
            this.id = id;
        }
        public int getId() {
            return id;
        }
    }

    public void MoveWest()
    {
        int tmp = this._values[ValueType.Top.getId()];
        this._values[ValueType.Top.getId()] = this._values[ValueType.Right.getId()];
        this._values[ValueType.Right.getId()] = this._values[ValueType.Bottom.getId()];
        this._values[ValueType.Bottom.getId()] = this._values[ValueType.Left.getId()];
        this._values[ValueType.Left.getId()] = tmp;
    }
    public void MoveEast()
    {
        int tmp = this._values[ValueType.Top.getId()];
        this._values[ValueType.Top.getId()] = this._values[ValueType.Left.getId()];
        this._values[ValueType.Left.getId()] = this._values[ValueType.Bottom.getId()];
        this._values[ValueType.Bottom.getId()] = this._values[ValueType.Right.getId()];
        this._values[ValueType.Right.getId()] = tmp;
    }
    public void MoveNorth()
    {
        int tmp = this._values[ValueType.Top.getId()];
        this._values[ValueType.Top.getId()] = this._values[ValueType.Front.getId()];
        this._values[ValueType.Front.getId()] = this._values[ValueType.Bottom.getId()];
        this._values[ValueType.Bottom.getId()] = this._values[ValueType.Back.getId()];
        this._values[ValueType.Back.getId()] = tmp;
    }
    public void MoveSouth()
    {
        int tmp = this._values[ValueType.Top.getId()];
        this._values[ValueType.Top.getId()] = this._values[ValueType.Back.getId()];
        this._values[ValueType.Back.getId()] = this._values[ValueType.Bottom.getId()];
        this._values[ValueType.Bottom.getId()] = this._values[ValueType.Front.getId()];
        this._values[ValueType.Front.getId()] = tmp;
    }

}

MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Dice III

Write a program which reads the two dices constructed in the same way as Dice I, and determines whether these two dices are identical. You can roll a dice in the same way as Dice I, and if all integers observed from the six directions are the same as that of another dice, these dices can be considered as identical.

Input

In the first line, six integers assigned to faces of a dice are given in ascending order of their corresponding labels.

In the second line, six integers assigned to faces of another dice are given in ascending order of their corresponding labels.

Output

Print "Yes" if two dices are identical, otherwise "No" in a line.

Constraints

$0 \leq $ the integer assigned to a face $ \leq 100$

Sample Input 1

1 2 3 4 5 6
6 2 4 3 5 1

Sample Output 1

Yes

Sample Input 2

1 2 3 4 5 6
6 5 4 3 2 1

Sample Output 2

No


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main {

  public static void main(String[] args) throws IOException {

    try(BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {

      int n = Integer.parseInt(br.readLine());
      Dice[] aDices = new Dice[n];

      for ( int i=0; i<n; i++ )
        aDices[i] = new Dice(br.readLine().split(" "));

      String result = "Yes";
      for ( int i=1; i<n; i++ ) {
        if(aDices[0].isSame(aDices[i])) continue;
        else { result = "No"; break; }
      }

      System.out.println(result);
    }
  }
}

class Dice {
  private int[] side;

  public Dice( int[] ia ) {
    side = Arrays.copyOf(ia, ia.length);
  }
  public Dice( String[] s ) {
    side = new int[s.length];
    for ( int i=0; i < 6; i++)
      side[i] = Integer.parseInt(s[i]);
  }

  @Override
  public boolean equals(Object o) {
    if(o == this) return true;
    if(o == null) return false;
    if(!(o instanceof Dice)) return false;
    Dice r = (Dice) o;

    if(
      (side[0] == r.side[0]) &&
      (side[1] == r.side[1]) &&
      (side[2] == r.side[2]) &&
      (side[3] == r.side[3]) &&
      (side[4] == r.side[4]) &&
      (side[5] == r.side[5])
      ) return true;
    else return false;
  }

  public int getUpperSide() {
    return side[0];
  }
  public int getLowerSide() {
    return side[5];
  }
  public int getFrontSide() {
    return side[1];
  }
  public int getBackSide() {
    return side[4];
  }
  public int getRightSide() {
    return side[2];
  }
  public int getLeftSide() {
    return side[3];
  }

  public boolean isSame(Dice d) {

    d.setFrontPosition(this.getFrontSide());

    for(int i=0; i<4; i++)  {
      for(int j=0; j<4; j++) {
        if( this.equals(d) ) return true;
        else d.rotate("W");
      }
      d.rotate("N");
    }

    return false;
  }

  public void setFrontPosition(int n) {
    for(int i=0; i < 3; i++){
      if(getUpperSide() != n) rotate("N");
    }
    for(int i=0; i < 3; i++){
      if(getUpperSide() != n) rotate("W");
    }
    rotate("S");
  }

  public int doRotate(String s) {
    for ( int i=0; i < s.length(); i++ ) {
      rotate(s.substring(i, i+1));
    }
    return side[0];
  }

  public void rotate(String o) {
    switch(o) {
      case "N" :
        rotateN();
        break;
      case "S" :
        rotateS();
        break;
      case "W" :
        rotateW();
        break;
      case "E":
        rotateE();
        break;
      default :
    }
  }

  private void rotateN() {
    int tmp = side[0];
    side[0] = side[1]; side[1] = side[5]; side[5] = side[4];
    side[4] = tmp;
  }
  private void rotateS() {
    int tmp = side[0];
    side[0] = side[4]; side[4] = side[5]; side[5] = side[1];
    side[1] = tmp;
  }
  private void rotateW() {
    int tmp = side[0];
    side[0] = side[2]; side[2] = side[5]; side[5] = side[3];
    side[3] = tmp;
  }
  private void rotateE() {
    int tmp = side[0];
    side[0] = side[3]; side[3] = side[5]; side[5] = side[2];
    side[2] = tmp;
  }
}

MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Dice IV

Write a program which reads $n$ dices constructed in the same way as Dice I, and determines whether they are all different. For the determination, use the same way as Dice III.

Input

In the first line, the number of dices $n$ is given. In the following $n$ lines, six integers assigned to the dice faces are given respectively in the same way as Dice III.

Output

Print "Yes" if given dices are all different, otherwise "No" in a line.

Constraints

$2 \leq n \leq 100$

$0 \leq $ the integer assigned to a face $ \leq 100$

Sample Input 1

3
1 2 3 4 5 6
6 2 4 3 5 1
6 5 4 3 2 1

Sample Output 1

No

Sample Input 2

3
1 2 3 4 5 6
6 5 4 3 2 1
5 4 3 2 1 6

Sample Output 2

Yes


import java.io.*;

class Main {
    public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String s = in.readLine();
    int x = Integer.parseInt(s);
    int res = x * x * x;
    System.out.println(res);
    }
}

X Cubic

Write a program which calculates the cube of a given integer x.

Input

An integer x is given in a line.

Output

Print the cube of x in a line.

Constraints

1 ≤ x ≤ 100

Sample Input 1

2

Sample Output 1

8

Sample Input 2

3

Sample Output 2

27


import java.io.*;
class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader r =
         new BufferedReader(new InputStreamReader(System.in), 1);

        String s = r.readLine();
        int i = Integer.parseInt(s);

        if((1 <= i )&(i<= 100)){
            System.out.print(i*i*i);
        }
        r.close();
    }
}


import java.util.*;

class Main{
   public static void main(String[] args){
      Scanner x = new Scanner(System.in);
   int a,b,c,d;
    a = x.nextInt();
    b = x.nextInt();
    c = a*b;
    a = a*2+b*2;
   System.out.println(c+" "+a);
}
}

Rectangle

Write a program which calculates the area and perimeter of a given rectangle.

Input

The length a and breadth b of the rectangle are given in a line separated by a single space.

Output

Print the area and perimeter of the rectangle in a line. The two integers should be separated by a single space.

Constraints

1 ≤ a, b ≤ 100

Sample Input 1

3 5

Sample Output 1

15 16


import java.util.Scanner;

public class Main{

public static void main(String[] args) {

Scanner a = new Scanner(System.in);

String str = a.next();
int b = Integer.parseInt(str);
str = a.next();
int c = Integer.parseInt(str);
System.out.println(c);
}
}


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        int a = scan.nextInt();
        int b = scan.nextInt();

        if (a >= 1 && b <= 100) {
            int lapLength = a * 2 + b * 2;
            int squareMeasure = a*b;

            System.out.println(squareMeasure + " " + lapLength);
        }
    }
}


import java.util.Scanner;

class Main {
    static public void main(String[] av) {
	Scanner sc = new Scanner(System.in);
	int a = sc.nextInt();
	int b = sc.nextInt();
	int s = a*b;
	int l = a*2+b*2;

	System.out.println(s+" "+l);
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int sec=Integer.parseInt(reader.readLine());
        int h=sec/3600;
        int m=(sec%3600)/60;
        int s=(sec%60);
        System.out.println(Integer.toString(h) + ":" + Integer.toString(m) +":" +Integer.toString(s));
	}
}

MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Watch

Write a program which reads an integer $S$ [second] and converts it to $h:m:s$ where $h$, $m$, $s$ denote hours, minutes (less than 60) and seconds (less than 60) respectively.

Input

An integer $S$ is given in a line.

Output

Print $h$, $m$ and $s$ separated by ':'. You do not need to put '0' for a value, which consists of a digit.

Constraints

$0 \leq S \leq 86400$

Sample Input 1

46979

Sample Output 1

13:2:59


import java.util.Scanner;

public class Main{
    private static int a, b;
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        Main ain = new Main();
        Main.a = scan.nextInt();
        Main.b = scan.nextInt();
        if(Main.a < Main.b){
            System.out.println("a < b");
        }
        else if(Main.a > Main.b){
            System.out.println("a > b");
        }
        else{
            System.out.println("a == b");
        }
    }
}

Small, Large, or Equal

Write a program which prints small/large/equal relation of given two integers a and b.

Input

Two integers a and b separated by a single space are given in a line.

Output

For given two integers a and b, print

a < b

if a is less than b,

a > b

if a is greater than b, and

a == b

if a equals to b.

Constraints

-1000 ≤ a, b ≤ 1000

Sample Input 1

1 2

Sample Output 1

a < b

Sample Input 2

4 3

Sample Output 2

a > b

Sample Input 3

5 5

Sample Output 3

a == b


import java.util.*;
class Main{
	public static void main(String args[]){
		Scanner s = new Scanner(System.in);
		String indata = s.next();
		int a = Integer.parseInt(indata);
		indata=s.next();
		int b = Integer.parseInt(indata);
		if(a==b)System.out.println("a == b");
		else if(a>b)System.out.println("a > b");
		else System.out.println("a < b");
	}
}


import java.util.*;

public class Main{
  public static void main(String args[]){
    Scanner scanner = new Scanner(System.in);
    int inputA = scanner.nextInt();
    int inputB = scanner.nextInt();

    if(inputA<inputB){
      System.out.println("a < b");
    }else if(inputA>inputB){
      System.out.println("a > b");
    }else{
      System.out.println("a == b");
    }
  }
}


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int x = scanner.nextInt();
        int y = scanner.nextInt();
        String s = "";

        if (x > y) {
            s = ">";
        }
        else if (x < y) {
            s = "<";
        }
        else {
            s = "==";
        }
        System.out.print("a");
        System.out.print(" ");
        System.out.print(s);
        System.out.print(" ");
        System.out.println("b");
    }
}


import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();

		if(a < b){
			if(b < c){
				System.out.println("Yes");
			}else{
				System.out.println("No");
			}
		}else{
			System.out.println("No");
		}
	}
}

Range

Write a program which reads three integers a, b and c, and prints "Yes" if a < b < c, otherwise "No".

Input

Three integers a, b and c separated by a single space are given in a line.

Output

Print "Yes" or "No" in a line.

Constraints

0 ≤ a, b, c ≤ 100

Sample Input 1

1 3 8

Sample Output 1

Yes

Sample Input 2

3 8 1

Sample Output 2

No


import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Scanner;

class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		if(a <= b && b <= c)
		{
			System.out.println("Yes");
		}else{
			System.out.println("No");
		}
	}
}


import java.io.*;
class Main {
    public static void main( String[] args ) throws IOException {
        BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );
        String line = br.readLine();
        String[] p = line.split( " ", 0 );
        boolean flg = false;
        for( int i = 1 ; i < p.length ; i++ ) {
            if( Integer.parseInt( p[ i - 1 ] ) >= Integer.parseInt( p[ i ] ) ) {
                flg = true;
                break;
            }
        }
        if( flg ) {
            System.out.println( "Yes" );
        } else {
            System.out.println( "No" );
        }
    }
}


import java.util.Scanner;

class Main {

	public static void main(String[] args){

		Scanner sc = new Scanner(System.in);

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

		if(a < b && b < c)
			System.out.println("Yes");
		else
			System.out.println("No");
	}

}


import java.util.Scanner;

public class Main {
    Main(){

        int table[] = new int[3];
        Scanner sc = new Scanner(System.in);

        table[0] = sc.nextInt();
        table[1] = sc.nextInt();
        table[2] = sc.nextInt();

        java.util.Arrays.sort(table);
        System.out.println(table[0] + " " + table[1] + " " + table[2]);
    }

    public static void main(String[] args){
        new Main();
    }
}

Sorting Three Numbers

Write a program which reads three integers, and prints them in ascending order.

Input

Three integers separated by a single space are given in a line.

Output

Print the given integers in ascending order in a line. Put a single space between two integers.

Constraints

1 ≤ the three integers ≤ 10000

Sample Input 1

3 8 1

Sample Output 1

1 3 8


import java.util.Scanner;
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();

        if(a <= b && b <= c){
            System.out.println(a + " " + b + " " + c);
        }
        else if(a <= c && c <= b){
            System.out.println(a + " " + c + " " + b);
        }
        else if(b <= a && b <= c){
            System.out.println(b + " " + a + " " + c);
        }
        else if(b <= c && c <= a){
            System.out.println(b + " " + c + " " + a);
        }
        else if(c <= a && a <= b){
            System.out.println(c + " " + a + " " + b);
        }
        else{
            System.out.println(c + " " + b + " " + a);
        }
    }
}


import java.util.Scanner;
import java.util.Arrays;

public class Main {
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();
        int b = in.nextInt();
        int c = in.nextInt();
        int[] array = {a, b, c};
        Arrays.sort(array);
        System.out.println(array[0] + " " + array[1] + " " + array[2]);
    }
}


import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        String input = new Scanner(System.in).nextLine();
        String[] inputs = input.split("\\s+");
        List<String> sorted = Arrays.stream(inputs).sorted().collect(Collectors.toList());
        System.out.println(sorted.get(0) + " " + sorted.get(1) + " " + sorted.get(2));
    }
}


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int W = scanner.nextInt();
        int H = scanner.nextInt();
        int x = scanner.nextInt();
        int y = scanner.nextInt();
        int r = scanner.nextInt();
        if(x+r<=W&&x-r>=0&&y+r<=H&&y-r>=0){System.out.println("Yes");}
        else if(x+r>W){System.out.println("No");}
        else if(y+r>H){System.out.println("No");}
        else if(x+r<0){System.out.println("No");}
        else if(y+r<0){System.out.println("No");}
        }
    }

MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Circle in a Rectangle

Write a program which reads a rectangle and a circle, and determines whether the circle is arranged inside the rectangle. As shown in the following figures, the upper right coordinate $(W, H)$ of the rectangle and the central coordinate $(x, y)$ and radius $r$ of the circle are given.

Input

Five integers $W$, $H$, $x$, $y$ and $r$ separated by a single space are given in a line.

Output

Print "Yes" if the circle is placed inside the rectangle, otherwise "No" in a line.

Constraints

$ -100 \leq x, y \leq 100$

$ 0 < W, H, r \leq 100$

Sample Input 1

5 4 2 2 1

Sample Output 1

Yes

Sample Input 2

5 4 2 4 1

Sample Output 2

No


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main{
	public static void main(String args[]) throws IOException{
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder builder = new StringBuilder();
		int i = 1;
		while(true){
			int data = Integer.parseInt(reader.readLine());
			if (data == 0)
				break;
			builder.append("Case ").append(Integer.toString(i++)).append(": ").append(Integer.toString(data)).append("\n");
		}
		System.out.print(builder);
	}
}

Print Test Cases

In the online judge system, a judge file may include multiple datasets to check whether the submitted program outputs a correct answer for each test case. This task is to practice solving a problem with multiple datasets.

Write a program which reads an integer x and print it as is. Note that multiple datasets are given for this problem.

Input

The input consists of multiple datasets. Each dataset consists of an integer x in a line.

The input ends with an integer 0. You program should not process (print) for this terminal symbol.

Output

For each dataset, print x in the following format:

Case i: x

where i is the case number which starts with 1. Put a single space between "Case" and i. Also, put a single space between ':' and x.

Constraints

1 ≤ x ≤ 10000

The number of datasets ≤ 10000

Sample Input

3
5
11
7
8
19
0

Sample Output

Case 1: 3
Case 2: 5
Case 3: 11
Case 4: 7
Case 5: 8
Case 6: 19


import java.util.*;
class Main{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int i = 1;
		while(true){
			int temp = sc.nextInt();
			if(temp == 0){
				break;
			}
			System.out.println("Case" + i++ + ": " + temp);
		}
	}
}


import java.io.*;

class Main {
    public static void main(String[] args) throws java.io.IOException {

        BufferedReader input = new BufferedReader(new InputStreamReader(
                System.in));
        StringBuilder str = new StringBuilder();

        int i = 0;

        while (true) {
            i++;
            String tmp = input.readLine();
            int x = Integer.parseInt(tmp);

            if (x == 0)
                break;

            str.append("Case " + i + ": " + x + "\n");

        }

        System.out.print(str);

    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
class Main{
	public static void main(String[] args){
	int x =0;
	Scanner scan = new Scanner(System.in);
	ArrayList<Integer> list = new ArrayList<Integer>();
	int ii = 0;
		while(ii>0){
			x = scan.nextInt();
			if(x == 0){
				break;
			}
			else{
				list.add(x);
			}
		}
		for(int i=0;i<list.size();i++){
			System.out.println("Case "+(i+1)+": "+list.get(i));
		}
	}
}


import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner stdIn = new Scanner(System.in);
    int in_num = 1;

    while(stdIn.hasNext()) {
      if (stdIn.nextInt() == 0) {
        break;
      } else {
        System.out.println("Case "+ in_num + ": " + stdIn.nextInt());
      }
      in_num++;
    }
  }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

	public static void main(String[] args) throws NumberFormatException, IOException {
		// TODO 自動生成されたメソッド・スタブ
		 InputStreamReader is = new InputStreamReader(System.in);
		 BufferedReader br = new BufferedReader(is);
		 int count = 1;
		 int input = Integer.parseInt(br.readLine());
		 while (input != 0) {
			 System.out.println("Case"+" "+count+": "+input);
			 input = Integer.parseInt(br.readLine());
			 count++;
		 }

	}

}


import java.util.Scanner;

public class Main{

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		for(int i = 1;i <= 10000;i++) {
			int x = sc.nextInt();
                        if(x == 0) {
				break;
			}
	                System.out.println("Case " + i + ": "+ x);
                }
	                        sc.close();
        }
}


import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner stdIn = new Scanner(System.in);
        StringBuilder sb = new StringBuilder();

        while(true) {
            int x = stdIn.nextInt();
            int y = stdIn.nextInt();

            if (x>y) {
                int n = x;
                x = y;
                y = n;
            } else if (x == 0 && y == 0) {
                break;
            }

            sb.append(x).append(" ").append(y).append("\n");
        }
        System.out.println(sb);
    }
}

Swapping Two Numbers

Write a program which reads two integers x and y, and prints them in ascending order.

Input

The input consists of multiple datasets. Each dataset consists of two integers x and y separated by a single space.

The input ends with two 0 (when both x and y are zero). Your program should not process for these terminal symbols.

Output

For each dataset, print x and y in ascending order in a line. Put a single space between x and y.

Constraints

0 ≤ x, y ≤ 10000

the number of datasets ≤ 3000

Sample Input

3 2
2 2
5 3
0 0

Sample Output

2 3
2 2
3 5


import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);

		while(true) {
			int x = s.nextInt();
			int y = s.nextInt();
			if(x == 0 && y == 0) {
				return;
			} else if(x <= y) {
				System.out.println(x + " " + y);
			} else {
				System.out.println(y + " " + x);
			}

		}
	}

}


import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ

		Scanner sc = new Scanner(System.in);
		int i = 0;

		while(true) {
			int x = sc.nextInt();
			int y = sc.nextInt();

			if((x == 0 && y == 0) || i > 3000) {
				break;
			}
			if(x < y) {
				System.out.println(x + " " + y);
			} else {
				System.out.println(y + " " + x);
			}
			i++;
		}
	}

}


import java.util.Scanner;

public class Main {

	public static void main(String[] args){
		Scanner a = new Scanner(System.in);
		int b = a.nextInt();
		int c = a.nextInt();
		int d = a.nextInt();
		int count = 0;
		for(int i = b;i < c;i++){
			if((d % i) == 0){
				count++;
			}
		}
		System.out.println(count);
	}
}

How Many Divisors?

Write a program which reads three integers a, b and c, and prints the number of divisors of c between a and b.

Input

Three integers a, b and c are given in a line separated by a single space.

Output

Print the number of divisors in a line.

Constraints

1 ≤ a, b, c ≤ 10000

a ≤ b

Sample Input 1

5 14 80

Sample Output 1

3


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main{

	public static void main(String[] args){

		BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));

		try{

			String str=reader.readLine();
			String[] str_Array=str.split(" ");

			int a=Integer.parseInt(str_Array[0]);
			int b=Integer.parseInt(str_Array[1]);
			int c=Integer.parseInt(str_Array[2]);
			int count=0;
			for(int i=a;i<b;i++){
				if(c%i==0)count++;
			}
			System.out.println(count);

		}catch(IOException e){
			System.out.println("??¨??????:"+e);

		}

	}

}


import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int n = sc.nextInt();
        int m = 0;
        for(int i = 1; i <= n; i++){
            if(n%i == 0 && a <= i && i <=b){
                m++;
            }
            if(b < i)break;
        }
        System.out.print(m);
        System.out.println();
    }
}


import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		int a = scan.nextInt();
		int b = scan.nextInt();
		int d,r;
		double f;
		d = a / b;
		r = a % b;
		f = (double)a / (double)b;
		String g = String.format("%.5f", f);
		System.out.println(d+" "+r+" "+g);

	}

}

A/B Problem

Write a program which reads two integers a and b, and calculates the following values:

a ÷ b: d (in integer)

remainder of a ÷ b: r (in integer)

a ÷ b: f (in real number)

Input

Two integers a and b are given in a line.

Output

Print d, r and f separated by a space in a line. For f, the output should not contain an absolute error greater than 10-5.

Constraints

1 ≤ a, b ≤ 109

Sample Input 1

3 2

Sample Output 1

1 1 1.50000


import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		long a = scanner.nextInt();
		long b = scanner.nextInt();
		scanner.close();

		System.out.println((a / b) + " " + (a % b) + " " + String.format("%.5f", (double) a / b));
	}

}


import java.util.Scanner;
 class Main{
  public static void main(String args[]){
  Scanner scan = new Scanner(System.in);
   int a = scan.nextInt();
   int b = scan.nextInt();

   int d=a/b;
   int r=a%b;
   double f=(double)a/b;

   System.out.println(d+" "+r+" "+String.format("%.5f",f));

                                     }
            }


import java.util.*;
public class Main {
	public static void main(String[] args) throws Exception{
		Scanner sc = new Scanner(System.in);
		final double pie =  3.141592653589;
		Double d = sc.nextDouble();
		System.out.println(String.format("%.6f %.6f",d*d*pie ,2*d*pie));
	}
}

Circle

Write a program which calculates the area and circumference of a circle for given radius r.

Input

A real number r is given.

Output

Print the area and circumference of the circle in a line. Put a single space between them. The output should not contain an absolute error greater than 10-5.

Constraints

0 < r < 10000

Sample Input 1

2

Sample Output 1

12.566371 12.566371

Sample Input 2

3

Sample Output 2

28.274334 18.849556


import java.util.Scanner;

public class Main {
	public static void main(String[]args){
		//????????£?????????????????????
		Scanner sc = new Scanner (System.in);
		//???????????? ??????r
		double r = sc.nextDouble();

		//???????°???
		double PI = 3.14;

		//??¢???????±??????? ??´??? ?? 3.14
		double Area = (r + r) * Math.PI;
		//?????¨????????? ?????? ?? ?????? ??3.14
		double Circle = ( r * r ) * Math.PI;

		//??¨???
		System.out.printf("%.6f %.6f", Area,Circle);
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String input = null;

		try {
			input = br.readLine();
		} catch (IOException e) {
		}
		double r = Double.parseDouble(input);
		double menseki =  r * r * Math.PI;
		double syuu =  (2 * r) * Math.PI;

		//System.out.println(menseki + " " + syuu);
		System.out.printf("%f %f\n", menseki, syuu);
	}
}


import java.util.Scanner;

class Main{
	public static void main(String[] args) {
		try(Scanner sc = new Scanner(System.in)){
			double r = sc.nextDouble();
			System.out.println(String.format("%f %f", r*r*Math.PI, 2*r*Math.PI));
		}
	}
}


import java.util.Scanner;
public class Main {
	public static void main(String[] args){
Scanner sc=new Scanner(System.in);

int f,n,sum=0;
String op;
while(true){
f=sc.nextInt();
op=sc.next();
n=sc.nextInt();
if(op.contains("+"))
	sum=f+n;
if(op.contains("-"))
	sum=f-n;
if(op.contains("*"))
	sum=f*n;
if(op.contains("/"))
	sum=f/n;

if(op.contains("?"))
	break;
System.out.println(sum);
}

}

}

Simple Calculator

Write a program which reads two integers a, b and an operator op, and then prints the value of a op b.

The operator op is '+', '-', '*' or '/' (sum, difference, product or quotient). The division should truncate any fractional part.

Input

The input consists of multiple datasets. Each dataset is given in the following format.

a op b

The input ends with a dataset where op = '?'. Your program should not process for this dataset.

Output

For each dataset, print the value in a line.

Constraints

0 ≤ a, b ≤ 20000

No divisions by zero are given.

Sample Input 1

1 + 2
56 - 18
13 * 2
100 / 10
27 + 81
0 ? 0

Sample Output 1

3
38
26
10
108


import java.io.*;
import java.util.*;

class Main {
    public static void main(String [] args){
        Scanner scan = new Scanner(System.in);

        while(true){
            int a   = scan.nextInt();
            char op = scan.next().charAt(0);
            int b   = scan.nextInt();

            if(op == '?'){
                break;
            }

            switch(op){
            case '+':
                System.out.println(a + b);
                break;

            case '-':
                System.out.println(a - b);
                break;

            case '*':
                System.out.println(a * b);
                break;

            case '/':
                System.out.println(a / b);
                break;
            }
        }
    }
}


import java.util.Scanner;
class Main{
    int n,i;
    int max = -999999;
    int min = 999999;
    int sam = 0;

    public void solve(){
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        int a[] = new int[n+1];
        for(i = 0; i < n; i++){
            a[i] = sc.nextInt();
            sam += a[i];
            if(n == 0){
                min = 0;
                max = 0;
            }else if(n == 1){
                max = a[i];
                min = a[i];
            }else if(a[i] < min){
                min = a[i];
            }else if(a[i] >= max){
                max = a[i];
            }else{
                continue;
            }

        }
    System.out.println(min + " " + max + " " + sam);
    }
    public static void main(String[] args){
        new Main().solve();
    }
}

MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Min, Max and Sum

Write a program which reads a sequence of $n$ integers $a_i (i = 1, 2, ... n)$, and prints the minimum value, maximum value and sum of the sequence.

Input

In the first line, an integer $n$ is given. In the next line, $n$ integers $a_i$ are given in a line.

Output

Print the minimum value, maximum value and sum in a line. Put a single space between the values.

Constraints

$0 < n \leq 10000$

$-1000000 \leq a_i \leq 1000000$

Sample Input

5
10 1 5 4 17

Sample Output

1 17 37


import java.util.Scanner;

class Main{
    public static void main(String[] args){
    	Scanner scan = new Scanner(System.in);
    	int n = scan.nextInt();
    	int[] values = new int[n];

        for(int i = 0; i < n; i++){
        	values[i] = scan.nextInt();
        }

        int min = values[0];
        int max = values[0];
        int sum = 0;

        for(int i = 0; i < values.length; i++){
        	if(min > values[i]){
        		min = values[i];
        	}
        	if(max < values[i]){
        		max = values[i];
        	}
        	sum += values[i];
        }
        System.out.printf("%d %d %d", min,max,sum);
    }
}


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }

        int min = a[0];
        int max = a[0];
        long sum = a[0];

        for (int i = 1; i < n; i++) {
            min = Math.min(min, a[i]);
            max = Math.max(max, a[i]);
            sum += a[i];
        }

        System.out.println(min + " " + max + " " + sum);
    }

}


import java.util.Scanner;
class Main {
	int W = 0,H = 0,I = 0,F = 0;
	public void pr() {
		Scanner sc = new Scanner(System.in);
		H = sc.nextInt();
		W = sc.nextInt();
		while(H != 0 || W != 0) {
			for(I = 0;I < H; I++) {
				for(F = 0;F < W; F++) {
					System.out.print("#");
					if(F == W-1) {
						System.out.println();
					}
				}
			}
				System.out.println();
			H = sc.nextInt();
			W = sc.nextInt();
		}
	}
	public static void main(String[] args) {
		new Main().pr();
	}
}

Print a Rectangle

Draw a rectangle which has a height of H cm and a width of W cm. Draw a 1-cm square by single '#'.

Input

The input consists of multiple datasets. Each dataset consists of two integers H and W separated by a single space.

The input ends with two 0 (when both H and W are zero).

Output

For each dataset, print the rectangle made of H × W '#'.

Print a blank line after each dataset.

Constraints

1 ≤ H ≤ 300

1 ≤ W ≤ 300

Sample Input

3 4
5 6
2 2
0 0

Sample Output

####
####
####

######
######
######
######
######

##
##


import java.io.BufferedInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main{

	public static void main(String[] args) {
		Scanner scan = new Scanner(new BufferedInputStream(System.in));
		List<String> output = new ArrayList<String>();
		while (scan.hasNext()) {
			String input = scan.nextLine();
			String[] in = input.split(" ");
			int H = Integer.parseInt(in[0]);
			int W = Integer.parseInt(in[1]);
			if (H == 0 && W == 0) {
				break;
			} else {
				if (H >= 1 && H <= 300 && W >= 1 && W <= 300) {
					for (int i = 1; i <= H; i++) {
						String result = "";
						for (int j = 1; j <= W; j++) {
							result += "#";
						}
						output.add(result);
					}
					output.add(System.lineSeparator());
				}
			}
		}
		for (String out : output) {
			System.out.println(out);
		}
	}
}


import java.util.Scanner;

public class Main
{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);

		int[] h = new int[100];
		int[] w = new int[100];

		int numData;
		for (int i = 0; ; i++)
		{
			int tempH = sc.nextInt();
			int tempW = sc.nextInt();

			if (tempH == 0 && tempW == 0)
			{
				numData = i;
				break;
			}

			h[i] = tempH;
			w[i] = tempW;
		}

		for (int i = 0; i < numData; i++)
		{
			for (int j = 0; j < h[i]; j++)
			{
				for (int k = 0; k < w[i]; k++)
				{
					System.out.print("#");
				}
				System.out.println("");
			}
			System.out.println("");
		}
	}
}


import java.util.Scanner;

public class Main{
	public static void main(String[]args){
		Scanner sc = new Scanner(System.in);

		int width, height;
		int w, h;

		height = sc.nextInt();
		width = sc.nextInt();

		while(height != 0 || width != 0){
			for(h = 0;h < height;h++){
				for(w = 0;w < width;w++){
					System.out.print("#");
				}
				System.out.println();
			}
			System.out.println();
			height = sc.nextInt();
			width = sc.nextInt();
		}
		System.out.println();

	}
}


import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    Scanner stdInt = new Scanner(System.in);

  int[][] ans = new int[300][2]; //int[][] の方にh,w代入
  int i = 0;
    while(true) {
      int h, w;
      h = stdInt.nextInt();
      w = stdInt.nextInt();
      if (h == 0 && w == 0) break;
      ans[i][0] = h;
      ans[i++][1] = w;
    }

    for (int j = 0; j < i; j++) {
      for (int k = 0; k < ans[j][0]; k++) {
        for (int l = 0; l < ans[j][1]; l++) {
          System.out.print("#");
        }
        System.out.println();
      }
      System.out.println();
    }
  }
}


import java.io.*;
public class Main{
	public static void main(String[] args)throws Exception{
		BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb=new StringBuilder();
		while(true){
		String str=input.readLine();
		String[] arr=str.split(" ");
		int h=Integer.parseInt(arr[0]);
		int w=Integer.parseInt(arr[1]);
		if(h==0&&w==0){
			break;
		}

		for(int i=0;i<h;i++){

			for(int j=0;j<w;j++){
				if(i==0||i==h-1||j==0||j==w-1){
				sb.append("#");
				}else{
					sb.append(".");
				}
			}
				sb.append("\n");
		}
			sb.append("\n");

			}

		System.out.print(sb);
	}
}

Print a Frame

Draw a frame which has a height of H cm and a width of W cm. For example, the following figure shows a frame which has a height of 6 cm and a width of 10 cm.

##########
#........#
#........#
#........#
#........#
##########

Input

The input consists of multiple datasets. Each dataset consists of two integers H and W separated by a single space.

The input ends with two 0 (when both H and W are zero).

Output

For each dataset, print the frame made of '#' and '.'.

Print a blank line after each dataset.

Constraints

3 ≤ H ≤ 300

3 ≤ W ≤ 300

Sample Input

3 4
5 6
3 3
0 0

Sample Output

####
#..#
####

######
#....#
#....#
#....#
######

###
#.#
###


import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		Scanner scan=new Scanner(System.in);

		while(true){
     		int H=scan.nextInt();
	    	int W=scan.nextInt();
		    if(H==0 && W==0)break;

			for(int i=0;i<H;i++){

				if(i==0 || i==H-1){
					for(int j=0;j<W;j++)System.out.print("#");
					System.out.println();
				}

				else {
					for(int j=0;j<W;j++){
						if(j==0 || j==W-1)System.out.print("#");
						else System.out.print(".");
					}
					System.out.println();
				}

			}
			System.out.println();
		}
	}
}


import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scanner = new Scanner(System.in);
		while (true) {
        // 入力されるH,Wを定義

        int H = scanner.nextInt();
        int W = scanner.nextInt();

        // H,Wともに0の時終了
        if (H == 0 && W == 0) {
        	break;
        }

        for (int i = 0; i < H; i++) {
        	for (int j = 0; j < W; j++) {
        		if (i == 0 || i == H - 1 || j == 0 || j == W - 1) {
        		System.out.print("#");
        		} else {
        			System.out.print(".");
        		}
        	}
        	System.out.println("");
        }
        System.out.println("");
		}
	}

}


import java.util.Scanner;
public class Main {
	    public static void main(String[] args){
	        Scanner sc=new Scanner(System.in);
	        int i=0,j=0;
	        while(true){
	            int H=sc.nextInt();
	            int W=sc.nextInt();
	            if(H==0&&W==0)break;
	            if(H==1&&W==1)System.out.println("#");
	            else for(i=0;i<H;i++){
	                for(j=0;j<W;j++){
	                	if(i%2==1){
	                    System.out.print("#.");
	                	}else System.out.print(".#");
	                }
	                System.out.print("\n");
	        }
	    }
	}

}

Print a Chessboard

Draw a chessboard which has a height of H cm and a width of W cm. For example, the following figure shows a chessboard which has a height of 6 cm and a width of 10 cm.

#.#.#.#.#.
.#.#.#.#.#
#.#.#.#.#.
.#.#.#.#.#
#.#.#.#.#.
.#.#.#.#.#

Note that the top left corner should be drawn by '#'.

Input

The input consists of multiple datasets. Each dataset consists of two integers H and W separated by a single space.

The input ends with two 0 (when both H and W are zero).

Output

For each dataset, print the chessboard made of '#' and '.'.

Print a blank line after each dataset.

Constraints

1 ≤ H ≤ 300

1 ≤ W ≤ 300

Sample Input

3 4
5 6
3 3
2 2
1 1
0 0

Sample Output

#.#.
.#.#
#.#.

#.#.#.
.#.#.#
#.#.#.
.#.#.#
#.#.#.

#.#
.#.
#.#

#.
.#

#


import java.util.Scanner;
public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    while(true){
      int h = sc.nextInt();
      int w = sc.nextInt();
      if(h == 0 && w == 0) return;
      for(int i = 0; i<h; i++) {
        for(int j = 0; j<w; j++) {
          if(i%2 == 0){
            System.out.print(j%2 == 0 ? "#" : ".");
          }else{
            System.out.print(j%2 == 0 ? "." : "#");
          }
        }
        System.out.println();
      }
      System.out.println();
    }
  }
}


import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

		while(scan.hasNext()) {
			int H = scan.nextInt(), W = scan.nextInt();
			if(H == 0 && W == 0) {
				break;
			}else {
				for(int i = 0 ; i < H ; i++) {
					for(int j = 0 ; j < W ; j++) {
						if(( i + j ) % 2 == 0) {
							System.out.print("#");
						}else {
							System.out.print(".");
						}
					}
					System.out.println();
				}
				System.out.println();
			}
		}
		scan.close();
	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) {

		try {
			InputStreamReader isr = new InputStreamReader(System.in);
			BufferedReader br = new BufferedReader(isr);
			String buf = br.readLine();
			int n = Integer.parseInt(buf);
			int i = 1;
			CHECK_NUM(i,n,0);
			System.exit(0);

		} catch (IOException e) {
			System.err.println(e);
		}
	}

	public static void CHECK_NUM(int i,int n,int x)
	{
		x=i;

		if(x % 3==0){
			System.out.print(" "+i);
			END_CHECK_NUM(i,n,x);
		}

		INCLUDE3(i,n,x);
	}

	public static void INCLUDE3(int i,int n,int x)
	{
		if ( x % 10 == 3 ){ //xを10で割ったとき余りが3の時は
			System.out.print(" "+i);
			END_CHECK_NUM(i,n,x);
		}
		x /= 10;
		//System.out.print(" x="+x);
		if ( x != 0 ) INCLUDE3(i,n,x);

		END_CHECK_NUM(i,n,x);
	}

	public static void END_CHECK_NUM(int i,int n,int x)
	{
		if ( ++i <= n )	CHECK_NUM(i,n,x);

		System.out.println();
		System.exit(0);
	}
}

Structured Programming

In programming languages like C/C++, a goto statement provides an unconditional jump from the "goto" to a labeled statement. For example, a statement "goto CHECK_NUM;" is executed, control of the program jumps to CHECK_NUM. Using these constructs, you can implement, for example, loops.

Note that use of goto statement is highly discouraged, because it is difficult to trace the control flow of a program which includes goto.

Write a program which does precisely the same thing as the following program (this example is wrtten in C++). Let's try to write the program without goto statements.

void call(int n){
int i = 1;
CHECK_NUM:
int x = i;
if ( x % 3 == 0 ){
cout << " " << i;
goto END_CHECK_NUM;
}
INCLUDE3:
if ( x % 10 == 3 ){
cout << " " << i;
goto END_CHECK_NUM;
}
x /= 10;
if ( x ) goto INCLUDE3;
END_CHECK_NUM:
if ( ++i <= n ) goto CHECK_NUM;

cout << endl;
}

Input

An integer n is given in a line.

Output

Print the output result of the above program for given integer n.

Constraints

3 ≤ n ≤ 10000

Sample Input

30

Sample Output

3 6 9 12 13 15 18 21 23 24 27 30

Put a single space character before each element.


import java.util.Scanner;
class Main {
	public void kami() {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		if(3 <= n && n <= 10000){
			for(int i = 3; i <= n; i++){
				int x = i % 3;
				int y = i % 10;
				int unko = i / 10;
				if(x == 0){
					System.out.print(" " + i);
				}else if(y == 3 || unko == 3 || unko == 30 || unko == 300){
					System.out.print(" " + i);
				}
			}
            System.out.println();
		}

	}

	public static void main(String[] args) {

		new Main().kami();
	}

}


import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner in = new Scanner(System.in);

		int num = in.nextInt();
		int count = 1;

		for (count = 1; count < num; count++) {

			if (count % 3 == 0 && count % 10 != 0) {

				// String dis_count = String.format("%5d", count);
				System.out.print(" " + count);

			}
		}
		// String dis_num = String.format("%5d", num);
		System.out.print(" " + num);
	}

}


import java.util.Scanner;

class Main {
    private static int a,in;
    public static void main(String[] args){

     Scanner s = new Scanner(System.in);

in= s.nextInt();
for(int i= 1;i<=in;i=i++)
{
    a=i;
if(a%3==0){
    System.out.print(" "+a);
    continue;

}do{
    if(a%10 == 3){
        System.out.print(" "+i);
        break;
    }
     a/=10;
}while(a>0);
}
System.out.println();
}}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

class Main {
    public static void main(String args[]) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();

        int n = Integer.parseInt(br.readLine());
        String[] str = br.readLine().split(" ");

        for (int i = n - 1; i >= 0; i--) {
            sb.append(str[i] + " ");
        }
        int a = sb.length();
        sb.deleteCharAt(a-1);
        System.out.println(sb);
    }
}

Reversing Numbers

Write a program which reads a sequence and prints it in the reverse order.

Input

The input is given in the following format:

n
a1 a2 . . . an

n is the size of the sequence and ai is the ith element of the sequence.

Output

Print the reversed sequence in a line. Print a single space character between adjacent elements (Note that your program should not put a space character after the last element).

Constraints

n ≤ 100

0 ≤ ai < 1000

Sample Input 1

5
1 2 3 4 5

Sample Output 1

5 4 3 2 1

Sample Input 2

8
3 3 4 4 5 8 7 9

Sample Output 2

9 7 8 5 4 4 3 3

Note

      解説


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[][] tow1 = new int[3][10];
        int[][] tow2 = new int[3][10];
        int[][] tow3 = new int[3][10];
        int[][] tow4 = new int[3][10];
        int b = 0, f = 0, r = 0, v = 0;
        int num = sc.nextInt();

        for (int i = 0; i < num; i++) {
            b = sc.nextInt();
            f = sc.nextInt()-1;
            r = sc.nextInt()-1;
            v = sc.nextInt();
            if (b == 1) {
                tow1[f][r] += v;
            } else if (b == 2) {
                tow2[f][r] += v;
            } else if (b == 3) {
                tow3[f][r] += v;
            } else if (b == 4) {
                tow4[f][r] += v;
            }

    }
        for (int i = 2; i <= 0; i--) {
            for (int j = 0; j < 10; j++) {
                System.out.printf("%d", tow1[i][j]);
            }
            System.out.println();
        }
        System.out.println("####################");
        for (int i = 2; i <= 0; i--) {
            for (int j = 0; j < 10; j++) {
                System.out.printf("%d", tow2[i][j]);
            }
            System.out.println();
        }
        System.out.println("####################");
        for (int i = 2; i <= 0; i--) {
            for (int j = 0; j < 10; j++) {
                System.out.printf("%d", tow3[i][j]);
            }
            System.out.println();
        }
        System.out.println("####################");
        for (int i = 2; i <= 0; i--) {
            for (int j = 0; j < 10; j++) {
                System.out.printf("%d", tow4[i][j]);
            }
            System.out.println();
        }

        sc.close();
    }
}

Official House

You manage 4 buildings, each of which has 3 floors, each of which consists of 10 rooms. Write a program which reads a sequence of tenant/leaver notices, and reports the number of tenants for each room.

For each notice, you are given four integers b, f, r and v which represent that v persons entered to room r of fth floor at building b. If v is negative, it means that −v persons left.

Assume that initially no person lives in the building.

Input

In the first line, the number of notices n is given. In the following n lines, a set of four integers b, f, r and v which represents ith notice is given in a line.

Output

For each building, print the information of 1st, 2nd and 3rd floor in this order. For each floor information, print the number of tenants of 1st, 2nd, .. and 10th room in this order. Print a single space character before the number of tenants. Print "####################" (20 '#') between buildings.

Constraints

No incorrect building, floor and room numbers are given.

0 ≤ the number of tenants during the management ≤ 9

Sample Input

3
1 1 3 8
3 2 2 7
4 3 8 1

Sample Output

0 0 8 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
####################
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
####################
0 0 0 0 0 0 0 0 0 0
0 7 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
####################
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0


import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		int[][][] ar = new int[4][3][10];
		for ( int i = 0; i < n; i++) {
			int b = scan.nextInt();
			int f = scan.nextInt();
			int r = scan.nextInt();
			int v = scan.nextInt();
			ar[b-1][f-1][r-1] += v;
		}
		for (int b = 0; b < 4; b++) {
			for (int i = 0; i < 3; i++) {
				for(int j = 0; j < 10; j++){
					if (j != 9) {
						System.out.print(ar[b][i][j] + " ");
					}else {
						System.out.print(ar[b][i][j] + "\n");
					}
				}
			}
			System.out.println("####################");
		}
	}
}


import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String line = br.readLine();
        int num = Integer.parseInt(line);
        int[][][] numAry = new int[4][3][10];

        //????????????
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 3; j++) {
                for (int k = 0; k < 10; k++) {
                    numAry[i][j][k] = 0;
                }
            }
        }

        //input?????£??\
        for (int i = 0; i < num; i ++) {
            String[] aryInput = br.readLine().split(" ");

            int a = Integer.parseInt(aryInput[0]);
            int b = Integer.parseInt(aryInput[1]);
            int c = Integer.parseInt(aryInput[2]);

            numAry[a - 1][b - 1][c - 1] = Integer.parseInt(aryInput[3]);
        }

        for(int i = 0; i < 4; i++){
            for (int j = 0; j < 3; j++) {
                for (int k = 0; k < 10; k++) {
                    System.out.print(numAry[i][j][k]);
                }
                System.out.println("");
            }
            if(i != 3){
                System.out.println("##########");
            }
        }

    }
}


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[][][] room = new int[4][3][10];
        for (int i = 0; i < n; i++) {
            int[] a = new int[]{sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt()};
            room[a[0] - 1][a[1] - 1][a[2] - 1] = a[3];
        }
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 3; j++) {
                for (int k = 0; k < 10; k++) {
                    System.out.print(" " + room[i][j][k]);
                }
                System.out.println();
            }
            if (i != 3) {
                System.out.println("####################");
            }
        }
    }
}


import java.util.Scanner;

public class Main{
    public static void main (String[] args){
        try(Scanner sc = new Scanner(System.in)){
            int n = sc.nextInt();
            int [] rooms = new int[500];
            for(int i=0;i<n;i++){
                int b = sc.nextInt();
                int f = sc.nextInt();
                int r = sc.nextInt();
                int v = sc.nextInt();
                rooms[getIndex(b,f,r)] += v;
            }
            String lineDelimiter = "";
            for(int b = 1; b<=4; b++){
                System.out.print(lineDelimiter);
                for(int f=1; f<=3; f++){
                    for(int r=1;r<=10;r++){
                        System.out.print(' ');
                        System.out.print(rooms[getIndex(b,f,r)]);
                    }
                    System.out.println();
                }
                lineDelimiter = "####################" + System.lineSeparator();
            }
        }
    }

    private static int getIndex(int b, int f, int r) {
        return(b-1)*100+(f-1)*10+(r-1);
    }
}


import java.util.*;

class Main {
        public static void main(String args[]) {
                try (Scanner sc = new Scanner(System.in)) {
                        int n = sc.nextInt(), m = sc.nextInt();
                        int[][] a = new int[n][m];
                        for (int i = 0; i < n; i++) {
                                for (int j = 0; j < m; j++) {
                                        a[i][j] = sc.nextInt();
                                }
                        }
                        int[] x = new int[m];
                        for (int i = 0; i < m; i++) {
                                x[i] = sc.nextInt();
                        }
                        for (int i = 0; i < n; i++) {
                                int ans = 0;
                                for (int j = 0; j < m; j++) {
                                        ans += x[j] * a[i][j];
                                }
                                System.out.println(ans);
                        }
                }
        }
}

MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Matrix Vector Multiplication

Write a program which reads a $ n \times m$ matrix $A$ and a $m \times 1$ vector $b$, and prints their product $Ab$.

A column vector with m elements is represented by the following equation.

\[
b = \left(
\begin{array}{c}
b_1 \\
b_2 \\
: \\
b_m \\
\end{array}
\right)
\]

A $n \times m$ matrix with $m$ column vectors, each of which consists of $n$ elements, is represented by the following equation.

\[
A = \left(
\begin{array}{cccc}
a_{11} & a_{12} & ... & a_{1m} \\
a_{21} & a_{22} & ... & a_{2m} \\
: & : & : & : \\
a_{n1} & a_{n2} & ... & a_{nm} \\
\end{array}
\right)
\]

$i$-th element of a $m \times 1$ column vector $b$ is represented by $b_i$ ($i = 1, 2, ..., m$), and the element in $i$-th row and $j$-th column of a matrix $A$ is represented by $a_{ij}$ ($i = 1, 2, ..., n,$ $j = 1, 2, ..., m$).

The product of a $n \times m$ matrix $A$ and a $m \times 1$ column vector $b$ is a $n \times 1$ column vector $c$, and $c_i$ is obtained by the following formula:

\[
c_i = \sum_{j=1}^m a_{ij}b_j = a_{i1}b_1 + a_{i2}b_2 + ... + a_{im}b_m
\]

Input

In the first line, two integers $n$ and $m$ are given. In the following $n$ lines, $a_{ij}$ are given separated by a single space character. In the next $m$ lines, $b_i$ is given in a line.

Output

The output consists of $n$ lines. Print $c_i$ in a line.

Constraints

$1 \leq n, m \leq 100$

$0 \leq b_i, a_{ij} \leq 1000$

Sample Input

3 4
1 2 0 1
0 3 0 1
4 1 1 0
1
2
3
0

Sample Output

5
6
9


import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);

		int n=sc.nextInt();
		int x=sc.nextInt();
		int p1,p2,p3;
		int cnt=0;
		while(n!=0 || x!=0){
			for(p1=1;p1<=n;p1++){
				for(p2=(p1+1);p2<=n;p2++){
					if(((p1+p2)>=x) ||(p1==p2)) break;
					for(p3=(p2+1);p3<=n;p3++){
						if(((p1+p2+p3)>x)||(p2==p3)){
							break;
						}else if((p1+p2+p3)==x){
							cnt++;
						}
					}//for
				}//for
			}//for
			System.out.println(cnt);
			cnt=0;
			n=sc.nextInt();
			x=sc.nextInt();
		}//while
	}
}

How many ways?

Write a program which identifies the number of combinations of three integers which satisfy the following conditions:

You should select three distinct integers from 1 to n.

A total sum of the three integers is x.

For example, there are two combinations for n = 5 and x = 9.

1 + 3 + 5 = 9

2 + 3 + 4 = 9

Input

The input consists of multiple datasets. For each dataset, two integers n and x are given in a line.

The input ends with two zeros for n and x respectively. Your program should not process for these terminal symbols.

Constraints

3 ≤ n ≤ 100

0 ≤ x ≤ 300

Output

For each dataset, print the number of combinations in a line.

Sample Input

5 9
0 0

Sample Output

2

Note

      解説


import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		StringBuilder output = new StringBuilder();
		int r = scan.nextInt();
		int c = scan.nextInt();
		int[][] sheet = new int[r+1][c+1];

		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				sheet[i][j] = scan.nextInt();
			}
		}

		// 表の端に合計を記録
		int sum_r = 0;
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				sum_r += sheet[i][j];
			}
			sheet[i][c] = sum_r;
			sum_r = 0;
		}
		int sum_c = 0;
		for (int j = 0; j < c; j++) {
			for (int i = 0; i < r; i++) {
				sum_c += sheet[i][j];
			}
			sheet[r][j] = sum_c;
			sum_c = 0;
		}
		int sum_all = 0;
		for (int i = 0; i < r; i++) {
			sum_all += sheet[i][c];
		}
		sheet[r][c] = sum_all;

		// 出力
		for (int i = 0; i < r+1; i++) {
			for (int j = 0; j < c+1; j++) {
				output.append(" ").append(sheet[i][j]);
			}
			output.append('\n');
		}
		System.out.print(output);
		scan.close();
	}
}

Spreadsheet

Your task is to perform a simple table calculation.

Write a program which reads the number of rows r, columns c and a table of r × c elements, and prints a new table, which includes the total sum for each row and column.

Input

In the first line, two integers r and c are given. Next, the table is given by r lines, each of which consists of c integers separated by space characters.

Output

Print the new table of (r+1) × (c+1) elements. Put a single space character between adjacent elements. For each row, print the sum of it's elements in the last column. For each column, print the sum of it's elements in the last row. Print the total sum of the elements at the bottom right corner of the table.

Constraints

1 ≤ r, c ≤ 100

0 ≤ an element of the table ≤ 100

Sample Input

4 5
1 1 3 4 5
2 2 2 4 5
3 3 0 1 1
2 3 4 4 6

Sample Output

1 1 3 4 5 14
2 2 2 4 5 15
3 3 0 1 1 8
2 3 4 4 6 19
8 9 9 13 17 56


import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int r,c;
		int[][] A = new int[100][100];

		r = sc.nextInt();
		c = sc.nextInt();

		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++){
				A[i][j] = sc.nextInt();
			}
		}
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++){
				A[i][c] += A[i][j];

			}
		}
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++){
				A[r][j] += A[i][j];
			}

		}
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++){
				A[r][c] += A[i][j];
			}
		}

		for (int i = 0; i <= r; i++) {

			for (int j = 0; j <= c; j++){
				System.out.print(A[i][j] + " ");
			}
			System.out.println();

		}

	}

}


import java.io.*;

class Main{
	public static void main(String[] args)throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String str = br.readLine();
		char[] cr = str.toCharArray();
		for(int i=0;i<cr.length;i++){
			if(Character.isLowerCase(cr[i])){
				System.out.print(Character.toUpperCase(cr[i]));
			}else{
				System.out.print(Character.toLowerCase(cr[i]));
			}
		}
		System.out.println();
	}
}

Toggling Cases

Write a program which converts uppercase/lowercase letters to lowercase/uppercase for a given string.

Input

A string is given in a line.

Output

Print the converted string in a line. Note that you do not need to convert any characters other than alphabetical letters.

Constraints

The length of the input string < 1200

Sample Input

fAIR, LATER, OCCASIONALLY CLOUDY.

Sample Output

Fair, later, occasionally cloudy.


import java.util.*;

public class Main {
    static public void main(String[] args){
       Scanner sc = new Scanner(System.in);
        while(true){
            String s = sc.next();
            if(s.equals("0")) break;
            int sum = 0;
            for(int i = 0; i<s.length(); i++){
                sum += Integer.parseInt(s.substring(i, i+1));
            }
            System.out.println(sum);
        }
        sc.close();
    }
}

Sum of Numbers

Write a program which reads an integer and prints sum of its digits.

Input

The input consists of multiple datasets. For each dataset, an integer x is given in a line. The number of digits in x does not exceed 1000.

The input ends with a line including single zero. Your program should not process for this terminal symbol.

Output

For each dataset, print the sum of digits in x.

Sample Input

123
55
1000
0

Sample Output

6
10
1


import java.util.*;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String line = sc.nextLine();
        char[] c = line.toCharArray();
        int l = c.length;

        int[] n = new int[26];
        for(int i = 0; i < l; i++) {
          switch(c[i]) {
            case 'a':
            case 'A':
              n[0] += 1;
              break;
            case 'b':
            case 'B':
              n[1] += 1;
              break;
            case 'c':
            case 'C':
              n[2] += 1;
              break;
            case 'd':
            case 'D':
              n[3] += 1;
              break;
            case 'e':
            case 'E':
              n[4] += 1;
              break;
            case 'f':
            case 'F':
              n[5] += 1;
              break;
            case 'g':
            case 'G':
              n[6] += 1;
              break;
            case 'h':
            case 'H':
              n[7] += 1;
              break;
            case 'i':
            case 'I':
              n[8] += 1;
              break;
            case 'j':
            case 'J':
              n[9] += 1;
              break;
            case 'k':
            case 'K':
              n[10] += 1;
              break;
            case 'l':
            case 'L':
              n[11] += 1;
              break;
            case 'm':
            case 'M':
              n[12] += 1;
              break;
            case 'n':
            case 'N':
              n[13] += 1;
              break;
            case 'o':
            case 'O':
              n[14] += 1;
              break;
            case 'p':
            case 'P':
              n[15] += 1;
              break;
            case 'q':
            case 'Q':
              n[16] += 1;
              break;
            case 'r':
            case 'R':
              n[17] += 1;
              break;
            case 's':
            case 'S':
              n[18] += 1;
              break;
            case 't':
            case 'T':
              n[19] += 1;
              break;
            case 'u':
            case 'U':
              n[20] += 1;
              break;
            case 'v':
            case 'V':
              n[21] += 1;
              break;
            case 'w':
            case 'W':
              n[22] += 1;
              break;
            case 'x':
            case 'X':
              n[23] += 1;
              break;
            case 'y':
            case 'Y':
              n[24] += 1;
              break;
            case 'z':
            case 'Z':
              n[25] += 1;
              break;
          }
        }
        System.out.println("a : " + n[0]);
        System.out.println("b : " + n[1]);
        System.out.println("c : " + n[2]);
        System.out.println("d : " + n[3]);
        System.out.println("e : " + n[4]);
        System.out.println("f : " + n[5]);
        System.out.println("g : " + n[6]);
        System.out.println("h : " + n[7]);
        System.out.println("i : " + n[8]);
        System.out.println("j : " + n[9]);
        System.out.println("k : " + n[10]);
        System.out.println("l : " + n[11]);
        System.out.println("m : " + n[12]);
        System.out.println("n : " + n[13]);
        System.out.println("o : " + n[14]);
        System.out.println("p : " + n[15]);
        System.out.println("q : " + n[16]);
        System.out.println("r : " + n[17]);
        System.out.println("s : " + n[18]);
        System.out.println("t : " + n[19]);
        System.out.println("u : " + n[20]);
        System.out.println("v : " + n[21]);
        System.out.println("w : " + n[22]);
        System.out.println("x : " + n[23]);
        System.out.println("y : " + n[24]);
        System.out.println("z : " + n[25]);
    }
}

Counting Characters

Write a program which counts and reports the number of each alphabetical letter. Ignore the case of characters.

Input

A sentence in English is given in several lines.

Output

Prints the number of alphabetical letters in the following format:

a : The number of 'a'
b : The number of 'b'
c : The number of 'c'
.
.
z : The number of 'z'

Constraints

The number of characters in the sentence < 1200

Sample Input

This is a pen.

Sample Output

a : 1
b : 0
c : 0
d : 0
e : 1
f : 0
g : 0
h : 1
i : 2
j : 0
k : 0
l : 0
m : 0
n : 1
o : 0
p : 1
q : 0
r : 0
s : 2
t : 1
u : 0
v : 0
w : 0
x : 0
y : 0
z : 0


import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String str;
        String[] alfa = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
        int[] count = new int[26];
        str = sc.nextLine();
        String[] s = new String[str.length()];
        for(int i=0;i<str.length();i++){
            s[i] = String.valueOf(str.charAt(i));
        }
        for(int i=0;i<str.length();i++){
            for(int j=0;j<26;j++){
                if(s[i].equalsIgnoreCase(alfa[j])) count[j]++;
            }
        }
        for(int i=0;i<26;i++){
            System.out.println(alfa[i] + " : " +count[i]);
        }
    }
}


import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        String s=in.nextLine();
        String s1=s+s;
        String check=in.nextLine();
        if(s1.contains(check)){
            System.out.println("Yes");
        }else{
            System.out.println("No");
        }
    }
}

MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Ring

Write a program which finds a pattern $p$ in a ring shaped text $s$.

Input

In the first line, the text $s$ is given.

In the second line, the pattern $p$ is given.

Output

If $p$ is in $s$, print Yes in a line, otherwise No.

Constraints

$1 \leq $ length of $p \leq $ length of $s \leq 100$

$s$ and $p$ consists of lower-case letters

Sample Input 1

vanceknowledgetoad
advance

Sample Output 1

Yes

Sample Input 2

vanceknowledgetoad
advanced

Sample Output 2

No


import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String W = sc.next();
		int ct = 0;
		while (true) {
			String T = sc.next();
			if (T.equals("END_OF_TEXT")) {
				break;
			}
			if (T.equalsIgnoreCase(W)) {
				ct++;
			}
		}
		System.out.println(ct);
	}
}

Finding a Word

Write a program which reads a word W and a text T, and prints the number of word W which appears in text T

T consists of string Ti separated by space characters and newlines. Count the number of Ti which equals to W. The word and text are case insensitive.

Constraints

The length of W ≤ 10

W consists of lower case letters

The length of T in a line ≤ 1000

Input

In the first line, the word W is given. In the following lines, the text T is given separated by space characters and newlines.

"END_OF_TEXT" indicates the end of the text.

Output

Print the number of W in the text.

Sample Input

computer
Nurtures computer scientists and highly-skilled computer engineers
who will create and exploit "knowledge" for the new era.
Provides an outstanding computer environment.
END_OF_TEXT

Sample Output

3


import java.io.IOException;
import java.util.Scanner;
class Main{
	public static void main(String[] args)throws NumberFormatException, IOException{
		Scanner scan = new Scanner(System.in);
		String answer = "";
		String temp = "";
		while(true){
			temp = scan.next();
			if(temp.equals("-")){
				break;
			}
			char[] targetChar = temp.toCharArray();
			int targetLength = temp.length();
			int shuffleTimes = scan.nextInt();
			int[] shuffleBegin = new int[shuffleTimes];

			for(int i =0;i<shuffleTimes; i++){
				shuffleBegin[i] = scan.nextInt();
			answer = "";
				for(int j=0; j<(targetLength - shuffleBegin[i]) ; j++){
					answer = answer + targetChar[j + shuffleBegin[i]];
				}

				for(int j=0; j<shuffleBegin[i]; j++){
					answer = answer + targetChar[j];
				}

			targetChar = answer.toCharArray();
			}
			System.out.println(answer);
		}
	}
}

Shuffle

Your task is to shuffle a deck of n cards, each of which is marked by a alphabetical letter.

A single shuffle action takes out h cards from the bottom of the deck and moves them to the top of the deck.

The deck of cards is represented by a string as follows.

abcdeefab

The first character and the last character correspond to the card located at the bottom of the deck and the card on the top of the deck respectively.

For example, a shuffle with h = 4 to the above deck, moves the first 4 characters "abcd" to the end of the remaining characters "eefab", and generates the following deck:

eefababcd

You can repeat such shuffle operations.

Write a program which reads a deck (a string) and a sequence of h, and prints the final state (a string).

Input

The input consists of multiple datasets. Each dataset is given in the following format:

A string which represents a deck
The number of shuffle m
h1
h2
.
.
hm

The input ends with a single character '-' for the string.

Constraints

The length of the string ≤ 200

1 ≤ m ≤ 100

1 ≤ hi < The length of the string

The number of datasets ≤ 10

Output

For each dataset, print a string which represents the final state in a line.

Sample Input

aabc
3
1
2
1
vwxyz
2
3
4
-

Sample Output

aabc
xyzvw


import java.util.Scanner;
public class Main {

    static char[] k = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};

    public static void main(String[] args) {
        Scanner stdIn = new Scanner(System.in);
        while (true) {
            String W = stdIn.next();
            if(W.equals("-")) {
                break;
            }
            int m = stdIn.nextInt();
            for(int i = 0; i < m; i++) {
                int h = stdIn.nextInt();
                String s1 = W.substring(0, h);
                String s2 = W.substring(h);
                W = s2 + s1;
            }
            System.out.println(W);
        }
    }
}


import java.util.Scanner;
public class Main{
    public static void main(String[] args){
	Scanner scan = new Scanner(System.in);
	String n = scan.nextLine();
	int cnt = Integer.valueOf(n);
	int taro = 0;
	int hanako = 0;
	for (int i = 0; i < cnt; i++) {
	    String fuda = scan.nextLine();
	    String[] cards = fuda.split(" ");
	    if (cards[0].compareTo(cards[1]) == 0) {
		taro += 1;
		hanako += 1;
	    } else if (cards[0].compareTo(cards[1]) < 0) {
		hanako += 3;
	    } else if (cards[0].compareTo(cards[1]) > 0) {
		taro += 3;
	    }
	}
	System.out.println(taro + " " + hanako);
    }
}

Card Game

Taro and Hanako are playing card games. They have n cards each, and they compete n turns. At each turn Taro and Hanako respectively puts out a card.
The name of the animal consisting of alphabetical letters is written on each card, and the bigger one in lexicographical order becomes the winner of that turn. The winner obtains 3 points. In the case of a draw, they obtain 1 point each.

Write a program which reads a sequence of cards Taro and Hanako have and reports the final scores of the game.

Input

In the first line, the number of cards n is given. In the following n lines, the cards for n turns are given respectively. For each line, the first string represents the Taro's card and the second one represents Hanako's card.

Constraints

n ≤ 1000

The length of the string ≤ 100

Output

Print the final scores of Taro and Hanako respectively. Put a single space character between them.

Sample Input

3
cat dog
fish fish
lion tiger

Sample Output

1 7


import java.util.*;
import java.io.*;
public class Main {

	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		mylist ml=new mylist();
		int q=in.nextInt();
		for(int i=0;i<q;i++) {
			int query=Integer.parseInt(in.next());
			switch(query) {
			case 0:
				int x=Integer.parseInt(in.next());
				ml.insert(x);
				break;

			case 1:
				int d=Integer.parseInt(in.next());
				ml.move(d);
				break;

			case 2:
				ml.delete();
				break;
			}
		}
		ml.printls();
	}

}

class node{
	node next,prev;
	int n;
	boolean END=false;
}

class mylist{
	node END=new node();
	node currentNode;
	int position=0,size=0;

	mylist(){
		END=new node();
		END.END=true;
		END.next=END;
		END.prev=END;
		currentNode=END;
	}

	void insert(int x) {
		node nn=new node();
		nn.n=x;
		nn.next=currentNode;
		nn.prev=currentNode.prev;
		nn.prev.next=nn;
		nn.next.prev=nn;
		currentNode=nn;
		size++;
	}

	void move(int m) {
		for(int i=0;i<Math.abs(m);i++) {
			if(m<0)currentNode=currentNode.prev;
			else currentNode=currentNode.next;
		}
	}

	void delete() {
		currentNode.prev.next=currentNode.next;
		currentNode.next.prev=currentNode.prev;
		currentNode=currentNode.next;
		size--;
	}

	void printls() {
		node cn=END.next;
		for(int i=0;i<size;i++) {
			System.out.println(cn.n);
			cn=cn.next;
		}
	}
}

List

For a dynamic list $L$ of integers, perform a sequence of the following operations. $L$ has a special element called END at the end of the list and an element of $L$ is indicated by a cursor.

insert($x$): Insert $x$ before the element indicated by the cursor. After this operation, the cursor points the inserted element.

move($d$): Move the cursor to the end by $d$, if $d$ is positive. Move the cursor to the front by $d$, if $d$ is negative.

erase(): Delete the element indicated by the cursor. After this operation, the cursor points the element next to the deleted element. In case there is no such element, the cursor should point END.

In the initial state, $L$ is empty and the cursor points END.

Input

The input is given in the following format.

$q$
$query_1$
$query_2$
:
$query_q$

Each query $query_i$ is given by

0 $x$

or

1 $d$

or

2

where the first digits 0, 1 and 2 represent insert, move and erase operations respectively.

Output

Print all elements of the list in order after performing given operations. Print an element in a line.

Constraints

$1 \leq q \leq 500,000$

The cursor indicates an element of $L$ or END during the operations

Erase operation will not given when the cursor points END

$-1,000,000,000 \leq x \leq 1,000,000,000$

Moving distance of the cursor ($\sum{|d|}$) does not exceed 1,000,000

$L$ is not empty after performing all operations

Sample Input 1

5
0 1
0 2
0 3
1 1
2

Sample Output 1

3
1


import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		int q = sc.nextInt();
		Map<String, Integer> map = new HashMap<>();

		for(int i = 0; i < q; i++) {
			int query = sc.nextInt();
			String key = sc.next();

			if(query == 0) {  //insert
				int x = sc.nextInt();
				map.put(key, x);
			}
			else if(query == 1){  //get
				System.out.println(map.getOrDefault(key, 0));
			}
			else map.remove(key);
		}
	}

}

Map: Delete

For a dictionary $M$ that stores elements formed by a pair of a string key and an integer value, perform a sequence of the following operations. Note that each key in $M$ must be unique.

insert($key$, $x$): Insert an element formed by a pair of $key$ and $x$ to $M$.

get($key$): Print the value with the specified $key$. Print 0 if there is no such element.

delete($key$): Delete the element with the specified $key$.

Input

The input is given in the following format.

$q$
$query_1$
$query_2$
:
$query_q$

Each query $query_i$ is given by

0 $key$ $x$

or

1 $key$

or

2 $key$

where the first digits 0, 1 and 2 represent insert, get and delete operations respectively.

Output

For each get operation, print an integer in a line.

Constraints

$1 \leq q \leq 200,000$

$1 \leq x \leq 1,000,000,000$

$1 \leq $ length of $key$ $ \leq 20$

$key$ consits of lower case letters

Sample Input 1

8
0 blue 4
0 red 1
0 white 5
1 red
1 blue
2 red
1 black
1 red

Sample Output 1

1
4
0
0


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

public class Main {

    static BufferedReader in;
    static PrintWriter out;
    static StringTokenizer tok;

    void solve() throws IOException {
        int n = ni();

        HashMap<Integer, Integer> map = primeFactorize(n);

        out.print(n + ":");
        Set<Integer> set = map.keySet();
        for (int x : set) {
            for (int i = 0; i < map.get(x); i++) {
                out.print(" " + x);
            }
        }
        out.println();
    }

    HashMap<Integer, Integer> primeFactorize(int n) {
        Prime p = new Prime(n + 1);

        HashMap<Integer, Integer> ret = new HashMap<>();
        int idx = 0;
        while (n > 1) {
            int x = p.get(idx);
            if (n % x == 0) {
                int cnt = 0;
                while (n % x == 0) {
                    cnt++;
                    n /= x;
                }
                ret.put(x, cnt);
            }
            idx++;
        }

        return ret;
    }

    class Prime {
        int primeMax;
        int pidx = 0;
        int[] prime, divp;

        Prime(int primeMax) {
            this.primeMax = primeMax;
            prime = new int[primeMax];
            divp = new int[primeMax];
            eratosthenes();
        }

        void eratosthenes() {
            for (int i = 2; i < primeMax; i++) {
                if (divp[i] == 0) {
                    prime[pidx++] = i;
                    for (long j = (long) i * i; j < primeMax; j += i) {
                        divp[(int) j] = i;
                    }
                }
            }
        }

        int get(int idx) {
            return prime[idx];
        }
    }

    String ns() throws IOException {
        while (!tok.hasMoreTokens()) {
            tok = new StringTokenizer(in.readLine(), " ");
        }
        return tok.nextToken();
    }

    int ni() throws IOException {
        return Integer.parseInt(ns());
    }

    long nl() throws IOException {
        return Long.parseLong(ns());
    }

    double nd() throws IOException {
        return Double.parseDouble(ns());
    }

    String[] nsa(int n) throws IOException {
        String[] res = new String[n];
        for (int i = 0; i < n; i++) {
            res[i] = ns();
        }
        return res;
    }

    int[] nia(int n) throws IOException {
        int[] res = new int[n];
        for (int i = 0; i < n; i++) {
            res[i] = ni();
        }
        return res;
    }

    long[] nla(int n) throws IOException {
        long[] res = new long[n];
        for (int i = 0; i < n; i++) {
            res[i] = nl();
        }
        return res;
    }

    public static void main(String[] args) throws IOException {
        in = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        tok = new StringTokenizer("");
        Main main = new Main();
        main.solve();
        out.close();
    }
}

Prime Factorization

Factorize a given integer n.

Input

n

An integer n is given in a line.

Output

Print the given integer n and :. Then, print prime factors in ascending order. If n is divisible by a prime factor several times, the prime factor should be printed according to the number of times. Print a space before each prime factor.

Constraints

2 ≤ n ≤ 109

Sample Input 1

12

Sample Output 1

12: 2 2 3

Sample Input 2

126

Sample Output 2

126: 2 3 3 7