[itint5]两数积全为1
这一题,首先如果直接去算的话,很容易就超出int或者long的表示范围了。那么要利用%的性质,(num * 10 + 1) % a = 10 * (num % a) + 1 % a。除了a为1的情况,都是10 * (num % a) + 1。然后计算的时候,先去掉是2和5的倍数的情况。也可以直接做,如果余数出现过,就不用继续了。
int findMinAllOne(int a) {
if (a % 2 == 0 || a % 5 == 0) return -1;
if (a == 1) return 1;
int num = 1;
int ans = 1;
while (true) {
if (num % a == 0) {
return ans;
} else {
num %= a;
num = num * 10 + 1;
ans++;
}
}
}
[itint5]两数积全为1的更多相关文章
- [LeetCode] Sum of Two Integers 两数之和
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
- [LeetCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- [CareerCup] 18.1 Add Two Numbers 两数相加
18.1 Write a function that adds two numbers. You should not use + or any arithmetic operators. 这道题让我 ...
- CSS+DIV两栏式全屏布局
在网上找了很久,发现大部分都是固定宽度设置两栏,全屏情况下的布局很少.最后终于完成了,写出来备查,也供大家参考. <!DOCTYPE html PUBLIC "-//W3C//DTD ...
- ✡ leetcode 167. Two Sum II - Input array is sorted 求两数相加等于一个数的位置 --------- java
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- 简单的两数之和再次乱入<< Add Two Numbers >>
请看题目描述: You are given two linked lists representing two non-negative numbers. The digits are stored ...
- d008: 求两数的整数商 和 商
内容: 求两数的整数商 和 商 ,商保留两位小数 输入说明: 一行 两个整数 输出说明: 一行,一个整数,一个实数(两位小数) 输入样例: 12 8 输出样例 : 1 1.50 #include ...
- d007: 求两数的整数商 和 余数
内容: 求两数的整数商 和 余数 输入说明: 一行两个整数 输出说明: 一行两个整数 输入样例: 18 4 输出样例 : 4 2 #include <stdio.h> int main ...
- C/S系统实现两数求和(非阻塞+epoll+心跳包检测用户在线状况+滚动日志+配置文件.)
C/S系统实现两数求和 任务要求: 实现配置文件 实现日志滚动 设置非阻塞套接字,EPOLL实现 检测客户端的连接,设置心跳检测 主线程 + 心跳检测线程 + EPOLL的ET模式处理事务线程 注意事 ...
随机推荐
- lucene4入门(2)搜索
欢迎转载http://www.cnblogs.com/shizhongtao/p/3440479.html 接着上一篇,这里继续搜索,对于搜索和创建一样,首先你要确定搜索位置,然后用规定的类来读取.还 ...
- Access数据库一种树形结构的实现和子节点查询
BOOL CManageDataBase::GetDepTreeAllSons( int rootItem ) { CADORecordset Rst(&m_DataBase); BOOL b ...
- 【转帖】error C2296: “^”: 非法,左操作数包含“double”类型
想要实现 ,写的C++程序为 double x; x=2^3; 结果程序总是出现这样的错误:error C2296: “^”: 非法,左操作数包含“double”类型 后来才发现操作符“^”,在C++ ...
- Maven使用总结
1.pom.xml文件中添加新的库 在中央仓库找不到你想要的jar的时候,可以在pom.xml中添加附加的库,语法如下 <repositories> <repository> ...
- PLSQL往Oracle数据库插入中文后变为问号 和 启动PLSQL时提示NLS_LANG在客户端不能确定的解决办法
PLSQL往Oracle数据库插入中文后变为问号 和 启动PLSQL时提示NLS_LANG在客户端不能确定的解决办法 1.检查服务器的字符编码 Select * from V$NLS_PARAMETE ...
- MongoDB之【增加用户认证、增加用户、删除用户、修改用户密码、读写权限、只读权限】
说明:增加用户是针对数据库进行操作 1.进入到数据库 use dbname 2.针对当前数据库添加用户 权限是针对当前数据 1.添加并验证用户 > use admin > db.addUs ...
- PHP curl 参数详解
PHP curl参数详解,分享一下. curl_setopt (PHP 4 >= 4.0.2) curl_setopt -- 为CURL调用设置一个选项 描述 bool curl_setopt ...
- CCNP第四天 OSPF综合实验(1)
ospf综合实验(1) 本实验主要考察ospf中的接口上的多种工作方式 实验如图所示: 所用拓扑为CCNP标准版,如图: --------------------------------------- ...
- iphone开发第一个UI应用程序QQ
#import <UIKit/UIKit.h> @interface HViewController : UIViewController @property (retain, nonat ...
- Django Admin后台使用tinymc 富文本编辑器
1.CDN地址 <script src="//cdn.tinymce.com/4/tinymce.min.js"></script> 2.修改base.ht ...