好文档 - 专业文书写作范文服务资料分享网站

MOOC西交大C++基础题100题 - 图文

天下 分享 时间: 加入收藏 我要投稿 点赞

C++基础题100题

第一部分:

1、显示Hello Worle!

编写C++程序,在屏幕上显示“Hello World!”。 #include int main() { using namespace std; cout << \ return 0; }

2、显示唐诗

编写C++程序,在屏幕上显示下列唐诗: 慈母手中线 游子身上衣 临行密密缝 意恐迟迟归 谁言寸草心 报得三春晖

#include int main() { using namespace std; cout << \慈母手中线\\n游子身上衣\\n临行密密缝\\n意恐迟迟归\\n谁言寸草心\\n报得三春晖\ return 0; }

3、显示一句话

编写C++程序,输入姓名,在屏幕上显示如下格式的文字: This program is coded by ***.

其中“***”是输入的名字。如输入“ZhangSan”,则显示: This program is coded by ZhangSan.

注意,姓名中间没有空格,末尾有英文句号。 #include int main() { using namespace std; char name[50]; cin >> name; cout << \ return 0;

}

4、还是一句话

编写C++程序,输入姓名,在屏幕上显示如下格式的文字: This program is coded by ***.

其中“***”是输入的名字。如输入“Zhang San”,则显示: This program is coded by Zhang San.

注意,姓名中间可能有空格,末尾有英文句号。 #include int main() { using namespace std; char name[50]; cin.getline(name, 49); cout << \ getchar(); return 0; }

5、计算矩形周长

输入矩形的两个边的长度,计算矩形的周长。 #include int main() { using namespace std; int a, b, c; cin >> a >> b; c = (a + b) * 2; cout << c << endl; return 0; }

6、已知直角边求斜边

输入一个三角形的两个直角边的长度,求其斜边的长度:计算公式是 c=sqrt(a*a+b*b)

其中, a,b是两个直角边的长度,c是斜边,sqrt表示开平方。 #include #include int main() { using namespace std; double a, b, c; cin >> a >> b; c = sqrt(a*a + b*b); cout << c << endl; return 0; }

第二部分:

1、求过平面上两点的直线的斜率

编写程序,输入平面上的两个点的坐标(x1,y1),(x2,y2),求过这两点的直线的斜率(设斜率不为无穷)。 【提示】数据类型都用double #include int main() { using namespace std; double x1, y1, x2, y2; double k; cin >> x1 >> y1 >> x2 >> y2; k = (y2 - y1) / (x2 - x1); cout << k << endl; return 0; }

2、计算平面上两点之间的距离

编写程序,输入平面上的两个点的坐标(x1,y1),(x2,y2),计算这两点之间的距离。

【提示】数据类型用double,包含头文件cmath, 计算公式distance=(x2-x1)*(x2-x1)+(y2-y1)*(y2-y1);distance=sqrt(distance); #include #include int main() { using namespace std; double x1, y1, x2, y2; double k; cin >> x1 >> y1 >> x2 >> y2; k = (x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1); k = sqrt(k); cout << k << endl; return 0; }

3、判断大小写

输入一个英文字母,判断大小写。大写输出1,小写输出0. #include int main() { using namespace std; char c; cin >> c; if (c >= 'A'&&c <= 'Z') cout << 1; else

}

cout << 0; return 0;

4、判断数字

输入一个英文字符,判断是否数字。是输出1,不是输出0.

【提示】字符类型用char,设输入的字母为c,判断数字的是:c>='0' && c<='9',然后使用条件运算符,条件?cout<<1 : cout<<0; #include int main() { using namespace std; char c; cin >> c; if (c >= '0'&&c <= '9') cout << 1; else cout << 0; return 0; }

5、判断闰年

编写程序,输入年份,判断是否闰年。是,输出“IsLeapYear”;“否”,输出“NotLeapYear”。 #include int main() { using namespace std; int year; cin >> year; if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) cout << \ else cout << \ return 0; }

6、求商和余数

输入两个正整数,求它们的商和余数。例如,输入 18和10,则它们的商是1,余数是8。 【提示】使用int类型,\用来求商,\用来求余数。 #include int main() { using namespace std; int a, b; cin >> a >> b; cout << a / b << ' ' << a % b << endl; return 0;

}

7、计算平均分取整

某招聘面试,7个专家给考生打分,编写程序,计算7个专家给分的平均分,取整。 【提示】四舍五入:y=int(x+0.5) #include using namespace std; int main() { double a, sum = 0.0; double average; for (int i = 0; i < 7; ++i) { cin >> a; sum += a; } average = sum / 7; average = (int)(average + 0.5); cout << average << endl; return 0; }

8、计算点到直线的距离保留两位小数

直线方程:Ax+By+C=0

编写程序,输入A,B,C,和点(x,y),计算该点到直线的距离。 点到直线的距离公式:

d=|Ax+By+C|/sqrt(A*A+B*B)

其中|z|表示绝对值,程序中使用条件表达式,如:z<0 ? z=-z:z=z; #include #include using namespace std;

int main() { double A, B, C, D, x, y, z; cin >> A >> B >> C; cin >> x >> y; z = A * x + B * y + C; z < 0 ? z = -z : z = z; D = z / sqrt(A*A + B * B); D = int(D * 100 + 0.5) / 100.00; cout << D << endl; return 0; }

9、输入字符显示ASCII值

编写程序,输入一个字符,显示其ASCII值。如输入“A”,显示65,输入“a”显示97。

MOOC西交大C++基础题100题 - 图文

C++基础题100题第一部分:1、显示HelloWorle!编写C++程序,在屏幕上显示“HelloWorld!”。#includeintmain(){usingnamespacestd;cout<<\return0;}2、显示唐诗编写C++程序,在屏幕上显
推荐度:
点击下载文档文档为doc格式
5cc255etse570pk9t8239nplx1m54t00amk
领取福利

微信扫码领取福利

微信扫码分享