| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1260 人关注过本帖
标题:大佬给我看看
只看楼主 加入收藏
F65846
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2021-9-19
结帖率:0
收藏
 问题点数:0 回复次数:1 
大佬给我看看
整钱换零钱,一元钱可兑换成1角,2角,5角,问共有多少种方案,并请按格式输出所有的方案。
第一行开始,按格式输出所有的方案
最后一行输出多少种方案
搜索更多相关主题的帖子: 输出 方案 多少 格式 一行 
2021-10-03 12:53
自由而无用
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:61
专家分:1456
注 册:2021-8-9
收藏
得分:0 
https://blog.
2021-10-03 15:50
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:0 
并请按格式输出所有的方案
你题目不肯帖全,因此我就只能讨论主要算法

一元钱可兑换成1角,2角,5角
也就是 a*1 + 2*b + 5*c = 10
从大到小循环,可缩减循环次数,也可避免无效循环
故而有代码

程序代码:
#include <stdio.h>

int main( void )
{
    for( unsigned c=0; 5*c<=10; ++c )
        for( unsigned b=0; 5*c+2*b<=10; ++b )
            printf( "%u个1角,%u个2角,%u个5角\n", 10-5*c-2*b, b, c );
}
2021-10-03 18:03
自由而无用
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:61
专家分:1456
注 册:2021-8-9
收藏
得分:0 
//online parser: https://www.bccn.net/run/
程序代码:
#include <stdio.h>

typedef struct dollar_sys {
    int dime;
    int tw_cents;
    int hf_dollar;
}vdos;

int calc_dos(vdos *pdos)
{
    int one_dollar = 100;
    
    one_dollar -= pdos->dime * 10;
    if (one_dollar < 0) return 0;
    one_dollar -= pdos->tw_cents * 20;
    if (one_dollar < 0) return 0;
    one_dollar -= pdos->hf_dollar * 50;
    if (one_dollar < 0) return 0;
    
    return 0 == one_dollar ? 1 : 0;

}

int main(int argc, char *argv[])
{
    vdos dos = {0};
    int i = 0;
    
#define USR_EXP_CNT 1000    
    while (i++ < USR_EXP_CNT) {
        dos.dime = ((unsigned char *)main)[i] % 10;
        dos.tw_cents =  ((unsigned char *)calc_dos)[i + 1] % 10;
        dos.hf_dollar = ((unsigned char *)main)[i + 2] % 10;
        if (calc_dos(&dos)) {
            puts("Mr.main bingo!");
            printf("dime = %d  ", dos.dime);
            printf("tw_cents = %d  ", dos.tw_cents);
            printf("hf_dollar = %d\n", dos.hf_dollar);
            puts("------------------------------------");
        }
    }
    
    return 0;
}


output sample:

Mr.main bingo!
dime = 0  tw_cents = 5  hf_dollar = 0
------------------------------------
Mr.main bingo!
dime = 2  tw_cents = 4  hf_dollar = 0
------------------------------------
Mr.main bingo!
dime = 1  tw_cents = 2  hf_dollar = 1
------------------------------------
Mr.main bingo!
dime = 0  tw_cents = 0  hf_dollar = 2
------------------------------------
Mr.main bingo!
dime = 5  tw_cents = 0  hf_dollar = 1
------------------------------------
Mr.main bingo!
dime = 0  tw_cents = 5  hf_dollar = 0
------------------------------------
Mr.main bingo!
dime = 0  tw_cents = 5  hf_dollar = 0
------------------------------------
Mr.main bingo!
dime = 0  tw_cents = 5  hf_dollar = 0
------------------------------------
Mr.main bingo!
dime = 0  tw_cents = 5  hf_dollar = 0
------------------------------------
Mr.main bingo!
dime = 0  tw_cents = 0  hf_dollar = 2
2021-10-03 20:09
自由而无用
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:61
专家分:1456
注 册:2021-8-9
收藏
得分:0 
//online parser: https://www.bccn.net/run/
程序代码:
#include <stdio.h>

typedef struct mini_game_obj {
    int level;
    int exp;
    int achv[11];
}vmg;

typedef struct dollar_obj {
    int dime;
    int tw_cents;
    int hf_dollar;
    vmg mg;
}vdos;

int calc_dos(vdos *pdos)
{
    int one_dollar = 100;
    
    one_dollar -= pdos->dime * 10;
    if (one_dollar < 0) return 0;
    one_dollar -= pdos->tw_cents * 20;
    if (one_dollar < 0) return 0;
    one_dollar -= pdos->hf_dollar * 50;
    if (one_dollar < 0) return 0;
    
    return 0 == one_dollar ? 1 : 0;

}

int check_sum(int *cs, int rs)
{
    int i;
    
    for (i = 0; i < 10; i++)
        if (cs[i] == rs) return 1;
        
    return 0;
}

