PTA brushing notes (C language) | 7-42 to eliminate bachelor (20 points)

Start brushing questions, I feel that the amount of code and the foundation are too scum, resulting in the data structure will not, slowly brush it ~
Insert picture description here


1. Title

Insert picture description here
Sample input:

31

Sample output:

3584229390681 15


2. Code

#include<stdio.h>
int main(){
	int x;
	scanf("%d",&x);
	int cnt=0;
	int a=0,c=0,d;
	while(a<x){
		a=a*10+1;
		cnt++;
	}
	while(1){
		printf("%d",a/x);
		a%=x;
		if(a==0) break;
		a=a*10+1;
		cnt++;
	}
	printf(" %d",cnt);
	
	return 0;
}

Insert picture description here

3 Discussion

This topic is very interesting, ha, would have thought that a "simple" question, with the continuous circulation by 10 plus 1, and sure enough there are two testtimed out, then there would be no ,,, then the.

#include<stdio.h>
#include<math.h>
int main(){
	int x;
	scanf("%d",&x);
	int i;
	int cnt=0;
	long long int a=0,c=0,d;
	while(1){
		a=a*10+1;
		cnt++;
		c=a/x;
		d=a%x;
		if(d==0){
			printf("%lld %d",c,cnt);
			break;
		}
	}
//	printf("%lld %d",c,cnt);
	
	return 0;
}

Insert picture description here
When I searched the Internet, I discovered a kinky trick, which is to simulate the operation of division, that is:
Insert picture description here
What does it mean?

That is, only a part of the bachelor number is generated first, 111 in the example, and then the division is performed, which is equivalent to calculating the result from the highest bit (the handwritten division operation step). When the remainder is not 0, add 1 at the end of the remainder (not Addition of addition, but the meaning of patchwork, that is, 1 "addition" 1 is 11).

Wonderful! ! !
Insert picture description here
Insert picture description here

Published 257 original articles · praised 5220 · 880,000 views

Guess you like

Origin blog.csdn.net/TeFuirnever/article/details/105539243