1017 A除以B (20 分)

265 阅读1分钟

题目链接

  1. 常规做法应该就是模拟计算除法的过程,被除数应该不超过两位。
  2. 进行一次遍历即可。
  3. python中大整数并不会超范围,瞬间把这道题的难度拉没了。

c++代码

#include <iostream>
#include<string>
#include<vector> 
#include<queue>
#include <algorithm>
using namespace std;


int main() {
	string s;
	int a = 0;
	int b;
	queue<int>q;
	vector<int>res;
	cin >> s >> b;
	for (int i = 0; i < s.size(); i++) {
		q.push(s[i] - '0');
	}
	while (!q.empty()) {
		a = a * 10 + q.front();
		q.pop();

		res.push_back(a / b);
		a = a % b;
	}
	int i = 0;
	if (res.size() > 1 && res[0] == 0) //防止商为0的情况
		i++;
	for (; i < res.size(); i++) {
		cout << res[i];
	}
	cout << " " << a << endl;
	return 0;
}

python3代码

def main():
    s = list(map(int,input().split()))
    print('{} {}'.format(s[0]//s[1],s[0]%s[1]))
main()