期末编程测试(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<<'
';
}
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;
}