目录
1096 Consecutive Factors (20分)
1023 Have Fun with Numbers (20分)
质因子分解
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<2^31).
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
思路:暴力枚举 连续的乘积因子,维护一个最大长度的乘积因子
#include <iostream>
#include <cmath>
using namespace std;
long int num, temp;
int main(){
cin >> num;
int first = 0, len = 0, maxn = sqrt(num*1.0) + 1;
for (int i = 2; i <= maxn; i++) { //暴力枚举 连续的乘积因子
int j;
temp = 1;
for (j = i; j <= maxn; j++) {
temp *= j;
if (num % temp != 0) break;
}
if (j - i > len) {
len = j - i;
first = i;
}
}
if (first == 0) {
cout << 1 << endl << num;
} else {
cout << len << endl;
for (int i = 0; i < len; i++) {
cout << first + i;
if (i != len - 1) cout << '*';
}
}
return 0;
}
1059 Prime Factors (25分)
Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1^k1×p2^k2×⋯×pm^km.
Input Specification:
Each input file contains one test case which gives a positive integer N in the range of long int. (即:int)
Output Specification:
Factor N in the format N =
p1^
k1*
p2^
k2*
…*
pm^
km, where pi's are prime factors of N in increasing order, and the exponent ki is the number of pi -- hence when there is only one pi, ki is 1 and must NOT be printed out.
Sample Input:
97532468
Sample Output:
97532468=2^2*11*17*101*1291
分析:根号int的最大值不超过50000,先建立个50000以内的素数表,然后从2开始一直判断是否为它的素数,如果是就将a=a/i继续判断i是否为a的素数,判断完成后输出这个素数因子和个数,用state判断是否输入过因子,输入过就要再前面输出“*”
#include <cstdio>
#include <vector>
using namespace std;
vector<int> prime(50000, 1);
int main() {
for(int i = 2; i * i < 50000; i++)
for(int j = 2; j * i < 50000; j++)
prime[j * i] = 0;
long int a;
scanf("%ld", &a);
printf("%ld=", a);
if(a == 1) printf("1");
bool state = false;
for(int i = 2; i < 50000 && a >= 2; i++) {
int cnt = 0, flag = 0;
while(prime[i] == 1 && a % i == 0) {
cnt++;
a = a / i;
flag = 1;
}
if(flag) {
if(state) printf("*");
printf("%d", i);
state = true;
}
if(cnt >= 2) printf("^%d", cnt);
}
if (a > 1) printf("%s%ld", state ? "*" : "", a);
return 0;
}
大整数运算
B1017 A除以B (20分)
本题要求计算 A/B,其中 A 是不超过 1000 位的正整数,B 是 1 位正整数。你需要输出商数 Q 和余数 R,使得 A=B×Q+R 成立。
输入格式:
输入在一行中依次给出 A 和 B,中间以 1 空格分隔。
输出格式:
在一行中依次输出 Q 和 R,中间以 1 空格分隔。
输入样例:
123456789050987654321 7
输出样例:
17636684150141093474 3
分析:模拟手动除法的过程,每次用第一位去除以B,如果得到的商不是0就输出,否则就*10+下一位,直到最后的数为余数~
#include <iostream>
using namespace std;
int main() {
string s;
int a, t = 0, temp = 0;
cin >> s >> a;
int len = s.length();
t = (s[0] - '0') / a;
if ((t != 0 && len > 1) || len == 1)
cout << t;
temp = (s[0] - '0') % a;
for (int i = 1; i < len; i++) {
t = (temp * 10 + s[i] - '0') / a;
cout << t;
temp = (temp * 10 + s[i] - '0') % a;
}
cout << " " << temp;
return 0;
}
1023 Have Fun with Numbers (20分)
Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, with no duplication. Double it we will obtain 246913578, which happens to be another 9-digit number consisting exactly the numbers from 1 to 9, only in a different permutation. Check to see the result if we double it again!
Now you are suppose to check if there are more numbers with this property. That is, double a given number with k digits, you are to tell if the resulting number consists of only a permutation of the digits in the original number.
Input Specification:
Each input contains one test case. Each case contains one positive integer with no more than 20 digits.
Output Specification:
For each test case, first print in a line "Yes" if doubling the input number gives a number that consists of only a permutation of the digits in the original number, or "No" if not. Then in the next line, print the doubled number.
Sample Input:
1234567899
Sample Output:
Yes
2469135798
#include <cstdio>
#include <string.h>
using namespace std;
int book[10];
int main() {
char num[22];
scanf("%s", num);
int flag = 0, len = strlen(num);
for(int i = len - 1; i >= 0; i--) {
int temp = num[i] - '0';
book[temp]++;
temp = temp * 2 + flag;
flag = 0;
if(temp >= 10) {
temp = temp - 10;
flag = 1;
}
num[i] = (temp + '0');
book[temp]--;
}
int flag1 = 0;
for(int i = 0; i < 10; i++) {
if(book[i] != 0)
flag1 = 1;
}
printf("%s", (flag == 1 || flag1 == 1) ? "No\n" : "Yes\n");
if(flag == 1) printf("1"); //注意点 下面例子
printf("%s", num);
return 0;
}
/**
9987654
No
19975308
*/
1024 Palindromic Number (25分)
A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers.
Non-palindromic numbers can be paired with palindromic ones via a series of operations.
First, the non-palindromic number is reversed and the result is added to the original number.
If the result is not a palindromic number, this is repeated until it gives a palindromic number.
For example, if we start from 67, we can obtain a palindromic number in 2 steps: 67 + 76 = 143, and 143 + 341 = 484.
Given any positive integer N, you are supposed to find its paired palindromic number and the number of steps taken to find it.
Input Specification:
Each input file contains one test case. Each case consists of two positive numbers N and K,
where N (≤10^10) is the initial numer and K (≤100) is the maximum number of steps.
The numbers are separated by a space.
Output Specification:
For each test case, output two numbers, one in each line. The first number is the paired palindromic number of N, and the second number is the number of steps taken to find the palindromic number. If the palindromic number is not found after K steps, just output the number obtained at the Kth step and K instead.
Sample Input 1:
67 3
Sample Output 1:
484
2
Sample Input 2:
69 3
Sample Output 2:
1353
3
分析:
1、会超出long int类型(会有两个点溢出错误),所以用字符串存储,大整数相加
2、可以通过对字符串翻转后比较来判断是否为回文串
#include <iostream>
#include <algorithm>
using namespace std;
string s;
void add(string t) {
int len = s.length(), carry = 0;
for(int i = 0; i < len; i++) {
s[i] = s[i] + t[i] + carry - '0';
carry = 0;
if(s[i] > '9') {
s[i] = s[i] - 10;
carry = 1;
}
}
if(carry) s += '1';
reverse(s.begin(), s.end());
}
int main() {
int cnt, i;
cin >> s >> cnt;
for(i = 0; i <= cnt; i++) {
string t = s;
reverse(t.begin(), t.end());
if(s == t || i == cnt) break;
add(t);
}
cout << s << endl << i;
return 0;
}