当前位置:网站首页>Pat class B 1017: a divided by B
Pat class B 1017: a divided by B
2022-07-19 05:13:00 【Doraemon 0219】
This problem requires calculation A/B, among A No more than 1000 Bit positive integer ,B yes 1 Positive integer . You need to export quotient Q And the remainder R, bring A=B×Q+R establish .
Input format :
The input is given in turn on a line A and B, In the middle to 1 The blank space to separate .
Output format :
Output sequentially in a row Q and R, In the middle to 1 The blank space to separate .
sample input :
123456789050987654321 7
sample output :
17636684150141093474 3
Code length limit
16 KB
The time limit
100 ms
Memory limit
64 MB
Problem analysis
The meaning of this question is easy to understand , Is to calculate a positive integer A/B The business of Q remainder R, But the problem is A The number of digits of involves the range of shaping . We know C In language int The number of bytes of type is generally 4, The scope is [-2^31~2^31-1], It's obviously not enough , So at first I thought of using Python Language to solve this problem .
Law 1 :Python
a,b=map(int,input().split(" "))
m=divmod(a,b)
print(m[0],m[1])among divmod(a,b) by Python A built-in function , What is returned is a list containing quotient and remainder list[Q,R];
map(function, iterable) It's also python A built-in function in ( It can be understood as mapping ): The first argument is a function , The second parameter is a sequence , Each element in the sequence acts as function Calculate and judge the parameters of the function , The result returned by the function will be saved as a new element .map The result returned by the function is a pass function New sequence after , This sequence is the sequence iterable stay function Last mapping .
Of course, if you don't use these functions, you can solve the problem :
str = input()
list = str.split(" ")
a=int(list[0])
b=int(list[1])
print(a//b,a-a//b*b)
Attention should be paid to Python Medium input() The default input to the function is a string , And it can contain spaces , If you want to enter two numbers in one line , It should be separated , It can be used list=str.split(“ ”) Separate them into a list , It can also be used. map() Function iterates .
Law two :C++
This method is more troublesome
| Fallibility and precautions |
| 1. because A No more than 1000 Bit positive integer , So it can't be used int,long long, Because the length is not enough |
| 2. Because I don't know the length of the number , So use string Length can be dynamically allocated |
| 3. When there's only one , It should directly output the first quotient , But if there are more than one , The first zero should be omitted , |
| 4. for example 3/7 The value of is 0 3; however 12/7 by 1 5, instead of 01 5, Therefore, when it is multi bit, the first bit should not be output |
C++ The code is as follows :
#include<iostream>
#include<string>
using namespace std;
int main()
{
string a; // Because I don't know the length of the number , So use string Length can be dynamically allocated
int b;
int q,r=0;
cin>>a>>b;
if(a.length()==1)
// When there's only one , It should directly output the first quotient , But if there are many people , The first zero should be omitted ,
{
q=(a[0]-'0'+r*10)/b;
r=(a[0]-'0'+r*10)%b;
printf("%d",q);
}
else
{
q=(a[0]-'0'+r*10)/b;
r=(a[0]-'0'+r*10)%b;
if(q!=0)
printf("%d",q);
for(int i=1;i<a.length();i++)
{
q=(a[i]-'0'+r*10)/b;
r=(a[i]-'0'+r*10)%b;
printf("%d",q);
}
}
printf(" %d\n",r);
return 0;
}
pure C edition :
#include<stdio.h>
#include<string.h>
int main()
{
char a[1002]={0};
int b;
scanf("%s",a);
scanf("%d",&b);
int len=strlen(a),h=a[0]-'0',q=0;// initialization len by A Number of digits ,h by A First place ,q by 0, Because there is no carry for the first
if(len==1)// If A There is only one
{
printf("%d %d\n",h/b,h%b);
return 0;//main function return 0; The program ends directly
}
if(h/b==0)//A There are many and the first is better than B Small
q=h;
else//A There are many and the first is better than B Big
{
printf("%d",h/b);
q=h%b;
}
int i=1;
while(a[i]!=0)// The end flag of the character array is ASCII Code value 0
{
h=a[i++]-'0';
printf("%d",(q*10+h)/b);
q=(q*10+h)%b;
}
printf(" %d\n",q); // Be careful %d There's a space in front
return 0;
}边栏推荐
猜你喜欢
随机推荐
Submit the uniapp form (input, radio, picker) to get the parameter value
C语言练习2
Two methods of obtaining URL parameters and various methods of obtaining location objects
使用js中的(offset,page)实现登录效果
这么6的刷题网站你不会没听说过吧?你已经out 了?
PyGame aircraft War 1.0 (step + window no response problem)
IDL 6S查找表
一个问题的探讨
[batch] batch delete intermediate folder - personal research script
【Es6】forEach,for...in ,for...of专栏,让你通过项目案例快速分辨各种for语句的使用方式及区别(完整版)内部有详细注释
es6新增-对象部分
Uni app conditional compilation ifdef ENDIF compatible with multiple terminals
MySQL optimization
Case summary of rotation chart moving speed (constant speed, slow motion)
【C语言_复习_学习第二课】什么是进制?进制之间应该如何转换
滚动轮加载的两种js方法及模态框拖拽归总
User login - and create SMS verification code
轮播图的两种方法及自动轮播
02 Bar _ Recommandation de film (basée sur le contenu) Portrait de l'utilisateur
[2022 10th Teddy Cup Challenge] Title A: complete version of pest identification (general idea. Detailed process and code and results CSV in compressed package)









