Ordered Fractions 运行超时啊~

#include<fstream>
#include<iostream>
#include<string>
#include<math.h>
using namespace std;
int myitoa(int data, string* p, int num)  
{  
    if (p == NULL)  
    {  
        return -1;  
    }  
    if (data < 0)  
    {  
        *p++ = '-';  
        data = 0 - data;  
    }  
    int temp = 0;  
    int flag = 0; //标志位 0-不存储 1-存储   
    if (num == 10)  
    {//十进制   
        for (int i = 0; i < 10; i++)  
        {  
            temp = static_cast<int>(data / pow(10.0, 9-i));// pow(i,j),求i的j次方,temp取得当前最高位   
            if (temp != 0)  //去掉最前面的0   
            {  
                flag = 1;//将标志位变为1,可以存储   
            }  
            if (flag != 0)  
            {  
                //*p++ = temp + '0';  //变成字符
				*p+= temp + '0';  //变成字符   
                data = data % static_cast<int>(pow(10.0, 9-i));  
            }  
        }  
    }  
} 
bool hasgy(int i,int j)
{
	for(int k=2;k<=i;k++)
	{
		if(i%k==0 && j%k==0)
			return false;
	}
	return true;
}
int main()
{
	ifstream fin ("frac1.in");
	ofstream fout ("frac1.out");
	int n,i,j,k=0;
	float a[10000];
	string zi[20000];
	string mu[20000];
	fin>>n;
	for(i=2;i<=n;i++)//i:分母
	{
		for(j=1;j<i;j++)//j:分子
		{
			if((hasgy(i,j))&&(i%j!=0 || j==1))//保证既约
			{
				a[k]=(float)j/i;
				myitoa(j,zi+k,10);
				myitoa(i,mu+k,10);
				k++;
			}
		}
	}
	for(i=0;i<k;i++)
	{
		for(j=i+1;j<k;j++)
		{
			if(a[i]>a[j])
			{
				float temp;
				temp=a[i];
				a[i]=a[j];
				a[j]=temp;
				string tempc;
				tempc=zi[i];
				zi[i]=zi[j];
				zi[j]=tempc;
				tempc=mu[i];
				mu[i]=mu[j];
				mu[j]=tempc;
			}
		}
	}
	fout<<"0/1"<<endl;
	for(i=0;i<k;i++)
		fout<<zi[i]<<'/'<<mu[i]<<endl;
	fout<<"1/1"<<endl;
	return 0;
}

阅读更多

Continuous Fractions

02-09

DescriptionnnA simple continuous fraction has the form:n![](http://poj.org/images/3197_1.gif)nwhere the ai’s are integer numbers.nnThe previous continuous fraction could be noted as [a1, a2, …, an]. It is not difficult to show that any rational number p⁄q, with integers p > q > 0, can be represented in a unique way by a simple continuous fraction with n terms, such that p⁄q = [a1, a2, …, an−1, 1], where n and the ai’s are positive natural numbers.nnYour task is to find and print the simple continuous fraction that corresponds to a given rational number.nnInputnnInput will consist of a series of cases, each one in a line. A line describing a case contains p and q, two integer numbers separated by a space, with 1020 > p > q > 0.nnThe end of the input is indicated by a line containing 0 0.nnOutputnnCases must be analyzed in the order that are read from the input. Output for each case will consist of several lines. The first line indicates the case number, starting at 1, using the format:nnCase i:nnreplacing i by the corresponding case number. The second line displays the input data in the form p / q.nnThe remaining lines must contain the continuous fraction corresponding to the rational number, p⁄q, specified in the given input line. The continuous fraction must be printed accordingly to the following rules:nn Horizontal bars are formed by sequences of dashes ‘-’.n The width of each horizontal bar is exactly equal to the width of the denominator under it.n Blank characters should be printed using periods ‘.’.n The number on a fraction numerator must be printed center justified. That is, the number of spaces at either side must be same, if possible; in other case, one more space must be added at the right side.nnSample Inputnn75 34n65 60n0 0nnSample OutputnnCase 1:n75 / 34n..........1......n2.+.-------------n............1....n....4.+.---------n..............1..n........1.+.-----n................1n............5.+.-n................1nCase 2:n65 / 60n......1...n1.+.------n.........1n....11.+.-n.........1

没有更多推荐了,返回首页