x=20.00;
y=0;
if (n<0)
printf("Sorry,you put the wrong number!");
else if (n==0)
printf("Please buy at least 1 book!");
else if (n==1)
{ y=x;
printf("------You should pay:%.2f",y*n);
}
else if (n==2)
{ y=x*0.9;
printf("------You should pay:%.2f",y*n);
}
else if (n==3)
{ y=x*0.85;
printf("------You should pay:%.2f",y*n);
}
else if (n<=100)
{ y=x*0.8;
printf("------You should pay:%.2f",y*n);
}
else if (n>100)
printf("Sorry,you can buy less then 100 books.");
}
二、
#include<stdio.h>
#include<conio.h>
main()
{
int t;
float n;
clrscr();
printf("Please Enter The Score:");
gotoxy(24,1);
scanf("%f",&n);
t=n/10;
if (n<0)
printf("Please Write The Right Score!");
else if (n>100)
printf("Is He A God?");
else
switch(t)
{
case 10:printf("A+");break;
case 9:printf("A");break;
case 8:printf("B");break;
case 7:printf("C");break;
case 6:printf("D");break;
default:printf("E");break;
}
}
实验四 循环控制
一:目的要求
1.熟悉用while语句 , do-while 语句和for语句实现循环的方法;
2.掌握在程序设计中用循环方法实现各种算法;
3.掌握计算程序运行所占机时的计算方法;
4.上机前按实验要求预习编写出完整的程序 , 才允许上机 。
二:实验内容与步骤
目标:100匹马驮100担货;
假设:大马一匹驮3担 , 中马一匹驮2担 , 小马两匹驮一担;
组合方法1:大马、中马、小马每种不能少于一匹;
组合方法2:对马匹种类无限制 , 可以缺少一种或缺二种
问题: 1. 采用组合方法1 , 用while求解 , 输出所有组合法 , 输出组合的总数?
2.采用2种组合 , 用do-while求解 , 输出所有组合法 , 输出组合的总数?
3.采用2种组合 , 用三重或二重for循环求解 , 输出所有组合法 , 输出组合的总数?
4.除打印结果和多少种组合法外 , 还要分别打印三种算法所费机时 。
提示:计算一种算法所占机时的程序提示:
#include <time.h>
#include <conio.h>
#include <dos.h>
main()
{
clock_t start,end; /* time_t start,end;*/
int i,big,middle,small,ncount;
clrscr();
start=clock(); /* start = time();*/
big=1; middle=1; small=2;
ncount=0;
printf("This a while program\");
while (big<=33)
{
.
}
end=clock(); /* end = time();*/
printf("The num of method1 is: %d\",ncount);
printf("and the time is: %5.1f time\",difftime(end,start));
/*printf f(“”The difference is :%5.1f second\”, difftime(end,start)/18.2);*/
……}
解答:
一、
#include <time.h>
#include <conio.h>
#include <dos.h>
main()
{
clock_t start,end; /* time_t start,end;*/
int i,big,middle,small,ncount,n;
clrscr();
start=clock(); /* start = time();*/
big=1; middle=1; small=2,i=0,n=0;
ncount=0;
printf("This a while program\");
while (big<=33)
{
for(small=2;small<=100;small=small+2)
for(middle=1;middle<=100;middle++)
{
n=big*3+middle*2+small/2;
i=big+middle+small;
if (n==100 && i==100)
{
printf("Big=%d,Middle=%d,Small=%d,n=%d,i=%d\",big,middle,small,n,i);
- c语言用户标识符规则,c语言用户自定义标识符作用
- c语言生成随机数矩阵,随机数法生成矩阵
- c语言比较三个数大小输出最小数,三个数比大小c语言输出最大值
- c语言强制类型转换详解,c语言类型转换注意点
- 乙肝抗体30多还用打疫苗吗
- 农村俗语“六月立秋快加油,七月立秋慢悠悠”是什么意思?今年立秋是哪一天?
- 我老婆左眼一直跳,各位大神有什么好的解决方案?
- 砖茯茶是什么茶?喝它有什么讲究?
- 属鼠男是什么性格?
- 桃胶的功效与作用及食用方法是什么?
特别声明:本站内容均来自网友提供或互联网,仅供参考,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
