Timus 1209. 1, 10, 100, 1000... 要求给出特定序列的指定位置的值。
Problem Author: Alexey Lakhtin
Problem Source: USU Open Collegiate Programming Contest October'2002 Junior Session
返回目录
1209. 1, 10, 100, 1000...
Time Limit: 1.0 second
Memory Limit: 16 MB
Memory Limit: 16 MB
Let's consider an infinite sequence of digits constructed of ascending powers of 10 written one after another. Here is the beginning of the sequence: 110100100010000… You are to find out what digit is located at the definite position of the sequence.
Input
There is the only positive integer number N in the first line, N < 65536. The i-th of N left lines contains the positive integer Ki — the number of position in the sequence. It's given that Ki < 231.
Output
You are to output N digits 0 or 1 separated with a space. More precisely, the i-th digit of output is to be equal to the Ki-th digit of described above sequence.
Sample
input | output |
---|---|
4 3 14 7 6 |
0 0 1 0 |
Problem Source: USU Open Collegiate Programming Contest October'2002 Junior Session
这道题目是说,让我们考虑依次由10的非负整数次方连接在一起构成的无限序列:110100100010000…。你的任务是给出该序列指定位置的值。
通过仔细观察,可以看出,该序列中第 (x + 1) 个 1 位于第 N = 1 + (1 + 2 + ... x) = 1 + x(x + 1) / 2 个位置。解这个关于 x 的一元二次方程,得到它的非负根为:x = (√8N - 7 - 1) / 2。于是,当 x 是整数时,该序列中对应的第 N 个位置的值是 1,其余位置的值是 0。
下面就是相应的 C# 程序:
1 using System;
2
3 // http://acm.timus.ru/problem.aspx?space=1&num=1209
4 class T1209
5 {
6 static void Main()
7 {
8 for (int i = int.Parse(Console.ReadLine()); i > 0; i--)
9 {
10 double x = (Math.Sqrt(8.0 * int.Parse(Console.ReadLine()) - 7) - 1) / 2;
11 Console.Write(((x == (int)x) ? 1 : 0) + " ");
12 }
13 }
14 }
2
3 // http://acm.timus.ru/problem.aspx?space=1&num=1209
4 class T1209
5 {
6 static void Main()
7 {
8 for (int i = int.Parse(Console.ReadLine()); i > 0; i--)
9 {
10 double x = (Math.Sqrt(8.0 * int.Parse(Console.ReadLine()) - 7) - 1) / 2;
11 Console.Write(((x == (int)x) ? 1 : 0) + " ");
12 }
13 }
14 }
返回目录
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述