购物编程题
以下在VC++6.0下编写并测试通过!
1.
#include <stdio.h>
float r1(float m);
void main()
{
float s,r;
printf("输入圆的半径:");
scanf("%f",&r);
s=r1(r);
printf("圆面积:%.2f\n",s);
}
float r1(float r)
{
float s;
s=3.14*r*r;
return(s);
}
2.
#include <stdio.h>
float fun(float p,int n)
{
if(n>=20)
return p*n*0.8;
else if(n>=10)
return p*n*0.9;
else
return p*n;
}
void main(void)
{
float p=0;
int n=0;
printf("please input the unit price p and numbers n\n");
scanf("%f %d",&p,&n);
printf("%8.2f\n",fun(p,n));
}
3. 三个整数由大到小排序
#include<stdio.h>
void sort(int a,int b,int c)
{ int temp;
if(a<b){temp=a;a=b;b=temp;}
if(a<c){temp=a;a=c;c=temp;}
if(b<c){temp=b;b=c;c=temp;}
printf("from big to small is:%d %d %d\n",a,b,c);
}
void main()
{int a,b,c;
printf("please input three numbers a b c\n");
scanf("%d %d %d",&a,&b,&c);
sort(a,b,c);
}
B. C++编程题目,“类”相关,两道十分简单,另外两道也不是很难,求编程高手解答!
呵呵,你是上肖老师的C++吧。。。
C. 一道编程题,请讲一下思路就好。
额,这倒题对于我这种数学本身就不太好的人来说,太难了,头都想大了(智商是硬伤啊)!不过我倒是想把自己能想到的拿出来分享一下,仅供参考;
首先,我要提出一个错误,a[1000001]这样的写法是不对的,具体最大不太清楚,但肯定没有这么大的下标了,在这里的目的只是为也设置一下足够大的数组即可,不需要搞那么大!
好,下面正题开始:
如题,设共有n个物品,m个人,生气程度最小的人生气程度为l,最大人的生气程度(即总共搬运物品)为r,第i个物品的重量为a[i],第j个人生气程度为b[j](此处未用到),假设只有最大和最小两种结果,则他们的平均值设为mid,若要使最大生气程度的人的生气值最小,则每个人的生气值都相等或近似相等且为最大值(因为如此一来,总数不变的情况下,得到的均值才最小),如下题解(注释)以此为基础;
scanf("%d%d",&n,&m);//输入n个物品和m个人
for(i=1;i<=n;i++){//输入n个物品各自的重量,依次序赋值,并保存总重量sum备用
scanf("%d",&a[i]);
sum+=a[i];
}
r=sum;//将总数默认为最大生气值的默认值(实际是不是可能的)
l=0;//将0作为最小生气的人的生气值(从这里的开始计算)
intrs;//找到的最大值的控制开关
while(r-l>1){//如果最大值和最小时之间相差小于1,则认为已找到最优解
mid=(l+r)/2;//算出中间值(缩小范围,中间的情况也是如此,没必要重复)
sum=0;//用以记录各个值间的大小大概关系
rs=1;
for(i=1;i<=n;i++){
if(sum+a[i]<=mid)sum+=a[i];//最开始时,各值不够大,让他们相加好了
else{//相加到一定程度后,转换另外一种算法,把他的值降小,重新计算
sum=a[i];
if(sum>mid){//如果这个值远远大于中间值,则基本上他就是最大值了,但亦不排除后面会有值比此大,因此未用break语句;而是赋予开关一个很大的值
rs=10000000;
}
rs++;//开关加1,证明大于平均值的生气值又多了一个
}
}
if(rs>m)l=mid;//生气值大于中间值的人数已大于总人数,也就是说大家可以再上一台阶
elseif(rs<=m)r=mid;//否则,大家应当降一个级别,将中间值赋予最大值
}
如此算下来,只要循环结束,以r为值即可得到最大生气的最小值(有点晕啊,没关系,多看看就好,看不懂也没关系,反正也不一定正确,哈哈);
D. C#编程题
简单
if(yes)
{
check();
}
else
{
continue;
}
check()
{
if()
{}
else
{}
}
E. 一道JAVA编程题
public class Person {
private String userName;// 名字
private int age;//
public Person(String userName, int age) {
this.userName = userName;
this.age = age;
// TODO Auto-generated constructor stub
}
public void display() {
System.out.println("姓名:" + userName);
System.out.println("年龄:" + age);
}
public static void main(String[] args) {
Person person=new Person("asss",1);
person.display();
}
}
F. C语言编程应用题
#include<stdio.h>
intmain()
{doublex,y;
charc;
printf("请选择普通顾客/会员顾客(P/H):");
scanf("%c",&c);
printf("请输入商品总额:");
scanf("%lf",&x);
if(c=='H'||c=='h')
if(x>=1000)y=0.7*x;
elsey=0.8*x;
else
if(x>100)y=0.9*x;
elsey=x;
printf("应收费:%.2lf
",y);
return0;
}
G. C语言题:输入一个购物金额求输出折扣率与付款金额,如图
#include<stdio.h>
intmain()
{
inti;floatpay;
scanf("%f",&pay);
if(pay<=0)printf("输入购物金额有误!");
else
{
if(pay<200)i=10;
elseif(pay<500)i=9;
elseif(pay<1000)i=8;
elsei=7;
i/10?printf("不打折,"):printf("折扣率:%d折,",i);
printf("实际付款金额:%.2f元",pay*i/10);
}
return0;
}
H. VB程序设计题:已知某商店全场9折,一次购物如果满1000元8折,3000元以上7折,5000元以上5折
private sub canmmand1_click()
dim a as single
dim m as single
dim z as string
a= val(inputbox "输入价格")
select case a
case is >=5000
m=a*0.5
z="五折"
case is >=3000
m=a*0.7
z="七折"
case is >=1000
m=a*0.8
z="8折"
case is >0
m=a*0.9
z="九折"
case else
msgbox "输入错误!"
exit sub
end select
msgbox "应付款"& a & "打" & z & "后,实付款" & m
end sub
I. c语言编程题
#include <stdio.h>
int main()
{ double x,y;
printf("请输入消费总额:\n");
scanf("%lf",&x);
if(x<1000) y=x;
else if(x<2000) y=0.9*x;
else if(x<3000) y=0.8*x;
else y=0.7*x;
printf("%.2lf\n",y);
return 0;
}
//祝你学习进步