This repository has been archived on 2022-06-22. You can view files and clone it, but cannot push or open issues or pull requests.
usaco-guide/content/1_Intro/Data_Types.mdx

27 lines
2.7 KiB
Text
Raw Normal View History

2020-06-16 00:11:54 +00:00
---
2020-06-16 01:06:49 +00:00
id: data-types
2020-06-16 00:11:54 +00:00
title: Data Types
author: Darren Yao
2020-06-18 17:08:18 +00:00
description: Learn about the basic data types needed for competitive programming.
2020-06-16 00:11:54 +00:00
---
2020-06-29 01:09:52 +00:00
<resources>
<resource source="IUSACO" title="2.2 - Data Types">module is based off this</resource>
2020-06-29 01:09:52 +00:00
<resource source="CPH" title="1.1 to 1.3 - Introduction" starred></resource>
<resource source="PAPS" title="2.3 - Variables & Types"></resource>
</resources>
2020-06-23 01:00:35 +00:00
<br />
2020-06-23 01:00:35 +00:00
There are several main **data types** that are used in contests: 64-bit integers, floating point numbers, booleans, characters, and strings. Assuming that you are familiar with the language you are using, this should be mostly review.
2020-06-22 03:32:32 +00:00
It's a good idea to use 64-bit integers (`long long` in C++) instead of 32-bit integers (`int`). 64-bit integers are less likely to have overflow issues, since they can store any number between $-9\,223\,372\,036\,854\,775\,808$ and $9\,223\,372\,036\,854\,775\,807$ which is roughly equal to $\pm 9 \times 10^{18}$. Contest problems are usually set such that the 64-bit integer is sufficient. Of course, you shouldn't do this when time and/or memory limits are tight, which may be the case in higher divisions of USACO.
2020-06-17 22:18:07 +00:00
**Floating point numbers** are used to store decimal values. It is important to know that floating point numbers are not exact, because the binary architecture of computers can only store decimals to a certain precision. Hence, we should always expect that floating point numbers are slightly off. Never try to compare two floating-point numbers for exact equality (==); instead, check if their absolute difference is less than some small constant like $10^{-9}$. Contest problems will accommodate this by either asking for the greatest integer less than $10^k$ times the value, or will mark as correct any output that is within a certain $\epsilon$ of the judge's answer. (what's epsilon?)
2020-06-16 00:11:54 +00:00
2020-06-16 01:44:03 +00:00
**Boolean** variables have two possible states: `true` and `false`. We'll usually use booleans to mark whether a certain process is done, and arrays of booleans to mark which components of an algorithm have finished.
2020-06-16 00:11:54 +00:00
2020-06-16 01:44:03 +00:00
**Character** variables represent a single Unicode character. They are returned when you access the character at a certain index within a string. Characters are represented using the ASCII standard, which assigns each character to a corresponding integer. This allows us to do arithmetic with them; for example, both `cout << ('f' - 'a');` in C++ and `System.out.print('f' - 'a');` in Java will print `5`.
2020-06-16 00:11:54 +00:00
2020-06-19 01:15:13 +00:00
**Strings** are stored as an array of characters. You can easily access the character at a certain index and take substrings of the string. String problems on USACO Bronze or Silver generally don't involve any special data structures.