usaco Chapter 2 section 2.4 Fractions to Decimals

本文介绍了一个C++程序,用于将分数转换为十进制小数形式,并能够正确处理无限循环小数的情况。程序读取输入文件中的两个整数(分子和分母),计算其商并判断是否为无限循环小数,最后将结果输出到指定文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 /*
ID: niepeng1
LANG: C++
TASK:fracdec
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <ctype.h>
//#include <iostream.h>
#define Max1 1001000


char s[Max1]={" "};
int c[Max1]={0};
bool cond[Max1]={false};

int main()
{
 int a,b,tem,tem1,j,i=0,point=0,k=0,a1;
 bool cond1=false;
 FILE *in,*out;
 in=fopen("fracdec.in","r");
 out=fopen("fracdec.out","w");
 fscanf(in,"%d %d",&a1,&b);
 {
  a=a1;
  tem=a/b;
  a = a-tem*b;
  fprintf(out,"%d",tem);
  if(tem==0)
   k=1;
  else while(tem>0){tem/=10;k++;}
 }
// s[i++]='.';
 fprintf(out,".");k++;
 if( a == 0){
  fprintf(out,"0/n");
  return 0;
 }
 a = a*10;
 for(;;){
  cond[a]=true;
  tem = a/b;
  tem1=a-tem*b;
  c[i] =a;
  s[i]=tem+'0';
  i++;

  if( tem1 == 0 ){
   for(j=0;j<i;j++,k++){
    if( (k+1)%76==0)
    {
     fprintf(out,"/n");
    }
    fprintf(out,"%c",s[j]);

   }
   fprintf(out,"/n");
   return 0;
  }
  else{
   if( cond[tem1*10] == true){
    point=tem1*10;
    if(point == (a1- (a1/b)*b)*10)
    {
     fprintf(out,"(");
     cond1=true;
    }
    for(j=0;j<i;j++,k++){
     if(c[j]==point&&cond1==false)//j <i-1
     {
      fprintf(out,"(");
      cond1=true;
     }
     if( (k+1)%76==0)
     {
      fprintf(out,"/n");
     }
     fprintf(out,"%c",s[j]);

     
    }
    fprintf(out,")/n");
    return 0;
   }
   

  }
  a=tem1*10;
  
 }
 fclose(in);
 fclose(out);
 

 return 0;
}

 
/*处于无限拍错中,就是把测试错误结果返回来才认真看代码,哎,太粗心了。他妈的眼都看晕了。*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值