c_cpp 第1周贪心

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 第1周贪心相关的知识,希望对你有一定的参考价值。

#include <cs50.h>
#include <stdio.h>

int main(void)
{

	float x;
	int  q1, d1, n1, p1, c, r1, r2, r3, t;

	//prompt user for an amount of change

	do
	{
		x = get_float("Please input the amount of change that is less than $1:  ");
	}
	while ((x<0) || (x>1.0));

	c = (int)(100 * x);

	//always use the largest coin possible

		//check if can use 25c coins

			r1 = c % 25;
			if ((c - r1) <0)
			{
				q1 = 0;  // there is no quarter coin
			}
			else
			{
				q1 = (c - r1)/25; // there are q1 number of quarters
			}

		//check if can use 10c coins

			r2 = r1 % 10;
			if ((r1 - r2) <0)
			{
				d1 = 0;  // there is no dime
			}
			else
			{
				d1 = (r1 - r2)/10; // there are d1 number of dimes
			}

		//check if can use 5c coins

			r3 = r2 % 5;
			if ((r2 - r3) <0)
			{
				n1 = 0;  // there is no nickle
			}
			else
			{
				n1 = (r2 - r3)/5; // there are n1 number of nickle
			}

		//check if can use 1c coins

			if ( r3 == 0)
			{
				p1 = 0;  // there is no penny
			}
			else
			{
				p1 = r3; // there are p1 number of pennies
			}

	//keep track of coins

	t = q1 + d1 + n1 + p1;

	//print the final number of coins

	printf("Total number of coins used: %i\n", t);
	printf("Total number of quaters used: %i\n", q1);
	printf("Total number of dimes used: %i\n", d1);
	printf("Total number of nickles used: %i\n", n1);
	printf("Total number of pennies used: %i\n", p1);

}

以上是关于c_cpp 第1周贪心的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 第1周马里奥1

c_cpp 第1周马里奥2

c_cpp 第1周为C程序员编写的C ++作业

hiho 第216周 Gas Stations(贪心 | 二分答案)

c_cpp 适用于C程序员的C ++第2周

POJ 2393 Yogurt factory贪心