当前位置:网站首页>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;
}边栏推荐
- STL容器——queue与deque的基本操作
- 【C语言—零基础_学习_复习_第五课】基本运算符的运算性质
- 学习C语言第三天
- 租用服务器,以及部署在pycharm专业版上的pytorch环境训练yolov5模型教程服务器环境安装库文件:
- 645. 错误的集合
- User management - restrictions
- 使用Echars实现水滴状、环形图、分割图、堆叠、组织架构图、地图轮廓等图表
- 游玩数据获取与数据分析、数据挖掘 【2022.5.30】
- 数据分析与数据挖掘实战案例本地房价预测(716):
- 【LeetCode——编程能力入门第一天】基本数据类型[在区间范围内统计奇数数目/去掉最低工资和最高工资后的工资平均值)
猜你喜欢

【C】 Beam calculator

About the current response, the method getoutputstream() has been called

IText modify PDF Text

Email (including attachments, Netease, QQ)

【LeetCode——编程能力入门第一天】基本数据类型[在区间范围内统计奇数数目/去掉最低工资和最高工资后的工资平均值)

第十届泰迪杯数据挖掘挑战赛A题害虫识别YOLOv5模型代码(已跑通,原创作品,持续更新)

C语言 带你 手撕 通讯录

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

Internship project 2 - Homepage configuration - my data module
![[2022 10th Teddy Cup Challenge] Title A: complete version of pest identification (general idea. Detailed process and code and results CSV in compressed package)](/img/e6/beea0bb0a9f4b41206c6fcb130fdfd.png)
[2022 10th Teddy Cup Challenge] Title A: complete version of pest identification (general idea. Detailed process and code and results CSV in compressed package)
随机推荐
Uni app conditional compilation ifdef ENDIF compatible with multiple terminals
es6新增-运算符的扩展
The principle and local storage of the throttle valve of the rotation chart are summarized
学习C语言第二天
IText modify PDF Text
Internship project 2 - Homepage configuration - my data module
Infinite classification
C语言练习2
Bi design: distributed high concurrency epidemic prevention health management system based on vue+socket+redis
读论文《Learning to Measure Changes: Fully Convolutional Siamese Metric Networks for Scene Change Detec》
百度地图 实现 热力图
Travel data acquisition, data analysis and data mining [2022.5.30]
Applet cloud development upload pictures to cloud storage
获取数组中对象内部的数值最大与最小值多功能版及点名系统完整版并展示效果
es6新增-Symbol数据类型
Applet cloud development form submission and data acquisition in the page
学习C语言第8天
Submit the uniapp form (input, radio, picker) to get the parameter value
Database training 7 [index and creation of data integrity constraints]
学习C语言的第五天