15.设计算法并编写程序,循环输入20个0100 的成绩 分别统计它们中 90 及以上89 70
8079 6069 小于 60 的数的个数。
#include <stdio.h>
int main() {
int score, count_90plus = 0, count_80to89 = 0, count_70to79 = 0, count_60to69 = 0, count_below60 = 0;
for (int i = 1; i <= 20; i++) {
printf("请输入第%d个成绩:", i);
scanf("%d", &score);
if (score >= 90) {
count_90plus++;
} else if (score >= 80 && score <= 89) {
count_80to89++;
} else if (score >= 70 && score <= 79) {
count_70to79++;
} else if (score >= 60 && score <= 69) {
count_60to69++;
} else {
count_below60++;
}
}
printf("90及以上的数的个数为%d\n", count_90plus);
printf("80~89的数的个数为%d\n", count_80to89);
printf("70~79的数的个数为%d\n", count_70to79);
printf("60~69的数的个数为%d\n", count_60to69);
printf("小于60的数的个数为%d\n", count_below60);
return 0;
}
代码来源:边界标志算法
#include <GL/glut.h>
#include <math.h>
bool a[200][200] = { false };
void Line(int x1, int y1, int x2, int y2) {
int step;
int dx = x2 - x1;
int dy = y2 - y1;
float xDelta, yDelta, x = x1, y = y1;
if (abs(dx) > abs(dy))
step = abs(dx);
else
step = abs(dy);
xDelta = float(dx) / step;
yDelta = float(dy) / step;
float xPre, yPre;
for (int i = 0; i < step; i++) {
xPre = x; yPre = y;
x += xDelta;
y += yDelta;
if (int(y + 0.5) != int(yPre + 0.5)) { //当此时的值与上一个值部同时,标记上
a[int(x + 0.5)][int(y + 0.5)] = !(a[int(x + 0.5)][int(y + 0.5)]);
glVertex2i(int(x + 0.5), int(y + 0.5));
}
}
}
void edqemark_fill(int Xmin, int Xmax, int Ymin, int Ymax) {
for (int i = Ymin + 1; i <= Ymax; i++) {
bool inside = false;
for (int j = Xmin; j <= Xmax; j++) {
if (a[j][i] == true) {//遇到边界就取反
inside = !(inside);
}
if (inside)
glVertex2i(j, i);
}
}
};
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0); // 设置背景颜色
glMatrixMode(GL_PROJECTION); // 设置投影参数
gluOrtho2D(0.0, 600.0, 0.0, 400.0); // 设置场景的大小
}
void draw(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.8, 0.3, 0.1); // 设置画图颜色
glLineWidth(3);
glPushMatrix();
glBegin(GL_POINTS);
Line(20, 30, 80, 120);
Line(150, 50, 20, 30);
Line(150, 50, 80, 120);
glEnd();
glBegin(GL_POINTS);
edqemark_fill(20, 150, 30, 120);
glEnd();
glPopMatrix();
glFlush(); // 处理绘图pipeLine
}
void main(int argc, char** argv)
{
glutInit(&argc, argv); // 初始化GLUT环境
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // GLUT显示模式:单缓冲区、RGB颜色模型
glutInitWindowPosition(50, 100); // 窗口左上角的位置
glutInitWindowSize(800, 600); // 显示窗口的大小
glutCreateWindow("An Example of OpenGL"); // 创建显示窗口,加上标题
init();
glutDisplayFunc(draw); // 调用绘图函数
glutMainLoop(); // 进入事件处理循环
}