天天看點

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