Problem Description
Zty很痴迷数学问题.。一天,yifenfei出了个数学题想难倒他,让他回答1 / n。但Zty却回答不了_. 请大家编程帮助他.
Input
第一行整数T,表示测试组数。后面T行,每行一个整数 n (1<=|n|<=10^5).
Output
输出1/n. (是循环小数的,只输出第一个循环节).
Sample Input
4
2
3
7
168
Sample Output
0.5
0.3
0.142857
0.005952380
思路:
这题就是除法模拟
手写一下1/n(n自己随便取一下)
然后就会写了
开mark数组标记每次的结果,如果结果重复出现说明出现了循环
code:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#define ll long long
using namespace std;
const int maxm=1e5+5;
int mark[maxm];
void solve(int n){
memset(mark,0,sizeof mark);
int now=1;
mark[now]=1;
while(now){//now=0的时候说明整除了就不继续进入循环
now*=10;
printf("%d",now/n);
now%=n;
if(mark[now])return ;
mark[now]=1;
}
}
int main(){
int T;
scanf("%d",&T);
while(T--){
int n;
scanf("%d",&n);
if(n<0){//负数改正数
printf("-");
n=-n;
}
if(n==1){//特判1
puts("1");
continue;
}
printf("0.");
solve(n);
puts("");
}
return 0;
}