HDU-1597find the nth digit,超短代码一遍过,啦啦啦啦~~
find the nth digit
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768
K (Java/Others)
Total Submission(s): 11311 Accepted Submission(s): 3378
-> Link <-
这种题应该是数位dp吧,之前在cf上第一次碰见这种题,此后不断碰到类似变形的,不过poj上那道Number sequense还是没有A出来,这道题和cf上的那道很相似,用的就是类似的思路,不是很难;
思路:既然是1121231234.....9 123..91 123...912 123...91234...,我们观察数字规律,第1位是1,第3位是2,第10位是4,出现第1个9的时候是第45位,也就是说前45位位数n=(1+i)*i/2,类似于前n项和;而45位以后,每出现的一个字串的长度从10开始递增,最低位从1开始到9然后又从1开始到9,直到位数凑够n;
这样说可能有点不明白,还是用代码解释吧:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int i=1;
while(n>i)
{
n-=i;//反过来就是相当于前i项和;
i++;
}
while(n>9) n-=9;//当其位数大于45时,就是上面所说的从1开始排到9又从1开始排到9.......所以。。
printf("%d\n",n);
}
return 0;
}//自己在稿子上模拟一下很好理解的;
要不是在cf上看到那种题这道题还不造要做多久,不得不又感慨一下CF上的题真的很启发思维啊,一道题发散性思维就可以解决很多问题,收获很大,做这道题估计不超过半小时,稍微推一下直接A了,真的好开心!!
HDU-1597find the nth digit,超短代码一遍过,啦啦啦啦~~的更多相关文章
- hdu 1597 find the nth digit (数学)
find the nth digit Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hdu 1597 find the nth digit
find the nth digit Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- find the nth digit(二分查找)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1597 find the nth digit Time Limit: 1000/1000 MS (Jav ...
- [LeetCode] Nth Digit 第N位
Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... Note: n i ...
- C++版 - Leetcode 400. Nth Digit解题报告
leetcode 400. Nth Digit 在线提交网址: https://leetcode.com/problems/nth-digit/ Total Accepted: 4356 Total ...
- (lower_bound)find the nth digit hdu1597
find the nth digit Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- leetcode 400 Add to List 400. Nth Digit
Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... Note:n is ...
- Leetcode: Nth Digit
Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... Note: n i ...
- Nth Digit | leetcode
Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... Note: n i ...
随机推荐
- 状压DP+记忆化搜索 UVA 1252 Twenty Questions
题目传送门 /* 题意:给出一系列的01字符串,问最少要问几个问题(列)能把它们区分出来 状态DP+记忆化搜索:dp[s1][s2]表示问题集合为s1.答案对错集合为s2时,还要问几次才能区分出来 若 ...
- ACM_折线中点
折线中点 Time Limit: 2000/1000ms (Java/Others) Problem Description: 给定平面上N个点P1, P2, ... PN,将他们按顺序连起来,形成一 ...
- 417 Pacific Atlantic Water Flow 太平洋大西洋水流
详见:https://leetcode.com/problems/pacific-atlantic-water-flow/description/ C++: class Solution { publ ...
- AJPFX总结FileWriter类的write方法
FileWriter从类OutputStreamWriter继承的 1.public void write(int c) throws IOException写入单个字符. ...
- hihocoder1705 座位问题
思路: 使用堆模拟.复习了priority_queue自定义结构体比较函数的用法. 实现: #include <bits/stdc++.h> using namespace std; ty ...
- git --版本对比
比较暂存区域和工作目录 -git diff 分别拷贝暂存区和工作目录的文件到a和b文件夹 --- //表示旧文件 暂存区的 +++ //表示新文件 工作目录的 F 一页一页往下移 B 一 ...
- nginx负载均衡浅析
熟悉Nginx的小伙伴都知道,Nginx是一个非常好的负载均衡器.除了用的非常普遍的Http负载均衡,Nginx还可以实现Email,FastCGI的负载均衡,甚至可以支持基于Tcp/UDP协议的各种 ...
- C++ 多态、虚函数、虚析构函数
1.若某种语言只支持类但不支持多态,则只能称为基于对象,不能说是面向对象. 2.多态:向不同对象发送同一个消息,不同的对象会产生不同的行为,发送消息可以是调用函数等操作.函数重载.运算符重载都是多态. ...
- UI概念体系要素
结构.渲染.交互.数据. 要素.呈现.交互 1)UI(组成)要素:结构 2)布局: 3)渲染: 4)事件处理: 5)数据:
- nginx 服务器
1.windows版本的nginx启动报错 No mapping for the Unicode character exists in the target multi-byte code page ...