??(1)一个顾客买了价值x元的商品(不考虑角、分),并将y元的钱交给售货员:编写代码:在各种币值的钱都很充分的情况下,使售货员能用张数最少的钱币找给顾客
#include<stdio.h>
int main()
{
int i = 0, j = 0, x = 0, y = 0, z = 0, a = 0, b[7] = { 0,50,20,10,2,1 }, s[7] = { 0 };//i,j用来循环计数,x为应付金额,y为实付金额,z为应找金额,a用来传参,b数组表示钱币的面值s数组用来存放找零每种币值的张数
printf("请输入应付金额:\n");
scanf_s("%d", &x, sizeof(x));
printf("请输入实付金额:\n");
scanf_s("%d", &y, sizeof(y));
while (x > y)//非法输入判断
{
printf("非法数据请重新输入xy的值:");
scanf_s("%d", &x, sizeof(x));
scanf_s("%d", &y, sizeof(y));
}
z = y - x;
printf("找零:%d\n", z);
for (i = 1; i < 6; i++)
{
a = z / b[i];//b[1]=50从最大面值为50开始对找零金额取整计算50元的张数,以此类推
s[i] = a;
z = z - a * b[i];//减去确定张数的钱币金额,剩余部分进入循环语句判断下一种面值的钱币数
}
for (i=1;i<=6;i++)//打印
{
if(s[i]>0)
{
printf("%d元%d张\t",b[i],s[i]);
}
}
return 1;
}
?<1>正确输入运行结果展示
<2>非法输入运行结果展示
(2)将输入数字编号332.....翻译为英文编号three?three two.....
#include<stdio.h>
int main()
{
int i = 0, j = 0, ind1 = 0, ind2 = 0;
int a[10] = {0};
char eng[10][6] = { "one","two","three","four","five","six","seven","eight","nine" };
printf("输入一个数ind1:");
scanf_s("%d", &ind1, sizeof(ind1));
while(ind1 >0)
{//例:ind1=326
ind2 = ind1 % 10;//先将输入数值个位对十取余,ind2=6
a[i] = ind2;//将取余得到的个位数数值存入数组a[i]中,注意此处排列顺序为个、十、百....a[0]=6
ind1 = ind1 / 10;//将对10取余后的数再对10取整,ind1=32
i++;
}
for (; i >= 0; i--)//因为a[i]数组存放顺序为个、十、百....例:623,所以要倒着输出为326
{
j = a[i-1]-1;
printf("%s ", eng[j]);//%输出为一维,这个地方输出用eng[j]就好,eng[j][6]反而会出错,因为eng[j][6]为二维数组
}
return 1;
}
?<1>输出结果展示