模拟竖式除法。最好在纸上写一写,111/13,1111/13,1111/13。每次结果的余数后面加一个1就相当于是在被除数后面加了一个1.
还有,a数组要开的足够大。

#include <bits/stdc++.h>
using namespace std;
int main(){
	int n,single =1;
	cin>>n;
	int cnt=1;//1有多少位 
	while(single<n){
		single=single*10+1;
		cnt++;
	}
	int a[100],rest;//最终的结果 
	int idx=0;
	a[idx++]=single/n;
	rest=single%n;
	while(true){
		//是否能够整除
        if(rest==0) break;
        single=rest*10+1;
        cnt++;
        a[idx++]=single/n;
        rest=single%n;
	}
	for(int i=0;i<idx;i++){
		cout<<a[i];
		if(i==idx-1) cout<<" ";
	}
	cout << cnt<<'\n';
	return 0;
}
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。