star

搜索

RSS

RSS Link

计数器

140162
对于给定的N,求N!结果的位数
求一个数的二进制有多少个1?

高精度除法器

Star posted @ 2010年8月02日 02:41 in C语言 with tags c 数学 高精度 除法 , 4045 阅读
/*高精度除法器 v0.1 Author:star date:2010
*可以计算无限循环小数,循环部分用括号表示

*因为整数除法最终肯定会循环,所以输出是有穷的
*一些例子:
*1/3 = 0.(3)
*22/5 = 4.4
*1/7 = 0.(142857)
*2/2 = 1.0
*3/8 = 0.375
*输入格式:数字1+/字符+数字2+回车 
*输入例子:45/56
*输出结果:0.803(571428)
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include <memory.h>
#define  BIT 100000 //除数与被除数范围都为100000 

int main(void)
{
	int N, D, Mods[BIT+100];
	char Ans[BIT+100];
	int i, j, st, ed, mod, cnt, Flag, tp , bt;
	char PreChar[100];
NEXT:
	memset(Mods, 0, sizeof(Mods)); //初始化内存 
	memset(Ans,  0, sizeof(Ans));
	memset(PreChar, 0, sizeof(PreChar)); 

	scanf("%d/%d", &N, &D); //输入
  
	assert(N >= 1 && N <= BIT);  //输入限制的数字和范围 
	assert(D >= 1 && D <= BIT);
	Mods[N] = cnt = Flag = 1; //初始化 
	if(!(N % D)) //整除了直接输出
	{
		printf("%d.0\n", N/D);
		goto NEXT;
	}
	if(N < D) //计算小数点前
	{
		PreChar[0] = '0';
		PreChar[1] = '.';
	}
 	else
 	{
 		tp = N / D;
  		bt = log10(tp);
		PreChar[bt+1] = '.';
  		for(i = bt; i >= 0; --i)
  		{
   			PreChar[i] = tp % 10 + '0';
   			tp /= 10;
  		}
  		N %= D;
  		PreChar[i] = '.';
	}
 	i   = 0;
 	mod = 100009; //Magic Number
 	while (!Mods[mod]) {  //计算小数点后的除法
  		Mods[mod] = cnt;
  		++cnt;
  		N        *= 10;
  		Ans[i++]  = N / D + '0';
  		N =  mod  = N % D;
 	} 
 	//寻找开始循环与结束循环
 	ed = cnt - 1;
 	st = Mods[mod];
 	//整除清0
 	if(Ans[ed-1] == '0' && Mods[0])
 	{
  		Flag      = 0;
  		Ans[ed-1] = '\n';
 	}
 	//输出
 	printf("%s", PreChar);
 
 	for(j = 0,i = 1; i < st; ++i)
  		if(Ans[i-1])
  			printf("%c", Ans[i-1]);
 	if(Flag) 
  		printf("(");
 	for(j = i; Ans[j-1]; ++j)
  		printf("%c", Ans[j-1]);
 	if(Flag) 
  		printf(")\n");
 	goto NEXT;
}
subway surfers 说:
2019年6月01日 17:57

The information you have posted is very useful. The sites you have referred was good. Thanks for sharing

NCERT GK Sample Pape 说:
2022年9月28日 13:35

GK stands for General knowledge which was introduced as one of the subjects. It is beginning from the foundation education to help the students by getting minimum knowledge. GK also play a key role in competitive examinations, NCERT GK Sample Paper Class 2 that’s the way NCERT included General knowledge is a subject that everyone should know and learn from Primary Education.Downloading NCERT GK Sample Paper 2023 Class 2 utilizes candidates who have to know the new examination pattern and want to get high scores smoothly for all formats of exams conducted in Term-1, Term-2 such as SA-1, SA-2, FA-1, FA-2, FA-3, FA-4, Assignments and other types of exams to Hindi Medium, English medium and Urdu Medium students.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter