第一题:
编程题#1:判断闰年
来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)
注意: 总时间限制: 1000ms 内存限制: 65536kB
描述
判断某年是否是闰年。
输入
输入只有一行,包含一个整数a(0 < a < 3000)
输出
一行,如果公元a年是闰年输出Y,否则输出N
样例输入
2006
样例输出
N
提示
公历纪年法中,能被4整除的大多是闰年,但能被100整除而不能被400整除的年份不是闰年, 能被3200整除的也不是闰年,如1900年是平年,2000年是闰年,3200年不是闰年。
这个题,不过是个分情况讨论的题目而已。#include<iostream>
using namespace std;
int main() {
int input;
cin >> input;
char output = 'N';
if (input % 4 == 0 && input % 100 == 0) {//能被4跟100整除的有可能是闰年,所以要分情况讨论
if (input % 400 == 0 && input % 3200 == 0)
output = 'N';
if (input % 400 != 0)
output = 'N';
if (input % 400 == 0 && input % 3200 != 0)
output = 'Y';
}
if (input % 4 == 0 && input % 100 != 0) {//能被4整除不能被100整除的肯定是闰年
output = 'Y';
}
cout << output << endl;
return 0;
}
然后我们来讨论第二题:
编程题#2:能被3,5,7整除的数
来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)
注意: 总时间限制: 1000ms 内存限制: 65536kB
描述
输入一个整数,判断它能否被3,5,7整除,并输出以下信息:
1、能同时被3,5,7整除(直接输出3 5 7,每个数中间一个空格);
2、能被其中两个数整除(输出两个数,小的在前,大的在后。例如:3 5或者 3 7或者5 7,中间用空格分隔)
3、能被其中一个数整除(输出这个除数)
4、不能被任何数整除;(输出小写字符'n',不包括单引号)
输入
一个数字
输出
一行数字,从小到大排列,包含3,5,7中为该输入的除数的数字,数字中间用空格隔开
样例输入
第一组 0 第二组 5 第三组 15 第四组 105 第五组 1
样例输出
第一组 3 5 7 第二组 5 第三组 3 5 第四组 3 5 7 第五组 n
提示
因为有多组测试数据,程序通过下面方式读入n
int n; while(cin>>n) { //你的代码 } good luck:)
开始吧:
#include<iostream>
using namespace std;
int main() {
int n;
while (cin >> n) {
if (n % 3 == 0) {//先把能被3整除的都分别讨论了
if (n % 5 == 0) {
if (n % 7 == 0)
cout << "3 5 7" << endl;
else
cout << "3 5" << endl;
}
if (n % 7 == 0 && n % 5 != 0)
cout << "3 7" << endl;
if (n % 7 != 0 && n % 5 != 0)
cout << '3' << endl;
}
if (n % 5 == 0 && n % 3 != 0) {//因为能被3整除的已经讨论过了,所以这里只讨论能被5整除,不能被3整除的
if (n % 7 == 0)
cout << "5 7" << endl;
else
cout << '5' << endl;
}
if (n % 7 == 0 && n % 3 != 0 && n % 5 != 0)//同理
cout << '7' << endl;
if (n % 7 != 0 && n % 3 != 0 && n % 5 != 0)//啥也不能的
cout << 'n' << endl;
}
return 0;
}
编程题#3:最远距离
来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)
注意: 总时间限制: 1000ms 内存限制: 65536kB
描述
给定一组点(x,y),求距离最远的两个点之间的距离。
输入
第一行是点数n(n大于等于2)
接着每一行代表一个点,由两个浮点数x y组成。
输出
输出一行是最远两点之间的距离。
使用cout << fixed << setprecision(4) << dis << endl;输出距离值并精确到小数点后4位。
fixed和setprecision是在<iomanip>头文件里定义的格式控制操作符,需要#include <iomanip>.
样例输入
6 34.0 23.0 28.1 21.6 14.7 17.1 17.0 27.2 34.7 67.1 29.3 65.1
样例输出
53.8516
这个题重点是输出格式,其他的都还好,不过求三角形的斜边,然后就是函数的用法:
#include<iostream>
#include<cmath>
#include <iomanip>
using namespace std;
int main() {
int n;
cin >> n;
double x[100];
double y[100];
for (int i = 0; i < n; i++) {
cin >> x[i] >> y[i];
}
double longest = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
double dis = sqrt(((x[i] - x[j])*(x[i] - x[j]) + (y[i] - y[j])*(y[i] - y[j])));//求三角形斜边
if (dis > longest)
longest = dis;//找出最大
}
}
cout << fixed << setprecision(4) << longest << endl;
return 0;
}
第四题:
编程题#4:简单计算器
来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)
注意: 总时间限制: 1000ms 内存限制: 65536kB
描述
一个最简单的计算器,支持+, -, *, / 四种运算。仅需考虑输入输出为整数的情况,数据和运算结果不会超过int表示的范围。
输入
输入只有一行,共有三个参数,其中第1、2个参数为整数,第3个参数为操作符(+,-,*,/)。
输出
输出只有一行,一个整数,为运算结果。然而:
1. 如果出现除数为0的情况,则输出:Divided by zero!
2. 如果出现无效的操作符(即不为 +, -, *, / 之一),则输出:Invalid operator!
样例输入
第一组 1 2 + 第二组 1 0 / 第三组 1 0 XOR
样例输出
第一组 3 第二组 Divided by zero! 第三组 Invalid operator!
提示
可以考虑使用if和switch结构。
已经有提示了:所以,把符号用来做switch:
#include<iostream>
using namespace std;
int main() {
int a, b;
char c;
while (cin >> a >> b >> c) {
switch (c) {
case '+':
cout << a + b << endl;
break;
case '-':
cout << a - b << endl;
break;
case '*':
cout << a * b << endl;
break;
case '/'://只有除法需要特别注意
if(b!=0)
cout << a / b << endl;
else
cout <<"Divided by zero!"<< endl;//除数不能为0
break;
default:
cout << "Invalid operator!" << endl;
break;
}
}
return 0;
}
最后一题:
编程题#5:字符串插入
来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)
注意: 总时间限制: 1000ms 内存限制: 65536kB、
描述
有两个字符串str和substr,str的字符个数不超过10,substr的字符个数为3。(字符个数不包括字符串结尾处的'\0'。)将substr插入到str中ASCII码最大的那个字符后面,若有多个最大则只考虑第一个。
输入
输入包括若干行,每一行为一组测试数据,格式为
str substr
输出
对于每一组测试数据,输出插入之后的字符串。
样例输入
abcab eee 12343 555
样例输出
abceeeab 12345553
#include<iostream>
using namespace std;
int main() {
char str[11];
char substr[4];
int mark = 0;
cin >> str >> substr;
int biggest = str[0];
for (int i = 0; i < 10; i++) {
if (str[i] == '\0')
break;
else if (str[i] > biggest) {
biggest = str[i];
mark = i;
}
}
for (int i = 0; i < mark + 1; i++) {
cout << str[i];
}
for (int i = 0; i < 3; i++) {
cout << substr[i];
}
for (int i = mark + 1; i < 10; i++) {
if (str[i] == '\0')
break;
else
cout << str[i];
}
cout << endl;
return 0;
}
这道题我错了,但是我到现在不知道错在哪里,输出是对的。希望看出来的人指教