当前位置:网站首页>Pat class B 1017 C language

Pat class B 1017 C language

2022-06-23 05:57:00 Octopus bro

1017. A Divide B (20)


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 :

Enter in 1 In turn, we give A and B, In the middle to 1 The blank space to separate .

Output format :

stay 1 Output in turn Q and R, In the middle to 1 The blank space to separate .

sample input :
123456789050987654321 7
sample output :
17636684150141093474 3


Ideas : You can only convert a string bit by bit into a number . The dividend is converted from the left side of the string to a digit divided by the divisor , If there is a remainder multiplied by 10 Save for the next , Otherwise zero , Remainder multiplication 10 Save for the next . Note that if the first divisor is less than the divisor, zero cannot be output , Only when there is a non-zero output can it be output 0

One 、 Starting variable

1. Divisor divider, Divisor dibideder, Each remainder left

2.flag Sign a , Non zero output used to mark whether there is quotient

Two 、 operation

1. Read the dividend and divisor .

2. stay for In the loop, the divisor is converted bit by bit , Convert one digit divided by the divisor , If not zero output , If zero , Output on the premise that there is already a non-zero quotient output , Otherwise, it will not output .

3. If there is a remainder multiplied by 10 Save for the next

4. if flag Always for 0, Then the dividend is less than the divisor , Output 0

3、 ... and 、 Code

#include "stdio.h"
#include "string.h"
int main()
{
	char divideder[1001];
	int divider;
	int flag = 0;// Whether there is a non-zero quotient output , Before there is no non-zero output 0 No output ; 
	int i;
	int left = 0;// The remainder after each division ; 
	scanf("%s %d", divideder, ÷r);
	int len = strlen(divideder);
	// If the divisor is 0, Direct results ; 
	for(i = 0; i < len; i++)
	{
		
		left = left*10 + divideder[i] - '0';
		
		if(left >= divider)
		{
			printf("%d",left / divider);
			flag = 1;// export merchant , And change flag Value ,flag Is a flag indicating whether a quotient of a bit is output  
		}
		else if(flag)
		{
			printf("0");// After the first output of the quotient , And the third of the divisor i Bit less than divisor , The output 0; 
		}
		left = left % divider;// Get the remainder  
		
	}
	if(flag == 0)
	{
		printf("0");
	}// The divisor has only one digit and is less than the divisor ; 
	printf(" %d",left);
	return 0;
}


原网站

版权声明
本文为[Octopus bro]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230410348025.html