Coursera课程笔记----计算导论与C语言基础----Week 12
2024-09-05 20:42:20
期末编程测试(Week 12)
Quiz1 判断闰年
#include <iostream>
using namespace std;
int main()
{
int year;
cin >> year;
if(year % 100 == 0 && year % 400 != 0 || year % 4 != 0 || year % 3200 == 0)
cout<<'N'<<endl;
else cout<<'Y'<<endl;
return 0;
}
Quiz2 能被3,5,7整除的数
#include <iostream>
using namespace std;
int main()
{
int n;
while(cin >> n)
{
if (n % 3 == 0)
cout << '3' << ' ';
if (n % 5 == 0)
cout << '5' << ' ';
if (n % 7 == 0)
cout << '7' << ' ';
if (n % 3 != 0 && n % 5 != 0 && n % 7 != 0)
cout << 'n' << ' ';
cout<<'\n';
}
return 0;
}
Quiz3 最远距离
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;
int main()
{
int n;
cin >> n;
const int N = n;
double one[N],two[N];
double maxDistance = 0.0;
for (int i = 0; i < N; i++) {
cin>>one[i]>>two[i];
}
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
double distance = sqrt((one[i]-one[j])*(one[i]-one[j])+(two[i]-two[j])*(two[i]-two[j]));
if(distance > maxDistance)
maxDistance = distance;
}
}
cout << fixed << setprecision(4) <<maxDistance<<endl;
return 0;
}
Quiz4 简单计算器
#include <iostream>
using namespace std;
int main()
{
int a,b;
char c;
cin>>a>>b>>c;
if(c == '/' && b == 0)
{
cout<<"Divided by zero!"<<endl;
}
if(c!='/' && c!='+' && c!='-' && c!='*')
{
cout<<"Invalid operator!"<<endl;
}
if(c == '+')
{
cout<<a+b<<endl;
}
if(c == '-')
{
cout<<a-b<<endl;
}
if(c == '*')
{
cout<<a*b<<endl;
}
if(c == '/')
{
cout<<a/b<<endl;
}
return 0;
}
Quiz5 字符串插入
#include<cstdio>
#include<string>
#include<iostream>
using namespace std;
int main()
{
string str,substr;
while(cin>>str)
{
cin>>substr;
char temp1,temp2,temp;
int len1,len2,max;
len1=str.length();
len2=substr.length();
max=0;
for(int i=1;i<len1;i++)
{
temp1 = str[max];
temp2 = str[i];
if(temp1<temp2)
max=i;
}
temp=max+1;
str.insert(temp,substr);
cout<<str<<endl;
}
return 0;
}
最新文章
- SQL Server数据库性能优化之索引篇【转】
- Ueditor 编译发布后无法使用上传图片、附件等功能
- 调用newtonsoft.json反序列出错
- mac 系统通用快捷键(mac 下的应用多数会往这些标准看齐)(转:http://yang3wei.github.io/blog/2013/02/08/chen-ni-yu-mac-chen-ni-yu-xcode/)
- ipcs命令
- Entity Framework 教程
- ios 实现推送消息
- Duilib中Webbrowser事件完善使其支持判断页面加载完毕
- SVN 忽略获取和提交obj、bin文件夹
- Ubuntu 15.04 配置VPN的方法详解
- HDURevenge of Segment Tree(第二长的递增子序列)
- php引用传值详解
- Android视频录制从不入门到入门系列教程(二)————显示视频图像
- 2018-2019-2 20175306实验二面向对象程序设计《Java开发环境的熟悉》实验报告
- orcale mysql基本的分页查询法
- 如何使用onclick方式添加多个事件回调函数
- 强迫症犯了,忍不住赞一下slf4j包Logger.java的优雅代码
- [UE4]让子弹产生伤害
- day01-Python输出
- 04_web基础(六)之请求转发与重定向