给定一个正整数n
是在P/Q甲酸号码,就必须打印升序其中由0之间的级分至1如何排序用C++
*输入 - 6的序列。
输出-0/1,1/6,1/5,1/4,1/3,2/5,1/2,3/6,3/5,2/3,3/4 ,4/5,5/6,1/1。
我在C编写的代码++但不给正确的输出
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<set>
using namespace std;
long gcd(long a, long b);
void foo(double input)
{
double frac = input ;
const long precision = 1000000000; // This is the accuracy.
long gcd_ = gcd(round(frac * precision), precision);
long denominator = precision/gcd_;
long numerator = round(frac * precision)/gcd_;
cout << numerator << "/" << denominator <<",";
}
long gcd(long a, long b){
if (a == 0)
return b;
else if (b == 0)
return a;
if (a < b)
return gcd(a, b % a);
else
return gcd(b, a % b);
}
int main()
{
double n;
set<double>s;
int c=0;
cin>>n;
for(int i=1;i<n;i++)
{
for(int j=n;j>0;j--)
{
if(i<j)
{
s.insert((double)i/j);
}
}
}
cout<<"0/1"<<",";
while(!s.empty())
{
foo(*s.begin());
s.erase(s.begin());
}
cout<<"1/1";
输出 - 0/1,1000000000分之166666667,1/5,1/4,333333333 /十亿,2/5, 1/2,3/5,666666667/1000000000,3/4,4/5,833333333/1000000000,1/1
*做*您得到什么输出?你有没有使用调试器来遍历代码,并试图找出它是否符合你的期望? –
我得到产量-0/1,166666667/1000000000,1/5,1/4,333333333/1000000000,2/5,1/2,3/5,666666667/1000000000,3/4,4/5, 833333333/1000000000,1/1这里输入= 6 – govindgeek