当前位置: 首页 > 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.lryc.cn/news/600914.html

相关文章:

  • OpenFeign-远程调用
  • 数据结构 二叉树(2)---二叉树的实现
  • excel删除重复项场景
  • HarmonyOS中的PX、 VP、 FP 、LPX、Percentage、Resource 详细区别是什么
  • 商汤InternLM发布最先进的开源多模态推理模型——Intern-S1
  • CUDA杂记--FP16与FP32用途
  • P2392 kkksc03考前临时抱佛脚
  • Linux——线程互斥
  • 【RHCSA 问答题】第 13 章 访问 Linux 文件系统
  • PYTHON从入门到实践-16数据视图化展示
  • 卫星通信终端天线对星之:参考星对星
  • DOM元素添加技巧全解析
  • 单片机CPU内部的定时器——滴答定时器
  • Linux DNS 服务器正反向解析
  • Mybatis学习之配置文件(三)
  • Linux随记(二十一)
  • 变频器实习DAY15
  • Linux内核设计与实现 - 第13章 虚拟文件系统(VFS)
  • Linux shuf命令随机打乱行顺序
  • 差模干扰 共模干扰
  • 利用RAII与析构函数避免C++资源泄漏
  • kafka的部署和jmeter连接kafka
  • 20250726-2-Kubernetes 网络-Service 定义与创建_笔记
  • C++/CLI vs 标准 C++ vs C# 语法对照手册
  • Java 大视界 -- Java 大数据在智能医疗影像数据标注与疾病辅助诊断模型训练中的应用(366)
  • greenhills编译出错问题
  • 20250726-1-Kubernetes 网络-Service存在的意义_笔记
  • 【Spring AI】大模型服务平台-阿里云百炼
  • 高可用集群KEEPALIVED的详细部署
  • 【MySQL】MySQL 缓存方案