当前位置:网站首页>PAT 乙等 1017 C语言
PAT 乙等 1017 C语言
2022-06-23 04:11:00 【章鱼bro】
1017. A除以B (20)
本题要求计算A/B,其中A是不超过1000位的正整数,B是1位正整数。你需要输出商数Q和余数R,使得A = B * Q + R成立。
输入格式:
输入在1行中依次给出A和B,中间以1空格分隔。
输出格式:
在1行中依次输出Q和R,中间以1空格分隔。
输入样例:123456789050987654321 7输出样例:
17636684150141093474 3
思路:只能用字符串一位一位转化为数字处理。被除数从字符串左侧起转化一位除以除数,若有余数乘10留到下一位,否则为零,余数乘10留到下一位。注意若第一位被除数小于除数不能输出零,必须有非零位输出后才可以输出0
一、起始变量
1.除数divider,被除数dibideder,每位余数left
2.flag标志位,用来标记是否有商的非零位输出
二、运算
1.读入被除数和除数。
2.在for循环中一位一位转化被除数,转化一位除以被除数,若非零输出,若为零,在已经有非零位商输出的前提下输出,否则不输出。
3.若有余数乘10留到下一位
4.若flag一直为0,则说明被除数小于除数,输出0
三、代码
#include "stdio.h"
#include "string.h"
int main()
{
char divideder[1001];
int divider;
int flag = 0;//是否有非零位商输出,在没有非零位输出之前0不输出;
int i;
int left = 0;//每位整除之后的余数;
scanf("%s %d", divideder, ÷r);
int len = strlen(divideder);
//如果被除数为0,直接得结果;
for(i = 0; i < len; i++)
{
left = left*10 + divideder[i] - '0';
if(left >= divider)
{
printf("%d",left / divider);
flag = 1;//输出商,并改变flag的值,flag是是否有一位的商被输出的标志
}
else if(flag)
{
printf("0");//商的第一位输出后,且被除数的第i位小于除数,则输出0;
}
left = left % divider;//得到余数
}
if(flag == 0)
{
printf("0");
}//被除数只有一位且小于除数;
printf(" %d",left);
return 0;
}边栏推荐
- Is there a real part-time job online? How do college students find part-time jobs in summer?
- IP6809三线圈15W无线充电发射端方案ic英集芯
- Shutdown shutdown command
- Raspberry pie assert preliminary exercise
- 抽奖 ddd 代码
- Yingjixin launched 4 series of lithium batteries 100W mobile power supply voltage rise and fall scheme SOC chip ip5389
- [image fusion] sparse regularization based on non convex penalty to realize image fusion with matlab code
- Jvm: when a method is overloaded, the specific method to call is determined by the static type of the incoming parameter rather than the actual type of the parameter
- STC 32位8051单片机开发实例教程 一 开发环境搭建
- 英集芯推出4串锂电池100W移动电源升降压方案SoC芯片IP5389
猜你喜欢

What does the English letter PC mean? What does the Internet PC mean

MySQL面试真题(二十四)——行列互换

MySQL面试真题(二十八)——案例-通讯运营商指标分析

Genetic engineering of AI art? Use # artbreeder to change any shape of the image

MySQL面试真题(二十二)——表连接后的条件筛选及分组筛选

Arctime makes Chinese and English subtitle video
![[image fusion] sparse regularization based on non convex penalty to realize image fusion with matlab code](/img/e2/24eb2a60e3dc603b3ec4bfefd0b8e5.png)
[image fusion] sparse regularization based on non convex penalty to realize image fusion with matlab code

基于SSM框架的借阅图书管理系统

How much disk IO will actually occur for a byte of the read file?

Real MySQL interview question (30) -- shell real estate order analysis
随机推荐
Advanced Mathematics (Seventh Edition) Tongji University exercises 1-9 personal solutions
Webrtc[47] - a common way for webrtc to save YUV data
MySQL面试真题(二十五)——常见的分组比较场景
runc 符号链接挂载与容器逃逸漏洞预警(CVE-2021-30465)
技能自检 | 想当测试Leader,这6项技能你会吗?
Real MySQL interview question (XXVIII) -- case - Analysis of indicators of communication operators
visdom的使用
【owt】owt-client-native-p2p-e2e-test vs2017构建 6:修改脚本自动生成vs工程
数字藏品——新的投资机遇
数字藏品如何赋能经济实体?
How does win11 enable mobile hotspot? How to enable mobile hotspot in win11
Build a gocd environment
June 22, 2022: golang multiple choice question, what does the following golang code output? A:3; B:1; C:4; D: Compilation failed. package main import ( “fmt“ ) func mai
How to move the software downloaded from win11 app store to the desktop
Management system of borrowed books based on SSM framework
What if win11 cannot record audio? Solution of win11 unable to input sound
TCP/IP 详解(第 2 版) 笔记 / 3 链路层 / 3.4 网桥与交换机
iNFTnews | 加密之家从宇宙寄来的明信片,你会收到哪一张?
What is the reason for the black screen of the computer monitor when the computer is turned on? What should I do about the black screen of the computer monitor
B-string value (string DP) of the 16th Northeast College Students' Programming Competition (warm-up)