国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

1081. Rational Sum (20)

2019-11-08 01:47:03
字體:
來源:轉載
供稿:網友

用相減法求最大公約數時要注意開始的時候有個為0的情況

#include<iostream>#include<vector>#include<sstream>#PRagma warning(disable:4996)using namespace std;string chuli(string x, string y){ int a, b, c, d; sscanf(x.c_str(), "%d/%d", &a, &b); sscanf(y.c_str(), "%d/%d", &c,&d); int A = a*d + b*c, B = b*d; a = A > 0 ? A : -A;b = B > 0 ? B : -B; while (a != b) { if (a == 0) { a = b;break; } if (a > b) a = a - b; else b = b - a; } char temp[100]; sprintf(temp,"%d/%d", A / a, B / a); return string(temp);}int main(){ int N; cin >> N; string str; cin >> str; while (--N) { char a[100]; string str1; scanf("%s", a); str1 = a; str = chuli(str, str1); } int a, b; sscanf(str.c_str(), "%d/%d", &a, &b); if (a*b < 0)cout << "-"; a = a > 0 ? a : -a; b = b > 0 ? b:-b; if (a == 0) cout << "0"; if(a/b!=0) cout << a / b; if (a%b != 0) { if (a / b != 0) cout << " "; cout << a%b << "/" << b; } cout << endl;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 扶沟县| 平凉市| 舒兰市| 连山| 大埔区| 天祝| 浑源县| 游戏| 武城县| 红安县| 郴州市| 福清市| 乡宁县| 日喀则市| 麟游县| 霍山县| 焉耆| 丽江市| 固原市| 秦皇岛市| 华容县| 正定县| 锦州市| 石台县| 扎兰屯市| 弋阳县| 青岛市| 交城县| 玉溪市| 攀枝花市| 岗巴县| 繁峙县| 德江县| 兰州市| 张家港市| 称多县| 汶川县| 丰都县| 福州市| 新竹县| 周口市|