PAT 1096. Consecutive Factors (20)
2015-08-18 12:57
477 查看
1096. Consecutive Factors (20)
Among all the factors of a positive integer N, there may exist several consecutive numbers. For example, 630 can be factored as 3*5*6*7, where 5, 6, and 7 are the three consecutive numbers. Now given any positive N, you are supposed to find the maximum number of consecutive factors, and list the smallest sequence of the consecutive factors.Input Specification:
Each input file contains one test case, which gives the integer N (1<N<231).
Output Specification:
For each test case, print in the first line the maximum number of consecutive factors. Then in the second line, print the smallest sequence of the consecutive factors in the format "factor[1]*factor[2]*...*factor[k]", where the factors are listed in increasing order, and 1 is NOT included.
Sample Input:
630
Sample Output:
3 5*6*7 最初将start的下一个设置为start = start + length + 1, 这种是错误的。而且这里用long long是因为start*start会超过int32表示的范围而不能跳出导致最后一个测试点运行超时
#include <iostream> #include <cmath> using namespace std; int main() { long long num; cin >> num; long long start = 2, length = 0; long long maxLength = 0, maxStart; long long remain = num; while (1) { if (remain % (start + length) != 0) { if (length > maxLength) { maxLength = length; maxStart = start; } start++; length = 0; remain = num; if (start*start > num) break; } else { remain /= (start + length); length++; } } if (maxLength == 0) { cout << 1 << endl; cout << num; return 0; } cout << maxLength << endl; for (long long i = 0; i < maxLength; i++) { cout << maxStart + i; if (i < maxLength - 1) cout << "*"; } }
相关文章推荐
- 网上搜集的一段php可逆加密函数
- 浅析pinyin4j源码 简单利用pinyin4j对中文字符进行自然排序(转)
- Flickr Architecture
- codeforces - 3C - Tic-tac-toe
- 开发Android硬件访问服务
- Rust初步(一):介绍
- 经典排序算法1--冒泡排序
- 我的高中时代──纪念1995年进入高中20周年
- 发两张学院的100元优惠券,满300可用
- iOS 8 毛玻璃效果(模糊)
- 基于java的挖地雷游戏
- Android源码分析之Handler机制
- 泛型(java基础)
- UI笔记:UILabel、UIButton和UITextField
- POJ 1753 Flip Game(类似与N皇后问题的DFS)
- Activity 生命周期
- 基于Volley,Gson封装支持JWT无状态安全验证和数据防篡改的GsonRequest网络请求类
- thinking in java 学习笔记(一)
- HDU 2616 Kill the monster (暴力搜索 || 终极全阵列暴力)
- NodeJS简介