Java实现第八届蓝桥杯取数位
2024-10-09 07:52:45
取数位
求1个整数的第k位数字有很多种方法。
以下的方法就是一种。
还有一个答案:f(x/10,k--)
public class Main {
static int len(int x){ // 返回多少位
if(x<10) return 1;
return len(x/10)+1;
}
// 取x的第k位数字
static int f(int x, int k){ //数字 第几位数23513 5-3=2
if(len(x)-k==0) return x%10; //如果是最后一位数
return (int) (x/Math.pow(10, len(x)-k)%10); //填空
}
public static void main(String[] args)
{
int x = 295631;
//System.out.println(len(x));
System.out.println(f(x,4));
}
}
最新文章
- CRLF line terminators导致shell脚本报错:command not found
- Windows 7 封装篇(一)【母盘定制】[手动制作]定制合适的系统母盘
- JS js与css的动态加载
- HTML5新特性及详解
- linux设备驱动归纳总结(三):2.字符型设备的操作open、close、read、write【转】
- 三步将Node应用部署到Heroku上 --转载
- html5文本框提示文字属性为placeholder
- css 关于两栏布局,左边固定,右边自适应
- UVA 125 Numbering Paths
- python-文件压缩和解压
- Session、Application、Cache
- MySQL5.7: sql script demo
- 什么是java字节码?
- Titanic缺失数值处理 &; 存活率预测
- leetcode33
- react native 使用TabNavigator编写APP底部导航
- Docker的常用命令
- ES6之Promise对象
- Swift 错误记录
- Faster R-CNN在GPU下的安装、测试经历