Shortening code
This commit is contained in:
parent
c210d9497b
commit
6fa9464ad6
76
luku01.tex
76
luku01.tex
|
@ -402,7 +402,7 @@ as follows ($\varepsilon=10^{-9}$):
|
||||||
|
|
||||||
\begin{lstlisting}
|
\begin{lstlisting}
|
||||||
if (abs(a-b) < 1e-9) {
|
if (abs(a-b) < 1e-9) {
|
||||||
// a ja b ovat yhtä suuret
|
// a and b are equal
|
||||||
}
|
}
|
||||||
\end{lstlisting}
|
\end{lstlisting}
|
||||||
|
|
||||||
|
@ -413,91 +413,91 @@ For example, using \texttt{double},
|
||||||
it is possible to accurately represent all
|
it is possible to accurately represent all
|
||||||
integers having absolute value at most $2^{53}$.
|
integers having absolute value at most $2^{53}$.
|
||||||
|
|
||||||
\section{Koodin lyhentäminen}
|
\section{Shortening code}
|
||||||
|
|
||||||
Kisakoodauksessa ihanteena on lyhyt koodi,
|
Short code is ideal in competitive programming,
|
||||||
koska algoritmi täytyy pystyä toteuttamaan
|
because the algorithm should be implemented
|
||||||
mahdollisimman nopeasti.
|
as fast as possible.
|
||||||
Monet kisakoodarit käyttävätkin lyhennysmerkintöjä
|
Because of this, competitive programmers often define
|
||||||
tietotyypeille ja muille koodin osille.
|
shorter names for datatypes and other parts of code.
|
||||||
|
|
||||||
\subsubsection{Tyyppinimet}
|
\subsubsection{Type names}
|
||||||
\index{tuppdef@\texttt{typedef}}
|
\index{tuppdef@\texttt{typedef}}
|
||||||
Komennolla \texttt{typedef} voi antaa lyhyemmän
|
Using the command \texttt{typedef}
|
||||||
nimen tietotyypille.
|
it is possible to give a shorter name
|
||||||
Esimerkiksi nimi \texttt{long long} on pitkä,
|
to a datatype.
|
||||||
joten tyypille voi antaa lyhyemmän nimen \texttt{ll}:
|
For example, the name \texttt{long long} is long,
|
||||||
|
so we can define a shorter name \texttt{ll}:
|
||||||
\begin{lstlisting}
|
\begin{lstlisting}
|
||||||
typedef long long ll;
|
typedef long long ll;
|
||||||
\end{lstlisting}
|
\end{lstlisting}
|
||||||
Tämän jälkeen koodin
|
After this, the code
|
||||||
\begin{lstlisting}
|
\begin{lstlisting}
|
||||||
long long a = 123456789;
|
long long a = 123456789;
|
||||||
long long b = 987654321;
|
long long b = 987654321;
|
||||||
cout << a*b << "\n";
|
cout << a*b << "\n";
|
||||||
\end{lstlisting}
|
\end{lstlisting}
|
||||||
voi lyhentää seuraavasti:
|
can be shortened as follows:
|
||||||
\begin{lstlisting}
|
\begin{lstlisting}
|
||||||
ll a = 123456789;
|
ll a = 123456789;
|
||||||
ll b = 987654321;
|
ll b = 987654321;
|
||||||
cout << a*b << "\n";
|
cout << a*b << "\n";
|
||||||
\end{lstlisting}
|
\end{lstlisting}
|
||||||
|
|
||||||
Komentoa \texttt{typedef} voi käyttää myös
|
The command \texttt{typedef}
|
||||||
monimutkaisempien tyyppien kanssa.
|
can also be used with more complex types.
|
||||||
Esimerkiksi seuraava koodi antaa nimen \texttt{vi}
|
For example, the following code gives
|
||||||
kokonaisluvuista muodostuvalle vektorille
|
the name \texttt{vi} for a vector of integers,
|
||||||
sekä nimen \texttt{pi} kaksi
|
and the name \texttt{pi} for a pair
|
||||||
kokonaislukua sisältävälle parille.
|
that contains two integers.
|
||||||
\begin{lstlisting}
|
\begin{lstlisting}
|
||||||
typedef vector<int> vi;
|
typedef vector<int> vi;
|
||||||
typedef pair<int,int> pi;
|
typedef pair<int,int> pi;
|
||||||
\end{lstlisting}
|
\end{lstlisting}
|
||||||
|
|
||||||
\subsubsection{Makrot}
|
\subsubsection{Macros}
|
||||||
\index{makro}
|
\index{macro}
|
||||||
Toinen tapa lyhentää koodia on määritellä \key{makroja}.
|
Another way to shorten the code is to define
|
||||||
Makro ilmaisee, että tietyt koodissa olevat
|
\key{macros}.
|
||||||
merkkijonot korvataan toisilla ennen koodin
|
A macro means that certain strings in
|
||||||
kääntämistä.
|
the code will be changed before the compilation.
|
||||||
C++:ssa makro määritellään
|
In C++, macros are defined using the
|
||||||
esikääntäjän komennolla \texttt{\#define}.
|
command \texttt{\#define}.
|
||||||
|
|
||||||
Määritellään esimerkiksi seuraavat makrot:
|
For example, we can define the following macros:
|
||||||
\begin{lstlisting}
|
\begin{lstlisting}
|
||||||
#define F first
|
#define F first
|
||||||
#define S second
|
#define S second
|
||||||
#define PB push_back
|
#define PB push_back
|
||||||
#define MP make_pair
|
#define MP make_pair
|
||||||
\end{lstlisting}
|
\end{lstlisting}
|
||||||
Tämän jälkeen koodin
|
After this, the code
|
||||||
\begin{lstlisting}
|
\begin{lstlisting}
|
||||||
v.push_back(make_pair(y1,x1));
|
v.push_back(make_pair(y1,x1));
|
||||||
v.push_back(make_pair(y2,x2));
|
v.push_back(make_pair(y2,x2));
|
||||||
int d = v[i].first+v[i].second;
|
int d = v[i].first+v[i].second;
|
||||||
\end{lstlisting}
|
\end{lstlisting}
|
||||||
voi kirjoittaa lyhyemmin seuraavasti:
|
can be shortened as follows:
|
||||||
\begin{lstlisting}
|
\begin{lstlisting}
|
||||||
v.PB(MP(y1,x1));
|
v.PB(MP(y1,x1));
|
||||||
v.PB(MP(y2,x2));
|
v.PB(MP(y2,x2));
|
||||||
int d = v[i].F+v[i].S;
|
int d = v[i].F+v[i].S;
|
||||||
\end{lstlisting}
|
\end{lstlisting}
|
||||||
|
|
||||||
Makro on mahdollista määritellä myös niin,
|
It is also possible to define a macro with parameters
|
||||||
että sille voi antaa parametreja.
|
which makes it possible to shorten loops and other
|
||||||
Tämän ansiosta makrolla voi lyhentää esimerkiksi
|
structures in the code.
|
||||||
komentorakenteita.
|
For example, we can define the following macro:
|
||||||
Määritellään esimerkiksi seuraava makro:
|
|
||||||
\begin{lstlisting}
|
\begin{lstlisting}
|
||||||
#define REP(i,a,b) for (int i = a; i <= b; i++)
|
#define REP(i,a,b) for (int i = a; i <= b; i++)
|
||||||
\end{lstlisting}
|
\end{lstlisting}
|
||||||
Tämän jälkeen koodin
|
After this, the code
|
||||||
\begin{lstlisting}
|
\begin{lstlisting}
|
||||||
for (int i = 1; i <= n; i++) {
|
for (int i = 1; i <= n; i++) {
|
||||||
haku(i);
|
haku(i);
|
||||||
}
|
}
|
||||||
\end{lstlisting}
|
\end{lstlisting}
|
||||||
voi lyhentää seuraavasti:
|
can be shortened as follows:
|
||||||
\begin{lstlisting}
|
\begin{lstlisting}
|
||||||
REP(i,1,n) {
|
REP(i,1,n) {
|
||||||
haku(i);
|
haku(i);
|
||||||
|
|
Loading…
Reference in New Issue