Tutte le sfide
Facile 4 risolte 80.0% di accettazione

Point Quadrant

Find which quadrant a point lies in.

Cosa imparerai

  • Check the signs of x and y
  • Handle points on the axes
  • Combine two conditions

Problema

Mostra traduzione

The x and y axes split the plane into four quadrants: Q1 (x > 0, y > 0), Q2 (x < 0, y > 0), Q3 (x < 0, y < 0) and Q4 (x > 0, y < 0). Points on an axis belong to no quadrant.

Implement point_quadrant(x, y).

Input

x: The x coordinate. y: The y coordinate.

Output

Return "Q1", "Q2", "Q3" or "Q4"; "X-axis" or "Y-axis" for points on an axis; or "Origin" for (0, 0).

Vincoli

-1,000,000 ≤ x, y ≤ 1,000,000

Esempi

Esempio 1
Input:
x = -3
y = 4

Output atteso:
expected = "Q2"
Input:
long long x = -3;
long long y = 4;

Output atteso:
std::string expected = "Q2";
Input:
long x = -3;
long y = 4;

Output atteso:
string expected = "Q2";

Spiegazione

x is negative and y is positive, so the point is in Q2.

Esempio 2
Input:
x = 5
y = 0

Output atteso:
expected = "X-axis"
Input:
long long x = 5;
long long y = 0;

Output atteso:
std::string expected = "X-axis";
Input:
long x = 5;
long y = 0;

Output atteso:
string expected = "X-axis";

Spiegazione

y is 0, so the point lies on the X-axis.

Come affrontarlo

Prova prima da solo, poi apri un suggerimento alla volta.

Suggerimento 1

First determine whether the point lies on an axis or at the origin.

Suggerimento 2

If it is not on an axis, the signs of x and y tell you the quadrant.

Suggerimento 3

The origin is a special case, and axis points are not in any quadrant.

Complessità attesa

O(1) time, O(1) extra space

Riferimenti

Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.

La migliore soluzione superata di ogni programmatore. Le più veloci, leggere e meglio valutate stanno in alto.

# Programmatore Linguaggio Tempo Memoria Voto dell'IA Soluzione
1 ДЗДавид Зайдулин Python 968 ms - 5/5 Vedi il codice
2 HJhettory Jurash Python 976 ms - 2/5 Vedi il codice
3 LMLaura Mahlow Python 1,003 ms - 5/5 Vedi il codice
4 PKPornchai Kaewkrabil Go 1,354 ms - 4/5 Vedi il codice

Impara dagli altri

Soluzioni della community

Attenzione spoiler: 4 soluzioni complete sotto Prova prima a risolverla da solo. Puoi comunque sbirciare il codice degli altri sviluppatori e le revisioni dell'IA. Accedi e risolvi la sfida per partecipare alla discussione.