UVa 10976 Fractions Again 枚舉

枚舉y從2k到k,,注意精度

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std ;

const double eps = 1e-8 ;

int main() {
	double k ;
	vector<pair<int ,int> > vct ;
	while(scanf("%lf",&k)==1) {
        vct.resize(0) ;
		for(double j = 2*k ; j >= k ; --j) {
			double tmp = k*j/(j-k) ;//注意精度,不能寫成1/(1/k-1/j) ;
			if(tmp-(int)tmp < eps) {//判斷是否爲整數
			   vct.push_back(make_pair((int)tmp , (int)j)) ;
			}
		}
		printf("%d\n" , vct.size()) ;
		for(int i= vct.size() ; i > 0 ; --i) {
			pair<int ,int> pp = vct[i-1] ;
			printf("1/%d = 1/%d + 1/%d\n" ,(int)k , pp.first , pp.second) ;
		}
	}
}


發佈了48 篇原創文章 · 獲贊 5 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章