{"id":1100,"date":"2009-11-24T11:30:51","date_gmt":"2009-11-24T03:30:51","guid":{"rendered":"http:\/\/www.slyar.com\/blog\/?p=1100"},"modified":"2022-07-03T15:47:02","modified_gmt":"2022-07-03T22:47:02","slug":"poj-1650-cpp","status":"publish","type":"post","link":"http:\/\/www.slyar.com\/blog\/poj-1650-cpp.html","title":{"rendered":"POJ 1650 Integer Approximation C++\u7248"},"content":{"rendered":"<p>\u6587\u7ae0\u4f5c\u8005\uff1a<span style=\"color: #0000ff;\">\u59dc\u5357(Slyar) <\/span>\u6587\u7ae0\u6765\u6e90\uff1aSlyar Home (<a title=\"Slyar\" href=\"http:\/\/www.slyar.com\" target=\"_blank\" rel=\"noopener\">www.slyar.com<\/a>) \u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff0c\u8c22\u8c22\u5408\u4f5c\u3002<\/p>\n<p>\u66b4\u529b\u679a\u4e3e<\/p>\n<p><!--more--><\/p>\n<p><span style=\"color: #0000ff;\">Description<\/span><\/p>\n<p>The FORTH programming language does not support floating-point arithmetic at all. Its author, Chuck Moore, maintains that floating-point calculations are too slow and most of the time can be emulated by integers with proper scaling. For example, to calculate the area of the circle with the radius R he suggests to use formula like R * R * 355 \/ 113, which is in fact surprisingly accurate. The value of 355 \/ 113 \u2248 3.141593 is approximating the value of PI with the absolute error of only about 2*10-7. You are to find the best integer approximation of a given floating-point number A within a given integer limit L. That is, to find such two integers N and D (1 &lt;= N, D &lt;= L) that the value of absolute error |A - N \/ D| is minimal.<\/p>\n<p><span style=\"color: #0000ff;\">Input<\/span><\/p>\n<p>The first line of input contains a floating-point number A (0.1 &lt;= A &lt; 10) with the precision of up to 15 decimal digits. The second line contains the integer limit L. (1 &lt;= L &lt;= 100000).<\/p>\n<p><span style=\"color: #0000ff;\">Output<\/span><\/p>\n<p>Output file must contain two integers, N and D, separated by space.<\/p>\n<p><span style=\"color: #0000ff;\">Sample Input<\/span><\/p>\n<p>3.14159265358979<br \/>\n10000<\/p>\n<p><span style=\"color: #0000ff;\">Sample Output<\/span><\/p>\n<p>355 113<\/p>\n<p><span style=\"color: #ff0000;\">Slyar:\u6c42\u51fa\u5728L\u8303\u56f4\u5185\u7684\u4e24\u4e2a\u6570\uff0c\u4f7f\u5f97\u5b83\u4eec\u76f8\u9664\u540e\u51cf\u53bbA\u7684\u7ed3\u679c\u7684\u7edd\u5bf9\u503c\u6700\u5c0f\u3002\u66b4\u529b\u679a\u4e3e\u5c31\u597d\u4e86\uff0c\u8fd9\u9898\u5f88\u6076\u5fc3...<\/span><\/p>\n<pre lang=\"c\" line=\"1\">\r\n#include <iostream>\r\n#include <cmath>\r\n\r\nusing namespace std;\r\n\r\nint main()\r\n{\r\n    double a, min, temp;\r\n    int i, j, l, n, d;\r\n    \r\n    cin >> a >> l;\r\n    n = d = 1;\r\n    min = 20;\r\n\r\n    for (i = 1; i <= l; i++)\r\n    {\r\n        j = (int)(i \/ a);\r\n        \r\n        if (j > l) continue;\r\n        temp = fabs(a - (double)i \/ (double)j);\r\n        if (temp < min)\r\n        {\r\n            min = temp;\r\n            n = i;\r\n            d = j;\r\n        }\r\n        \r\n        j++;\r\n        if (j > l) continue;\r\n        temp = fabs(a - (double)i \/ (double)j);\r\n        if (temp < min)\r\n        {\r\n            min = temp;\r\n            n = i;\r\n            d = j;\r\n        }\r\n    }\r\n    \r\n    cout << n << \" \" << d << endl;\r\n    \r\n    \/\/system(\"pause\");\r\n    return 0;\r\n}\r\n<\/pre>\n<p>\u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1a<a href=\"http:\/\/www.slyar.com\/blog\">Slyar Home<\/a> &raquo; <a href=\"http:\/\/www.slyar.com\/blog\/poj-1650-cpp.html\">POJ 1650 Integer Approximation C++\u7248<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u4f5c\u8005\uff1a\u59dc\u5357(Slyar) \u6587\u7ae0\u6765\u6e90\uff1aSlyar Home (www.slyar.com) \u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff0c\u8c22\u8c22\u5408\u4f5c\u3002 \u66b4\u529b\u679a\u4e3e \u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1aSlyar Home &raquo; POJ 1650 Integer Approximation C++\u7248<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1103],"tags":[1006],"class_list":["post-1100","post","type-post","status-publish","format-standard","hentry","category-acm-oj-solution","tag-poj"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.2.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"POJ 1650 Integer Approximation C++\u7248\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Slyar\"\/>\n\t<meta name=\"keywords\" content=\"slyar,poj,pku,1650,integer approximation,c++,\u89e3\u9898\u62a5\u544a,\u9898\u89e3\" \/>\n\t<link rel=\"canonical\" href=\"http:\/\/www.slyar.com\/blog\/poj-1650-cpp.html\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.2.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"zh_CN\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Slyar Home | \u8bb0\u5f55\u4e2a\u4eba\u751f\u6d3b\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"POJ 1650 Integer Approximation C++\u7248 - Slyar Home\" \/>\n\t\t<meta property=\"og:description\" content=\"POJ 1650 Integer Approximation C++\u7248\" \/>\n\t\t<meta property=\"og:url\" content=\"http:\/\/www.slyar.com\/blog\/poj-1650-cpp.html\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2009-11-24T03:30:51+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2022-07-03T22:47:02+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"POJ 1650 Integer Approximation C++\u7248 - Slyar Home\" \/>\n\t\t<meta name=\"twitter:description\" content=\"POJ 1650 Integer Approximation C++\u7248\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\\\/\\\/www.slyar.com\\\/blog\\\/poj-1650-cpp.html#article\",\"name\":\"POJ 1650 Integer Approximation C++\\u7248 - Slyar Home\",\"headline\":\"POJ 1650 Integer Approximation C++\\u7248\",\"author\":{\"@id\":\"http:\\\/\\\/www.slyar.com\\\/blog\\\/author\\\/slyar#author\"},\"publisher\":{\"@id\":\"http:\\\/\\\/www.slyar.com\\\/blog\\\/#person\"},\"image\":{\"@type\":\"ImageObject\",\"@id\":\"http:\\\/\\\/www.slyar.com\\\/blog\\\/poj-1650-cpp.html#articleImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/fa995532c4fa6a1a16430db283689338da430a677b2ff974e0cad68e3bba05c4?s=96&d=monsterid\",\"width\":96,\"height\":96,\"caption\":\"Slyar\"},\"datePublished\":\"2009-11-24T11:30:51-08:00\",\"dateModified\":\"2022-07-03T15:47:02-07:00\",\"inLanguage\":\"zh-CN\",\"mainEntityOfPage\":{\"@id\":\"http:\\\/\\\/www.slyar.com\\\/blog\\\/poj-1650-cpp.html#webpage\"},\"isPartOf\":{\"@id\":\"http:\\\/\\\/www.slyar.com\\\/blog\\\/poj-1650-cpp.html#webpage\"},\"articleSection\":\"OJ\\u9898\\u89e3, POJ\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\\\/\\\/www.slyar.com\\\/blog\\\/poj-1650-cpp.html#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"http:\\\/\\\/www.slyar.com\\\/blog\\\/#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\\\/\\\/www.slyar.com\\\/blog\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"http:\\\/\\\/www.slyar.com\\\/blog\\\/category\\\/acm-oj-solution#listItem\",\"name\":\"OJ\\u9898\\u89e3\"}},{\"@type\":\"ListItem\",\"@id\":\"http:\\\/\\\/www.slyar.com\\\/blog\\\/category\\\/acm-oj-solution#listItem\",\"position\":2,\"name\":\"OJ\\u9898\\u89e3\",\"item\":\"http:\\\/\\\/www.slyar.com\\\/blog\\\/category\\\/acm-oj-solution\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"http:\\\/\\\/www.slyar.com\\\/blog\\\/poj-1650-cpp.html#listItem\",\"name\":\"POJ 1650 Integer Approximation C++\\u7248\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"http:\\\/\\\/www.slyar.com\\\/blog\\\/#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"http:\\\/\\\/www.slyar.com\\\/blog\\\/poj-1650-cpp.html#listItem\",\"position\":3,\"name\":\"POJ 1650 Integer Approximation C++\\u7248\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"http:\\\/\\\/www.slyar.com\\\/blog\\\/category\\\/acm-oj-solution#listItem\",\"name\":\"OJ\\u9898\\u89e3\"}}]},{\"@type\":\"Person\",\"@id\":\"http:\\\/\\\/www.slyar.com\\\/blog\\\/#person\",\"name\":\"Slyar\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"http:\\\/\\\/www.slyar.com\\\/blog\\\/poj-1650-cpp.html#personImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/fa995532c4fa6a1a16430db283689338da430a677b2ff974e0cad68e3bba05c4?s=96&d=monsterid\",\"width\":96,\"height\":96,\"caption\":\"Slyar\"}},{\"@type\":\"Person\",\"@id\":\"http:\\\/\\\/www.slyar.com\\\/blog\\\/author\\\/slyar#author\",\"url\":\"http:\\\/\\\/www.slyar.com\\\/blog\\\/author\\\/slyar\",\"name\":\"Slyar\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"http:\\\/\\\/www.slyar.com\\\/blog\\\/poj-1650-cpp.html#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/fa995532c4fa6a1a16430db283689338da430a677b2ff974e0cad68e3bba05c4?s=96&d=monsterid\",\"width\":96,\"height\":96,\"caption\":\"Slyar\"}},{\"@type\":\"WebPage\",\"@id\":\"http:\\\/\\\/www.slyar.com\\\/blog\\\/poj-1650-cpp.html#webpage\",\"url\":\"http:\\\/\\\/www.slyar.com\\\/blog\\\/poj-1650-cpp.html\",\"name\":\"POJ 1650 Integer Approximation C++\\u7248 - Slyar Home\",\"description\":\"POJ 1650 Integer Approximation C++\\u7248\",\"inLanguage\":\"zh-CN\",\"isPartOf\":{\"@id\":\"http:\\\/\\\/www.slyar.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"http:\\\/\\\/www.slyar.com\\\/blog\\\/poj-1650-cpp.html#breadcrumblist\"},\"author\":{\"@id\":\"http:\\\/\\\/www.slyar.com\\\/blog\\\/author\\\/slyar#author\"},\"creator\":{\"@id\":\"http:\\\/\\\/www.slyar.com\\\/blog\\\/author\\\/slyar#author\"},\"datePublished\":\"2009-11-24T11:30:51-08:00\",\"dateModified\":\"2022-07-03T15:47:02-07:00\"},{\"@type\":\"WebSite\",\"@id\":\"http:\\\/\\\/www.slyar.com\\\/blog\\\/#website\",\"url\":\"http:\\\/\\\/www.slyar.com\\\/blog\\\/\",\"name\":\"Slyar Home\",\"description\":\"\\u8bb0\\u5f55\\u4e2a\\u4eba\\u751f\\u6d3b\",\"inLanguage\":\"zh-CN\",\"publisher\":{\"@id\":\"http:\\\/\\\/www.slyar.com\\\/blog\\\/#person\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"POJ 1650 Integer Approximation C++\u7248 - Slyar Home","description":"POJ 1650 Integer Approximation C++\u7248","canonical_url":"http:\/\/www.slyar.com\/blog\/poj-1650-cpp.html","robots":"max-image-preview:large","keywords":"slyar,poj,pku,1650,integer approximation,c++,\u89e3\u9898\u62a5\u544a,\u9898\u89e3","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/www.slyar.com\/blog\/poj-1650-cpp.html#article","name":"POJ 1650 Integer Approximation C++\u7248 - Slyar Home","headline":"POJ 1650 Integer Approximation C++\u7248","author":{"@id":"http:\/\/www.slyar.com\/blog\/author\/slyar#author"},"publisher":{"@id":"http:\/\/www.slyar.com\/blog\/#person"},"image":{"@type":"ImageObject","@id":"http:\/\/www.slyar.com\/blog\/poj-1650-cpp.html#articleImage","url":"https:\/\/secure.gravatar.com\/avatar\/fa995532c4fa6a1a16430db283689338da430a677b2ff974e0cad68e3bba05c4?s=96&d=monsterid","width":96,"height":96,"caption":"Slyar"},"datePublished":"2009-11-24T11:30:51-08:00","dateModified":"2022-07-03T15:47:02-07:00","inLanguage":"zh-CN","mainEntityOfPage":{"@id":"http:\/\/www.slyar.com\/blog\/poj-1650-cpp.html#webpage"},"isPartOf":{"@id":"http:\/\/www.slyar.com\/blog\/poj-1650-cpp.html#webpage"},"articleSection":"OJ\u9898\u89e3, POJ"},{"@type":"BreadcrumbList","@id":"http:\/\/www.slyar.com\/blog\/poj-1650-cpp.html#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"http:\/\/www.slyar.com\/blog\/#listItem","position":1,"name":"Home","item":"http:\/\/www.slyar.com\/blog\/","nextItem":{"@type":"ListItem","@id":"http:\/\/www.slyar.com\/blog\/category\/acm-oj-solution#listItem","name":"OJ\u9898\u89e3"}},{"@type":"ListItem","@id":"http:\/\/www.slyar.com\/blog\/category\/acm-oj-solution#listItem","position":2,"name":"OJ\u9898\u89e3","item":"http:\/\/www.slyar.com\/blog\/category\/acm-oj-solution","nextItem":{"@type":"ListItem","@id":"http:\/\/www.slyar.com\/blog\/poj-1650-cpp.html#listItem","name":"POJ 1650 Integer Approximation C++\u7248"},"previousItem":{"@type":"ListItem","@id":"http:\/\/www.slyar.com\/blog\/#listItem","name":"Home"}},{"@type":"ListItem","@id":"http:\/\/www.slyar.com\/blog\/poj-1650-cpp.html#listItem","position":3,"name":"POJ 1650 Integer Approximation C++\u7248","previousItem":{"@type":"ListItem","@id":"http:\/\/www.slyar.com\/blog\/category\/acm-oj-solution#listItem","name":"OJ\u9898\u89e3"}}]},{"@type":"Person","@id":"http:\/\/www.slyar.com\/blog\/#person","name":"Slyar","image":{"@type":"ImageObject","@id":"http:\/\/www.slyar.com\/blog\/poj-1650-cpp.html#personImage","url":"https:\/\/secure.gravatar.com\/avatar\/fa995532c4fa6a1a16430db283689338da430a677b2ff974e0cad68e3bba05c4?s=96&d=monsterid","width":96,"height":96,"caption":"Slyar"}},{"@type":"Person","@id":"http:\/\/www.slyar.com\/blog\/author\/slyar#author","url":"http:\/\/www.slyar.com\/blog\/author\/slyar","name":"Slyar","image":{"@type":"ImageObject","@id":"http:\/\/www.slyar.com\/blog\/poj-1650-cpp.html#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/fa995532c4fa6a1a16430db283689338da430a677b2ff974e0cad68e3bba05c4?s=96&d=monsterid","width":96,"height":96,"caption":"Slyar"}},{"@type":"WebPage","@id":"http:\/\/www.slyar.com\/blog\/poj-1650-cpp.html#webpage","url":"http:\/\/www.slyar.com\/blog\/poj-1650-cpp.html","name":"POJ 1650 Integer Approximation C++\u7248 - Slyar Home","description":"POJ 1650 Integer Approximation C++\u7248","inLanguage":"zh-CN","isPartOf":{"@id":"http:\/\/www.slyar.com\/blog\/#website"},"breadcrumb":{"@id":"http:\/\/www.slyar.com\/blog\/poj-1650-cpp.html#breadcrumblist"},"author":{"@id":"http:\/\/www.slyar.com\/blog\/author\/slyar#author"},"creator":{"@id":"http:\/\/www.slyar.com\/blog\/author\/slyar#author"},"datePublished":"2009-11-24T11:30:51-08:00","dateModified":"2022-07-03T15:47:02-07:00"},{"@type":"WebSite","@id":"http:\/\/www.slyar.com\/blog\/#website","url":"http:\/\/www.slyar.com\/blog\/","name":"Slyar Home","description":"\u8bb0\u5f55\u4e2a\u4eba\u751f\u6d3b","inLanguage":"zh-CN","publisher":{"@id":"http:\/\/www.slyar.com\/blog\/#person"}}]},"og:locale":"zh_CN","og:site_name":"Slyar Home | \u8bb0\u5f55\u4e2a\u4eba\u751f\u6d3b","og:type":"article","og:title":"POJ 1650 Integer Approximation C++\u7248 - Slyar Home","og:description":"POJ 1650 Integer Approximation C++\u7248","og:url":"http:\/\/www.slyar.com\/blog\/poj-1650-cpp.html","article:published_time":"2009-11-24T03:30:51+00:00","article:modified_time":"2022-07-03T22:47:02+00:00","twitter:card":"summary","twitter:title":"POJ 1650 Integer Approximation C++\u7248 - Slyar Home","twitter:description":"POJ 1650 Integer Approximation C++\u7248"},"aioseo_meta_data":{"post_id":"1100","title":"POJ 1650 Integer Approximation C++\u7248 - #site_title","description":"POJ 1650 Integer Approximation C++\u7248","keywords":[{"label":"slyar","value":"slyar"},{"label":"poj","value":"poj"},{"label":"pku","value":"pku"},{"label":"1650","value":"1650"},{"label":"Integer Approximation","value":"Integer Approximation"},{"label":"c++","value":"c++"},{"label":"\u89e3\u9898\u62a5\u544a","value":"\u89e3\u9898\u62a5\u544a"},{"label":"\u9898\u89e3","value":"\u9898\u89e3"}],"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2021-02-10 02:44:04","updated":"2022-10-18 14:09:01","seo_analyzer_scan_date":null,"focus_keyword":null,"additional_keywords":null,"truseo_locale":null},"_links":{"self":[{"href":"http:\/\/www.slyar.com\/blog\/wp-json\/wp\/v2\/posts\/1100","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.slyar.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.slyar.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.slyar.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.slyar.com\/blog\/wp-json\/wp\/v2\/comments?post=1100"}],"version-history":[{"count":1,"href":"http:\/\/www.slyar.com\/blog\/wp-json\/wp\/v2\/posts\/1100\/revisions"}],"predecessor-version":[{"id":8288,"href":"http:\/\/www.slyar.com\/blog\/wp-json\/wp\/v2\/posts\/1100\/revisions\/8288"}],"wp:attachment":[{"href":"http:\/\/www.slyar.com\/blog\/wp-json\/wp\/v2\/media?parent=1100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.slyar.com\/blog\/wp-json\/wp\/v2\/categories?post=1100"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.slyar.com\/blog\/wp-json\/wp\/v2\/tags?post=1100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}