- 问答
- 2017-07-26 14:34:55 @
期末来临了,班长小Q决定将剩余班费x元钱,用于购买若干支钢笔奖励给一些学习好的同学。已知商店里有三种钢笔,它们的单价分别是4元,5元,6元。小Q想买尽量多的笔(鼓励尽量多的学生),同时他又不想有剩余钱。请你编一个程序,帮小Q制定一种买笔的方案。
3 条评论
-
遥望天空 LV 7 @ 2017-07-26 18:49:01
#include<iostream>
using namespace std;
int main()
{
int a,b,c,x,y;
cin>>x;
if(x<4) cout<<"4元"<<0<<"个"<<" "<<"5元"<<0<<"个"<<" "<<"6元"<<0<<"个"<<endl;
else
{a=x/4;
y=x%4;
switch(y)
{
case 0:b=0,c=0;break;
case 1:a-=1,b=1,c=0;break;
case 2:a-=1,b=0,c=1;break;
case 3:a-=2,b=1,c=1;break;
}
cout<<"4元"<<a<<"个"<<" "<<"5元"<<b<<"个"<<" "<<"6元"<<c<<"个"<<"或";
switch(y)
{
case 2:a-=1,b=2,c=0;break;
case 3:a-=1,b=3,c=0;break;
}
cout<<"4元"<<a<<"个"<<" "<<"5元"<<b<<"个"<<" "<<"6元"<<c<<"个"<<endl;
}
} -
2017-07-26 15:17:00@
#include<iostream>
using namespace std;
int main()
{
int a,b,c,x,y;
cin>>x;
if(x<4) cout<<"4元"<<0<<"个"<<" "<<"5元"<<0<<"个"<<" "<<"6元"<<0<<"个"<<endl;
a=x/4;
y=x%4;
switch(y)
{
case 0:b=0,c=0;break;
case 1:a-=1,b=1,c=0;break;
case 2:a-=1,b=0,c=1;break;
case 3:a-=2,b=1,c=1;break;
}
if(x>4&&y>=2) cout<<"4元"<<a<<"个"<<" "<<"5元"<<b<<"个"<<" "<<"6元"<<c<<"个"<<"或";
switch(y)
{
case 2:a-=1,b=2,c=0;break;
case 3:a-=1,b=3,c=0;break;
}
if(x>4) cout<<"4元"<<a<<"个"<<" "<<"5元"<<b<<"个"<<" "<<"6元"<<c<<"个"<<endl;
} -
2017-07-26 14:35:57@
背包
- 1