如何在c ++中cout不可减少的分数,如1/2

时间:2017-11-01 20:30:58

标签: c++ math

我正在尝试解决代码中的problem并且输出必须采用格式«A / B»我想输出1/2而不是0.5但我不知道如何输出这格式有什么帮助?

#include <iostream>
using namespace std;


    int main(){
        int A, B, big = 0, i = 1;
        double result = 0;
        cin>>A>>B;
        if(A == B)
            cout<<1/1<<endl;
        if(A > B)
            big = A;
        else
            big = B;

        for( i ; big != 6 ; i++){
            big++;
        }

        cout<<i/6.0<<endl;
        system("pause");
    }

1 个答案:

答案 0 :(得分:0)

您需要考虑的值列表如此之短,您可以将其作为一个案例:

#include <iostream>
using namespace std;

int main()
{
    int Y, W;

   cin>>Y>>W;

   switch( max(Y,W) )
   {
    case 1: cout << "1/1" << endl; break;
    case 2: cout << "5/6" << endl; break;
    case 3: cout << "2/3" << endl; break;
    case 4: cout << "1/2" << endl; break;
    case 5: cout << "1/3" << endl; break;
    case 6: cout << "1/6" << endl; break;
   }
   system("pause");
   return 0;
}