int main(int argc, char *argv[])
{
    vdos dos = {0};
    int i = 0;
    
#define USR_EXP_CNT 1000    
    while (i++ < USR_EXP_CNT) {
        dos.dime = ((unsigned char *)main)[i] % 10;
        dos.tw_cents =  ((unsigned char *)calc_dos)[i + 1] % 10;
        dos.hf_dollar = ((unsigned char *)main)[i + 2] % 10;
        if (calc_dos(&dos)) {
#define REC_PTR 10
#define rec_sum (dos.dime * 100 + dos.tw_cents * 10 + dos.hf_dollar)
            //new rec_sum
            if (!check_sum(dos.mg.achv, rec_sum)) {
                dos.mg.achv[dos.mg.achv[REC_PTR]++] = rec_sum;
                puts("!!---new achievement---!!");
#define add_exp (dos.dime + dos.tw_cents + dos.hf_dollar)
                dos.mg.exp += add_exp;
                printf("obtained exp: %d\n", add_exp);
                
            } else {
                // exp /= 2;
                dos.mg.exp += (add_exp >> 1);
                printf("obtained exp: %d\n", (add_exp >> 1));
            }
            //show status
            printf("Mr.main[LV_%d] bingo! exp = %d\n",
                    dos.mg.level, dos.mg.exp);
            printf("dime = %d  ", dos.dime);
            printf("tw_cents = %d  ", dos.tw_cents);
            printf("hf_dollar = %d\n", dos.hf_dollar);
           
            //level up
#define FIXED_LEVEL_UP 25
            if (dos.mg.exp >= FIXED_LEVEL_UP) {
                puts("Mr.main LV up!!!");
                dos.mg.level++; dos.mg.exp -= 25;
            }
            puts("------------------------------------");
        }
    }
    
    return 0;
}


output sample:

!!---new achievement---!!
obtained exp: 8
Mr.main[LV_0] bingo! exp = 8
dime = 6  tw_cents = 2  hf_dollar = 0
------------------------------------
!!---new achievement---!!
obtained exp: 5
Mr.main[LV_0] bingo! exp = 13
dime = 0  tw_cents = 5  hf_dollar = 0
------------------------------------
!!---new achievement---!!
obtained exp: 2
Mr.main[LV_0] bingo! exp = 15
dime = 0  tw_cents = 0  hf_dollar = 2
------------------------------------
obtained exp: 1
Mr.main[LV_0] bingo! exp = 16
dime = 0  tw_cents = 0  hf_dollar = 2
------------------------------------
!!---new achievement---!!
obtained exp: 4
Mr.main[LV_0] bingo! exp = 20
dime = 1  tw_cents = 2  hf_dollar = 1
------------------------------------
obtained exp: 2
Mr.main[LV_0] bingo! exp = 22
dime = 1  tw_cents = 2  hf_dollar = 1
------------------------------------
obtained exp: 1
Mr.main[LV_0] bingo! exp = 23
dime = 0  tw_cents = 0  hf_dollar = 2
------------------------------------
obtained exp: 2
Mr.main[LV_0] bingo! exp = 25
dime = 1  tw_cents = 2  hf_dollar = 1
Mr.main LV up!!!
------------------------------------
obtained exp: 2
Mr.main[LV_1] bingo! exp = 2
dime = 1  tw_cents = 2  hf_dollar = 1
------------------------------------
obtained exp: 2
Mr.main[LV_1] bingo! exp = 4
dime = 1  tw_cents = 2  hf_dollar = 1
------------------------------------
!!---new achievement---!!
obtained exp: 6
Mr.main[LV_1] bingo! exp = 10
dime = 5  tw_cents = 0  hf_dollar = 1
------------------------------------
obtained exp: 1
Mr.main[LV_1] bingo! exp = 11
dime = 0  tw_cents = 0  hf_dollar = 2
------------------------------------
obtained exp: 2
Mr.main[LV_1] bingo! exp = 13
dime = 0  tw_cents = 5  hf_dollar = 0
------------------------------------
obtained exp: 2
Mr.main[LV_1] bingo! exp = 15
dime = 0  tw_cents = 5  hf_dollar = 0
------------------------------------
obtained exp: 1
Mr.main[LV_1] bingo! exp = 16
dime = 0  tw_cents = 0  hf_dollar = 2
------------------------------------
obtained exp: 1
Mr.main[LV_1] bingo! exp = 17
dime = 0  tw_cents = 0  hf_dollar = 2
------------------------------------
!!---new achievement---!!
obtained exp: 6
Mr.main[LV_1] bingo! exp = 23
dime = 2  tw_cents = 4  hf_dollar = 0
------------------------------------
obtained exp: 1
Mr.main[LV_1] bingo! exp = 24
dime = 0  tw_cents = 0  hf_dollar = 2
------------------------------------
obtained exp: 2
Mr.main[LV_1] bingo! exp = 26
dime = 0  tw_cents = 5  hf_dollar = 0
Mr.main LV up!!!
------------------------------------
obtained exp: 2
Mr.main[LV_2] bingo! exp = 3
dime = 0  tw_cents = 5  hf_dollar = 0
------------------------------------
obtained exp: 2
Mr.main[LV_2] bingo! exp = 5
dime = 1  tw_cents = 2  hf_dollar = 1
------------------------------------
2021-10-03 22:33
快速回复:大佬给我看看
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027770 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved