当前位置:网站首页>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;
}边栏推荐
- Two JS methods of rolling wheel loading and modal box dragging
- 微信小程序wx.setClipboardData复制文本
- Class object automatic injection attribute operation tool
- Teddy Cup title a full version optimization update (4/23)
- uniapp中使用ucharts图表,饼状图,柱状图,折线图
- Internship project 3- change owner
- 哨兵二号轨道数据下载
- The code of yolov5 model for pest identification in Title A of the 10th Teddy cup data mining challenge (has been run through, original works, continuously updated)
- C语言 带你 手撕 通讯录
- Applet cloud development upload pictures to cloud storage
猜你喜欢

决策树原理和案例应用-泰坦尼克号生存预测

数据库实训7【索引与数据完整性约束的创建】

Uni app conditional compilation ifdef ENDIF compatible with multiple terminals

Simply and quickly establish a pytorch environment yolov5 target detection model to run (super simple)

IText modify PDF Text

小程序云开发表单提交并在页面中获取数据

C语言 带你 手撕 通讯录

uniapp中使用ucharts图表,饼状图,柱状图,折线图

Ucharts chart, pie chart, bar chart and line chart are used in uniapp

基于RTX30显卡的ArcGIS Pro2.8深度学习环境配置
随机推荐
Email (including attachments, Netease, QQ)
模拟库函数
使用js实现安居客二级菜单及(注意事项和问题点演示)完整版
这么6的刷题网站你不会没听说过吧?你已经out 了?
C语言练习题
Two methods of rotation chart and automatic rotation
Uniapp uses uview to realize folding panel
Applet cloud development form submission and data acquisition in the page
Use of flask
H5页面使用js生成二维码
Infinite classification
uniapp 表单(input、radio、picker)提交获取参数值
User management - restrictions
ArcGIS Pro发布服务
学习C语言第8天
【C语言—零基础第十一课】旋转大转盘之指针
Base64 and file conversion
User management - paging
微信小程序5-基础加强(没写完)
Class object automatic injection attribute operation tool