天天看点

PAT练习笔记——5.6 大整数运算第5章 入门篇(3)——数学问题

2019年9月PAT - 练习笔记——5.6

以下页码标注的是阅读器中实际页码,而不是书本身自印的页码。

第5章 入门篇(3)——数学问题

5.6 大整数运算

注意

  1. 注意判断是否需要用大整数运算
  2. 注意大整数加法和乘法时最后一位的进位
  3. 注意大整数减法和除法时得到的结果要从第一个非0数字开始输出

目录

  1. B1017 A除以B
  2. A1023 Have Fun with Numbers
  3. A1024 Palindromic Number
  1. B1017 A除以B

    本题要求计算 A/B,其中 A 是不超过 1000 位的正整数,B 是 1 位正整数。你需要输出商数 Q 和余数 R,使得 A=B×Q+R 成立。

    输入格式:

    输入在一行中依次给出 A 和 B,中间以 1 空格分隔。

    输出格式:

    在一行中依次输出 Q 和 R,中间以 1 空格分隔。

    输入样例:

    123456789050987654321 7
               

    输出样例:

    17636684150141093474 3
               
    1. 我的
      #include <iostream>
      #include <string>
      
      using namespace std;
      
      int main(void)
      {
      	string a = "";
      	cin >> a;
      	int b = 0;
      	cin >> b;
      	
      	string q = "";
      	int r = 0;
      	for (int i = 0;i < a.size();++i) {
      		int now = a[i] - '0';
      		int num = r * 10 + now;
      		q += to_string(num / b);
      		r = num % b;
      	}
      	
      	int i = 0;
      	for (;q[i] == '0' && i < q.size() - 1;++i);
      	for (;i < q.size();++i) cout << q[i];
      	cout << " " << r;
      	
      	return 0;
      } 
                 
    2. 《算法笔记》P231
  2. A1023 Have Fun with Numbers

    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
               
    1. 我的
      #include <iostream>
      #include <string>
      #include <map>
      
      using namespace std;
      
      int main(void)
      {
      	string a = "";
      	cin >> a;
      	
      	string b = "";
      	int carry = 0;
      	map<int, int> numbers;
      	for (int i = a.size() - 1;i >= 0;--i) {
      		int now = a[i] - '0';
      		++numbers[now];
      		int num = now * 2 + carry;
      		int number = num % 10;
      		--numbers[number];
      		b = to_string(number) + b;
      		carry = num / 10;
      	}
      	if (carry) {
      		--numbers[carry];
      		b = to_string(carry) + b;
      	}
      	
      	bool flag = true;
      	for (int i = 0;i < 10;++i) {
      		if (numbers[i]) {
      			flag = false;
      			break;
      		}
      	}
      	flag ? (cout << "Yes\n") : (cout << "No\n");
      	for (int i = 0;i < b.size();++i) cout << b[i];
      	
      	return 0;
      } 
                 
      注意处理最后一位的进位,否则测试点2、7答案错误
    2. 《算法笔记》P233
  3. A1024 Palindromic Number

    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 (≤1010) 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. 我的
      #include <iostream>
      #include <algorithm>
      
      using namespace std;
      
      bool IsPalindromic(const string&n)
      {
      	for (int i = 0, j = n.size() - 1;i < j;++i, --j) {
      		if (n[i] != n[j]) return false;
      	}
      	return true;
      }
      
      int main(void)
      {
      	long long n = 0;
      	int k = 0;
      	cin >> n >> k;
      	
      	string str = to_string(n), rstr = str;
      	int i = 1;
      	for (;i <= k && !IsPalindromic(str);++i) {
      		reverse(rstr.begin(), rstr.end());
      		
      		string sum = "";
      		int carry = 0;
      		for (int i = str.size() - 1;i >= 0;--i) {
      			int now1 = str[i] - '0', now2 = rstr[i] - '0';
      			int num = now1 + now2 + carry;
      			int number = num % 10;
      			sum = to_string(number) + sum;
      			carry = num / 10;
      		}
      		if (carry) sum = to_string(carry) + sum;
      		
      		str = sum;
      		rstr = str;
      	}
      	cout << str << endl << --i;
      	
      	return 0;
      } 
                 
    2. 《算法笔记》P236