周测题 坤坤的难题(数学,技巧)

57 篇文章 1 订阅

坤坤的难题

Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 49   Accepted Submission(s) : 8
Font: Times New Roman | Verdana | Georgia
Font Size: ← →

Problem Description

坤坤在成功学习完A+B、A-B以及A*B之后,开始学习A/B,但是,结果会有循环小数,这下就难倒了坤坤,为了简化问题,我们假设A=1,请计算A/B

Input

第一行整数T,表示测试组数。后面T行,每行一个整数 B (1<=|B|<=10^5).

Output

输出A/B. (是循环小数的,只输出第一个循环节).

Sample Input

3
1
3
7

Sample Output

1
0.3
0.142857

Author

hpustudent

ac代码

#include<stdio.h>
#include<string.h>
int v[100010];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,mod;
		scanf("%d",&n);
		if(n==1)
		{
			printf("1\n");
			continue;
		}
		if(n==-1)
		{
			printf("-1\n");
			continue;
		}
		if(n<0)
		{
			printf("-0.");
			n=-n;
		}
		else
			printf("0.");
		memset(v,0,sizeof(v));
		v[1]=1;
		mod=1;
		while(1)
		{
			mod*=10;
			if(mod>=n)
			{
				printf("%d",mod/n);
				mod=mod%n;
			}
			else
				printf("0");
			
			if(mod==0)
				break;
			if(v[mod])
				break;
			v[mod]=1;
		}
		printf("\n");
	}
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值