156 lines
5.6 KiB
HTML
156 lines
5.6 KiB
HTML
<!doctype html>
|
|
<html>
|
|
<head>
|
|
<meta charset="UTF-8" />
|
|
<meta name="viewport" content="width=device-width,initial-scale=1" />
|
|
<link rel="stylesheet " type="text/css" href="/cses.css?0" id="styles" />
|
|
<link
|
|
rel="stylesheet alternate"
|
|
type="text/css"
|
|
href="/cses-dark.css?0"
|
|
id="styles-dark"
|
|
/>
|
|
<meta name="theme-color" content="white" id="theme-color" />
|
|
<script type="application/json" id="darkmode-enabled">
|
|
false
|
|
</script>
|
|
<script src="/ui.js"></script>
|
|
<link
|
|
rel="stylesheet"
|
|
type="text/css"
|
|
href="/lib/fontawesome/css/all.min.css"
|
|
/>
|
|
</head>
|
|
<body class="with-sidebar">
|
|
<div class="header">
|
|
<div>
|
|
<a href="/" class="logo"><img src="/logo.png?1" alt="CSES" /></a>
|
|
<a
|
|
class="menu-toggle"
|
|
onclick="document.body.classList.toggle('menu-open')"
|
|
>
|
|
<i class="fas fa-bars"></i>
|
|
</a>
|
|
<div class="controls">
|
|
<a class="account" href="/login">Login</a>
|
|
<span>—</span>
|
|
<a
|
|
href="/darkmode"
|
|
title="Toggle dark mode"
|
|
onclick="return toggle_theme()"
|
|
><i aria-label="Dark mode" class="fas fa-adjust"></i
|
|
><span>Dark mode</span></a
|
|
>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="skeleton">
|
|
<div class="navigation">
|
|
<div class="title-block">
|
|
<h3><a href="/problemset/list/">CSES Problem Set</a></h3>
|
|
<h1>Weird Algorithm</h1>
|
|
<ul class="nav">
|
|
<li><a href="/problemset/task/1068/" class="current">Task</a></li>
|
|
<li><a href="/problemset/stats/1068/">Statistics</a></li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="content-wrapper">
|
|
<div class="content">
|
|
<title>CSES - Weird Algorithm</title
|
|
><link rel="stylesheet" href="/lib/katex/katex.min.css" />
|
|
<script defer src="/lib/katex/katex.min.js"></script>
|
|
<script defer src="/lib/katex/contrib/copy-tex.min.js"></script>
|
|
<script
|
|
defer
|
|
src="/lib/google-code-prettify/run_prettify.js"
|
|
></script>
|
|
<script>
|
|
addEventListener('DOMContentLoaded', function (e) {
|
|
const mathElements = document.getElementsByClassName('math')
|
|
const macros = {}
|
|
for (let element of mathElements) {
|
|
katex.render(element.textContent, element, {
|
|
displayMode: element.classList.contains('math-display'),
|
|
throwOnError: false,
|
|
globalGroup: true,
|
|
macros
|
|
})
|
|
}
|
|
})
|
|
</script>
|
|
<ul class="task-constraints">
|
|
<li><b>Time limit:</b> 1.00 s</li>
|
|
<li><b>Memory limit:</b> 512 MB</li>
|
|
</ul>
|
|
<div class="md">
|
|
<p>
|
|
Consider an algorithm that takes as input a positive integer
|
|
<span class="math math-inline">n</span>. If
|
|
<span class="math math-inline">n</span> is even, the algorithm
|
|
divides it by two, and if
|
|
<span class="math math-inline">n</span> is odd, the algorithm
|
|
multiplies it by three and adds one. The algorithm repeats this,
|
|
until <span class="math math-inline">n</span> is one. For example,
|
|
the sequence for <span class="math math-inline">n=3</span> is as
|
|
follows:
|
|
<span class="math math-display">
|
|
3 \rightarrow 10 \rightarrow 5 \rightarrow 16 \rightarrow 8
|
|
\rightarrow 4 \rightarrow 2 \rightarrow 1</span
|
|
>
|
|
Your task is to simulate the execution of the algorithm for a
|
|
given value of <span class="math math-inline">n</span>.
|
|
</p>
|
|
<h1 id="input">Input</h1>
|
|
<p>
|
|
The only input line contains an integer
|
|
<span class="math math-inline">n</span>.
|
|
</p>
|
|
<h1 id="output">Output</h1>
|
|
<p>
|
|
Print a line that contains all values of
|
|
<span class="math math-inline">n</span> during the algorithm.
|
|
</p>
|
|
<h1 id="constraints">Constraints</h1>
|
|
<ul>
|
|
<li><span class="math math-inline">1 \le n \le 10^6</span></li>
|
|
</ul>
|
|
<h1 id="example">Example</h1>
|
|
<p>Input:</p>
|
|
<pre>
|
|
3
|
|
</pre
|
|
>
|
|
<p>Output:</p>
|
|
<pre>
|
|
3 10 5 16 8 4 2 1
|
|
</pre
|
|
>
|
|
</div>
|
|
</div>
|
|
<div class="nav sidebar">
|
|
<h4>Introductory Problems</h4>
|
|
<a class="current" href="/problemset/task/1068"
|
|
>Weird Algorithm<span class="task-score icon"></span></a
|
|
><a href="/problemset/task/1083"
|
|
>Missing Number<span class="task-score icon"></span></a
|
|
><a href="/problemset/task/1069"
|
|
>Repetitions<span class="task-score icon"></span></a
|
|
><a href="/problemset/task/1094"
|
|
>Increasing Array<span class="task-score icon"></span></a
|
|
><a href="/problemset/task/1070"
|
|
>Permutations<span class="task-score icon"></span></a
|
|
><a href="/problemset/task/1071"
|
|
>Number Spiral<span class="task-score icon"></span></a
|
|
><a href="/problemset/task/1072"
|
|
>Two Knights<span class="task-score icon"></span></a
|
|
><a href="/problemset/task/1092"
|
|
>Two Sets<span class="task-score icon"></span></a
|
|
>...
|
|
<hr />
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>
|