# 用一张一元票换 1 分、2 分和 5 分的硬币?程序输出有问题，求高手指教

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int one, two, five;
int count = 0;

for (five = 1; five <= 19; five++)
for (two = 1; two <= 47; two++) {
one = 100 - 5 * five - 2 * two;
if (one > 0) {
count++;
printf("第 %d 种换法：five = %d, two = %d, one = %d \n", count, five, two, one);
}
}
printf("一共有 %d 种换法", count);
return 0;
}

...全文
451 14 点赞 打赏 收藏 举报

14 条回复

didijiji 2013-01-29

• 打赏
• 举报

didijiji 2013-01-29

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main()
{
int i, j, k, m, n, r;
int a, b, c, d, e, f;
int count = 0;

for(r = 0; r <= 2; r++)
{
for(n = 0; n <= 5; n++)
{
for(m = 0; m <= 10; m++)
{
for(k = 0; k <= 20; k++)
{
for(j = 0; j <= 50; j++)
{
for(i = 0; i <= 100; i++)
{
int total = i * 1 + j * 2 + k * 5 + m * 10 + n * 20 + r * 50;

if(20 == total)
{
//printf("50:%d   20:%d   10:%d   5:%d   2:%d  1:%d\n", r, n, m, k, j, i);

for(a = 0; a < r; a++)
{
printf("50 ");
}

for(b = 0; b < n; b++)
{
printf("20 ");
}

for(c = 0; c < m; c++)
{
printf("10 ");
}

for(d = 0; d < k; d++)
{
printf("5 ");
}

for(e = 0; e < j; e++)
{
printf("2 ");
}

for(f = 0; f < i; f++)
{
printf("1 ");
}

printf("\n");
count++;
}
}
}
}
}
}
}

printf("\n\ncount = %d\n", count);
system("pause");
return 0;
}

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main()
{
int i, j, k, m, n, r;
int count = 0;

for(r = 0; r <= 2; r++)
{
for(n = 0; n <= 5; n++)
{
for(m = 0; m <= 10; m++)
{
for(k = 0; k <= 20; k++)
{
for(j = 0; j <= 50; j++)
{
for(i = 0; i <= 100; i++)
{
if(100 == i * 1 + j * 2 + k * 5 + m * 10 + n * 20 + r * 50)
{
count++;
}
}
}
}
}
}
}

printf("count = %dn", count);
system("pause");
return 0;
}

• 打赏
• 举报

Shawshenk_We 2013-01-28

• 打赏
• 举报

• 打赏
• 举报

system("mode con:cols=100 lines=500");

• 打赏
• 举报

• 打赏
• 举报

• 打赏
• 举报

• 打赏
• 举报

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
ofstream ofs("yuan.txt", ios::out);
int one, two, five;
int leave = 0;
int maxtwo;
int count = 0;
char buffer[100];
for (five = 1; five <= 19; five++)
{
leave = 100 - five * 5;
maxtwo = (leave - 1) / 2;
for (two = 1; two <= maxtwo; two++)
{
one = leave - two - two;
count++;
sprintf_s(buffer, 100, "%5d:  5-%d, 2-%d, 1-%d\n", count, five, two, one);
printf_s("%s", buffer);
ofs.write(buffer, (streamsize)strlen(buffer));

if (five < 1 || two < 1 || one < 1)
{
system("pause");
}
}
}
sprintf_s(buffer, 100, "Count=%d\n", count);
printf_s("%s", buffer);
ofs.write(buffer, (streamsize)strlen(buffer));
ofs.close();
system("pause");
return 0;
}
• 打赏
• 举报

derekrose 2013-01-28

• 打赏
• 举报

• 打赏
• 举报

didijiji 2013-01-28

• 打赏
• 举报

• 打赏
• 举报

• 打赏
• 举报

C语言

6.5w+

C语言相关问题讨论

2013-01-28 10:59