c语言计算价格

Python013

c语言计算价格,第1张

int main()

{

int price[100]

int  n, m, total = 0, i

scanf("%d %d", &n, &m)

i = 0

while (i < n) {

int no, p

scanf("%d %d", &no, &p)

price[no-1] = p

i++

}

i = 0

while (i < m) {

int no, cnt

scanf("%d %d", &no, &cnt)

total += cnt * price[no-1]

i++

}

printf("%d\n", total)

return 0

}

你好,如上所说,一共有361种兑换方法,可以得出{5C[1],C[1] + 2C[2],20 - C[1] -C[2]}方程,只需要保证都大于0就行。

1、遇到这种题目,我们首先要区分的概念是钱数和张数。这个问题我可以说大家伙都是明白的,但是用计算机来表达并不容易,是很有难度的。所以,在这种情况下,我们可以设置更多变量以使其易于查看,接着清空你的思绪。将100元换成1元、5元和10元。最简单的零钱是10元。因为 10 是 1、5、10 的最小公倍数。这总是为较小的数字留下空间来计算其他值。

2、然后可以用100删除,简而言之,无法清除并除以 100 / 10。这样10元10件、1元0件、5元最多可以获得。因此,最多为 10 件。如果9块是10元,此时定义变量13、以下就是这道题的代码

#include "stdio.h"

#include "math.h"

main()

{

printf("共有%d种不同的兑换方案",fun (int n))

}

fun(int m)

{ int i

for(i=0i{int j

for(j=0j{int k

for(k=0k{

if(10i+5j+k==100) m+=1

}

}

if(i==10) return m

}

}

程序源码如下:

循环嵌套循环次数少的尽量在外!

#include

#include

using namespace std

int main()

{

int count = 0//换法数量

int temp

int m = 100//总钱数

int a = 1//1元面值

int b = 2//2元面值

int c = 5//5元面值

//a, b, c的值还可以是10,20,50

for (int i = 0i {

for (int j = 0j {

temp = m - (b * i + c * j)

//只要2元和5元的总和加起来不大于100,剩下的都换1元

if (temp >= 0 &&temp % a == 0)

{

++count//换法种数+1

}

}

}

cout "//输出换法种数

system("pause")

return 0

}