[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模式处理事务线程 注意事 ...
随机推荐
- JavaScript学习笔记(13)——BOM
1.window 所有浏览器都支持window对象,它表示浏览器窗口本身. 所有 JavaScript 全局对象.函数以及变量均自动成为 window 对象的成员. 全局变量是 window 对象的属 ...
- undrop for innodb c_parser 不完美之处
今天发现c_parser导出数据是会丢掉某些行,给过调试发现是他处理utf8编码时计算有误,目前还没有发现自动解决总是的方法,只会手动改代码来解决. 下一步计划把c_parser移植到windows下 ...
- An Easy Task
An Easy Task Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
- 一个封装好的C++比特数组BitArray,可以对位进行直接操作
本来仅仅只是用来做哈夫曼实验时的辅助,后来一想干脆封装好,省得以后又要用到比特位的操作. 基本用法示例: BitArray bits; bits[] = ; bits[] = ; cout<& ...
- 【C++】GacLib——ListView.ViewSwitching
http://www.gaclib.net/Demos/Controls.ListView.ViewSwitching/Demo.html#FILESYSTEMINFORMATION_H
- N皇后问题2
Description Examine the checkerboard below and note that the six checkers are arranged on the board ...
- linux配置JDK(转载)
转载自:http://blog.csdn.net/xinxin19881112/article/details/46816385 Linux CentOS 6.6安装JDK1.7 目录 1.下载JDK ...
- (转)使用Migrations更新数据库结构(Code First )
原文地址:http://blog.csdn.net/luoyeyu1989/article/details/8275237 背景 code first起初当修改model后,要持久化至数据库中时,总要 ...
- 巧用九宫格以减少UI资源量
UI资源量对资源包大小和内存的影响 UI资源具有以下特点: (1)UI资源几乎都是图片,而图片是最占资源量的资源类型之一. (2)Unity不支持外部压缩,即使在外部将一个10MB的图片压缩到只剩1M ...
- 洛谷 P1858 多人背包
求01背包前k优解的价值和 输入输出格式 Input/output 输入格式:第一行三个数K.V.N(k<=50,v<=5000,n<=200)接下来每行两个数,表示体积和价值输出格 ...