模拟除法

#include <iostream>
#include <string.h>
using namespace std;

int main()
{
    int n;
    cin >> n;
    int len=0;
    int p=0;
    char str[1001];
    int m = 1;
    memset(str, '\0', sizeof(str));
    while(true)
    {
        if(p||m / n)
        {
            str[p++] = '0' + m / n;  //将得到的商放进字符串

        }
        len++;
        m = m % n;  //对除数求余,拿到下一次需要用的被除数,如果为0,说明已经整出,可以试着用笔进行除法运算,如11111111111除32来观察一下,很好理解
        if(m%n==0)
        {
            cout << str << " " << len;
                break;
        }
        m = m * 10 + 1;
    }
    return 0;
}

 

posted @ 2017-08-10 19:41  diamondDemand  阅读(497)  评论(0编辑  收藏  举报