{"id":172,"date":"2013-06-20T11:52:12","date_gmt":"2013-06-20T15:52:12","guid":{"rendered":"http:\/\/www.ferociouscoder.com\/blog\/?p=172"},"modified":"2013-07-13T02:49:05","modified_gmt":"2013-07-13T06:49:05","slug":"uva-10114-loansome-car-buyer","status":"publish","type":"post","link":"https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-10114-loansome-car-buyer-172.html","title":{"rendered":"UVA 10114 &#8211; Loansome Car Buyer"},"content":{"rendered":"<p>This problem was truly annoying and yet extremely simple at the same time. This has been ever so rightly starred in Competitive Programming 3 book. I messed up with an edge case and I wrote `break` instead of `continue` (big oops) in one of my many submissions. The problem mentions that we need to use files as input and output but that is not the case! That lost me a couple of tries as well :[<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport java.io.File;\r\nimport java.io.FileNotFoundException;\r\nimport java.io.PrintWriter;\r\nimport java.util.Scanner;\r\n\r\n\/**\r\n * \r\n * @author Sanchit M. Bhatnagar\r\n * @see http:\/\/uhunt.felix-halim.net\/id\/74004\r\n * \r\n *\/\r\npublic class P10114 {\r\n\r\n\tpublic static void main(String&#x5B;] args) throws FileNotFoundException {\r\n\t\tScanner sc = new Scanner(System.in);\r\n\t\tPrintWriter out = new PrintWriter(System.out);\r\n\r\n\t\twhile (true) {\r\n\t\t\tint duration = sc.nextInt();\r\n\t\t\tdouble downpayment = sc.nextDouble();\r\n\t\t\tdouble loan = sc.nextDouble();\r\n\t\t\tint depreciations = sc.nextInt();\r\n\t\t\tif (duration &lt; 0) {\r\n\t\t\t\tbreak;\r\n\t\t\t}\r\n\t\t\tdouble valOfCar = loan + downpayment;\r\n\t\t\tdouble monthlyPayment = loan \/ duration;\r\n\r\n\t\t\tint&#x5B;] depList = new int&#x5B;depreciations];\r\n\t\t\tdouble&#x5B;] depVal = new double&#x5B;depreciations];\r\n\t\t\tfor (int i = 0; i &lt; depreciations; i++) {\r\n\t\t\t\tdepList&#x5B;i] = sc.nextInt();\r\n\t\t\t\tdepVal&#x5B;i] = sc.nextDouble();\r\n\t\t\t}\r\n\r\n\t\t\tint lastMonth = 0;\r\n\t\t\tdouble lastVal = depVal&#x5B;0];\r\n\r\n\t\t\tvalOfCar *= (1 - lastVal);\r\n\r\n\t\t\tif (loan &lt; valOfCar) {\r\n\t\t\t\tout.println(&quot;0 months&quot;);\r\n\t\t\t\tcontinue;\r\n\t\t\t}\r\n\r\n\t\t\tlastMonth = 1;\r\n\r\n\t\t\tfor (int i = 1; i &lt; depList.length; i++) {\r\n\t\t\t\tint nextMonth = depList&#x5B;i];\r\n\t\t\t\tdouble nextMonthDepreciation = depVal&#x5B;i];\r\n\t\t\t\twhile (lastMonth &lt;= nextMonth) {\r\n\t\t\t\t\tif (nextMonth == lastMonth) {\r\n\t\t\t\t\t\tlastVal = nextMonthDepreciation;\r\n\t\t\t\t\t}\r\n\t\t\t\t\tvalOfCar *= (1 - lastVal);\r\n\t\t\t\t\tloan -= monthlyPayment;\r\n\t\t\t\t\tlastMonth++;\r\n\t\t\t\t\tif (loan &lt; valOfCar) {\r\n\t\t\t\t\t\tbreak;\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t\tif (loan &lt; valOfCar) {\r\n\t\t\t\t\tbreak;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\twhile (loan &gt; valOfCar) {\r\n\t\t\t\tvalOfCar *= (1 - lastVal);\r\n\t\t\t\tloan -= monthlyPayment;\r\n\t\t\t\tlastMonth++;\r\n\t\t\t}\r\n\r\n\t\t\tlastMonth--;\r\n\t\t\tif (lastMonth == 1) {\r\n\t\t\t\tout.println(&quot;1 month&quot;);\r\n\t\t\t} else {\r\n\t\t\t\tout.println(lastMonth + &quot; months&quot;);\r\n\t\t\t}\r\n\t\t}\r\n\t\tout.close();\r\n\t\tsc.close();\r\n\t}\r\n\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This problem was truly annoying and yet extremely simple at the same time. This has been ever so rightly starred in Competitive Programming 3 book. I messed up with an edge case and I wrote `break` instead of `continue` (big oops) in one of my many submissions. The problem mentions that we need to use &hellip; <a href=\"https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-10114-loansome-car-buyer-172.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">UVA 10114 &#8211; Loansome Car Buyer<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[18,19,17],"tags":[21,20,22],"class_list":["post-172","post","type-post","status-publish","format-standard","hentry","category-competitive-programming-3","category-java","category-uva-online-judge","tag-competitive-programming-3-2","tag-java-2","tag-uva-online-judge-2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>UVA 10114 - Loansome Car Buyer - Ferocious Coder<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-10114-loansome-car-buyer-172.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"UVA 10114 - Loansome Car Buyer - Ferocious Coder\" \/>\n<meta property=\"og:description\" content=\"This problem was truly annoying and yet extremely simple at the same time. This has been ever so rightly starred in Competitive Programming 3 book. I messed up with an edge case and I wrote `break` instead of `continue` (big oops) in one of my many submissions. The problem mentions that we need to use &hellip; Continue reading UVA 10114 &#8211; Loansome Car Buyer &rarr;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-10114-loansome-car-buyer-172.html\" \/>\n<meta property=\"og:site_name\" content=\"Ferocious Coder\" \/>\n<meta property=\"article:published_time\" content=\"2013-06-20T15:52:12+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2013-07-13T06:49:05+00:00\" \/>\n<meta name=\"author\" content=\"Rawrosaur\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rawrosaur\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/uva-10114-loansome-car-buyer-172.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/uva-10114-loansome-car-buyer-172.html\"},\"author\":{\"name\":\"Rawrosaur\",\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/#\\\/schema\\\/person\\\/1fb5cbee546cffd619a7b301e3dc447a\"},\"headline\":\"UVA 10114 &#8211; Loansome Car Buyer\",\"datePublished\":\"2013-06-20T15:52:12+00:00\",\"dateModified\":\"2013-07-13T06:49:05+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/uva-10114-loansome-car-buyer-172.html\"},\"wordCount\":276,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/#\\\/schema\\\/person\\\/1fb5cbee546cffd619a7b301e3dc447a\"},\"keywords\":[\"competitive programming 3\",\"java\",\"uva online judge\"],\"articleSection\":[\"Competitive Programming 3\",\"Java\",\"UVA Online Judge\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/uva-10114-loansome-car-buyer-172.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/uva-10114-loansome-car-buyer-172.html\",\"url\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/uva-10114-loansome-car-buyer-172.html\",\"name\":\"UVA 10114 - Loansome Car Buyer - Ferocious Coder\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/#website\"},\"datePublished\":\"2013-06-20T15:52:12+00:00\",\"dateModified\":\"2013-07-13T06:49:05+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/uva-10114-loansome-car-buyer-172.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/uva-10114-loansome-car-buyer-172.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/uva-10114-loansome-car-buyer-172.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"UVA 10114 &#8211; Loansome Car Buyer\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/\",\"name\":\"Ferocious Coder\",\"description\":\"RAWR!\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/#\\\/schema\\\/person\\\/1fb5cbee546cffd619a7b301e3dc447a\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/#\\\/schema\\\/person\\\/1fb5cbee546cffd619a7b301e3dc447a\",\"name\":\"Rawrosaur\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg\",\"caption\":\"Rawrosaur\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg\"},\"sameAs\":[\"http:\\\/\\\/www.ferociouscoder.com\\\/\"],\"url\":\"https:\\\/\\\/www.ferociouscoder.com\\\/blog\\\/archives\\\/author\\\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"UVA 10114 - Loansome Car Buyer - Ferocious Coder","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-10114-loansome-car-buyer-172.html","og_locale":"en_US","og_type":"article","og_title":"UVA 10114 - Loansome Car Buyer - Ferocious Coder","og_description":"This problem was truly annoying and yet extremely simple at the same time. This has been ever so rightly starred in Competitive Programming 3 book. I messed up with an edge case and I wrote `break` instead of `continue` (big oops) in one of my many submissions. The problem mentions that we need to use &hellip; Continue reading UVA 10114 &#8211; Loansome Car Buyer &rarr;","og_url":"https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-10114-loansome-car-buyer-172.html","og_site_name":"Ferocious Coder","article_published_time":"2013-06-20T15:52:12+00:00","article_modified_time":"2013-07-13T06:49:05+00:00","author":"Rawrosaur","twitter_misc":{"Written by":"Rawrosaur","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-10114-loansome-car-buyer-172.html#article","isPartOf":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-10114-loansome-car-buyer-172.html"},"author":{"name":"Rawrosaur","@id":"https:\/\/www.ferociouscoder.com\/blog\/#\/schema\/person\/1fb5cbee546cffd619a7b301e3dc447a"},"headline":"UVA 10114 &#8211; Loansome Car Buyer","datePublished":"2013-06-20T15:52:12+00:00","dateModified":"2013-07-13T06:49:05+00:00","mainEntityOfPage":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-10114-loansome-car-buyer-172.html"},"wordCount":276,"commentCount":2,"publisher":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/#\/schema\/person\/1fb5cbee546cffd619a7b301e3dc447a"},"keywords":["competitive programming 3","java","uva online judge"],"articleSection":["Competitive Programming 3","Java","UVA Online Judge"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-10114-loansome-car-buyer-172.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-10114-loansome-car-buyer-172.html","url":"https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-10114-loansome-car-buyer-172.html","name":"UVA 10114 - Loansome Car Buyer - Ferocious Coder","isPartOf":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/#website"},"datePublished":"2013-06-20T15:52:12+00:00","dateModified":"2013-07-13T06:49:05+00:00","breadcrumb":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-10114-loansome-car-buyer-172.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-10114-loansome-car-buyer-172.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.ferociouscoder.com\/blog\/archives\/uva-10114-loansome-car-buyer-172.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.ferociouscoder.com\/blog"},{"@type":"ListItem","position":2,"name":"UVA 10114 &#8211; Loansome Car Buyer"}]},{"@type":"WebSite","@id":"https:\/\/www.ferociouscoder.com\/blog\/#website","url":"https:\/\/www.ferociouscoder.com\/blog\/","name":"Ferocious Coder","description":"RAWR!","publisher":{"@id":"https:\/\/www.ferociouscoder.com\/blog\/#\/schema\/person\/1fb5cbee546cffd619a7b301e3dc447a"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.ferociouscoder.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.ferociouscoder.com\/blog\/#\/schema\/person\/1fb5cbee546cffd619a7b301e3dc447a","name":"Rawrosaur","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg","caption":"Rawrosaur"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/1372156bd3b16de375c8727c2c617467bf6ff38f1679a7912f48286349c17e96?s=96&d=retro&r=pg"},"sameAs":["http:\/\/www.ferociouscoder.com\/"],"url":"https:\/\/www.ferociouscoder.com\/blog\/archives\/author\/admin"}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1mYMV-2M","jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/posts\/172","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/comments?post=172"}],"version-history":[{"count":4,"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/posts\/172\/revisions"}],"predecessor-version":[{"id":296,"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/posts\/172\/revisions\/296"}],"wp:attachment":[{"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/media?parent=172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/categories?post=172"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ferociouscoder.com\/blog\/wp-json\/wp\/v2\/tags?post=172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}