当前位置: 代码迷 >> 综合 >> [U]2.4.5 Fractions to Decimals 简单的语言控制题
  详细解决方案

[U]2.4.5 Fractions to Decimals 简单的语言控制题

热度:76   发布时间:2024-01-20 20:44:38.0

简单的语言控制,考察代码的熟悉程度。

代码比较丑陋,但是能ac......

/*
ID:sevenst4
LANG:C++
PROG:fracdec
*/
#include<stdio.h>
using namespace std;struct node
{int num;int left;
}p[100000];bool flag[100001];int main()
{freopen( "fracdec.in","r",stdin );freopen( "fracdec.out","w",stdout );int a,b;for( int i=0;i<100000;i++ )flag[i]=false;scanf( "%d %d",&a,&b );if( a%b==0 ){printf( "%d.0\n",a/b );return 0;}int head=0;if( a/b>=0 ) head++;if( a/b>=10 ) head++;if( a/b>=100 ) head++;if( a/b>=1000 ) head++;if( a/b>=10000 ) head++;if( a/b>=100000 ) head++;head++;printf( "%d.",a/b );a=a%b;flag[a]=true;int len=0;int index;bool loop=true;while( true ){a*=10; p[len].num=a/b;a=a%b; p[len].left=a;if( a==0 ) loop=false;len++;if( flag[a]==true ){index=a*10%b;break;}flag[a]=true;}for( int i=0;i<len;i++ ){if( (i+head)%76==75 )printf( "\n" );if( p[i].left!=index )printf( "%d",p[i].num );else{if( loop ){	 printf( "(" );for( ;i<len;i++ ){if( (i+head)%76==75 )printf( "\n" );printf( "%d",p[i].num );}printf( ")" );	}else{printf( "%d",p[i].num );break;}}}printf( "\n" );return 0;
}