当前位置: 首页 > news >正文

Digit Queries

题目描述

Consider an infinite string that consists of all positive integers in increasing order:
12345678910111213141516171819202122232425...

Your task is to process q queries of the form: what is the digit at position k in the string?

输入

The first input line has an integer q(1 ≤ q ≤ 1000): the number of queries.
After this, there are q lines that describe the queries. Each line has an integer k(1\leqslant k\leqslant 10^{18}): a 1-indexed position in the string.

输出

For each query, print the corresponding digit.

样例输入
3
7
19
12
样例输出
7
4
1
思路分析

自然数按位数分组,确定k所在区间,找出k定位到的自然数。

代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int q;
ll k;
int main(){ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);cin>>q;while(q--){cin>>k;ll d=9,w=1,total_d,total_w,ans;total_w=d*w;total_d=d;while(k>total_w){d*=10;w++;total_w+=d*w;total_d+=d;}ll diff=(k-(total_w-d*w))%w;ll n=(k-diff-(total_w-d*w))/w;ll st=pow(10,w-1);for(ll i=2;i<=n;i++){st++;}if(diff==0){ans=st%10;}else{st++;for(int i=1;i<=w-diff;i++){st/=10;}ans=st%10;}cout<<ans<<"\n";}return 0;
}

http://www.dtcms.com/a/299355.html

相关文章:

  • git推送文件失败
  • 深入解析Hadoop如何实现数据可靠性:三副本策略、校验和验证与Pipeline复制
  • Qt 网络编程进阶:RESTful API 调用
  • Android Studio Profiler工具使用流程
  • C++连接MySQL完整教程
  • 哪些人需要用到MBTI职业性格测试?
  • c#Lambda 表达式与事件核心知识点整理
  • 对比JS“上下文”与“作用域”
  • 网络资源模板--基于Android Studio 实现的简易购物App
  • 变频器实习DAY15
  • 地级市-生态韧性数据(2000-2022年)-实证数据
  • VTK交互——Callback
  • MySQL 8.0 OCP 1Z0-908 题目解析(36)
  • 【Redis】 Redis 基础命令和原理
  • 前端设计中如何在鼠标悬浮时同步修改块内样式
  • LeetCode 895:最大频率栈
  • HTML初学者第五天
  • 计算机编码
  • Linux权限机制:RUID/EUID/SUID与进程安全
  • 排序算法,咕咕咕
  • 【Zephyr】Window下的Zephyr编译和使用
  • window下MySQL安装(三)卸载mysql
  • PYTHON从入门到实践-16数据视图化展示
  • Rust Web框架性能对比与实战指南
  • 数据结构 二叉树(3)---层序遍历二叉树
  • SQLite 子查询详解
  • 告别复杂配置!Spring Boot优雅集成百度OCR的终极方案
  • C语言基础10——函数
  • Qt 与 WebService 交互开发
  • Linux学习 韦东山IMX6ULL(一)搭建环境并编译程序