[NJUST]19级上机考试题解
(先赞后看,已成习惯)
上机考试结束了,成绩也出来了,大家有没有考到500分吖 ,我整理了一些同学的满分代码,以及我对题目的一些个人理解。
A 日历题
题意:
- 输入 t 天,输出离2000年1月1日为 t 天的日期并说明是星期几
样例:
- 输入:1
- 输出:2000/1/2 Sunday
正确率:38.42%
这个只要乖乖的一天一天的加就可以了,因为题目 t 的最大取值也只有4000,基本不会存在超时的情况。
注意闰年的判断:
if((nowyear % 4 == 0 && nowyear % 100 != 0) || nowyear % 400 == 0)
因为题目很和善地给了固定的起始日期,所以省了很多事。可以选择整月整月地加。
下面给出我自己的代码
#include<iostream>
#include<stdio.h>
using namespace std;
const char week[8][20] = {" ","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
const int month[13] = {0 , 31 , 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int main()
{
int n;
while(cin>>n)
{
int nowyear = 1999;//由于下面写了nowyear++
int wd = 6;//周几
int Fday = 28, Yday = 365;
int nm = 1,nd = 1;//现在的月份,现在的天数
if(n == 0)
{
cout<<nowyear + 1<<'/'<<nm<<'/'<<nd<<' '<<week[wd]<<endl;
continue;
}
while(n > 0)
{
nowyear++;
nm = 1;
if((nowyear % 4 == 0 && nowyear % 100 != 0) || nowyear % 400 == 0)
{
Fday = 29; //判断闰年
}
else
{
Fday = 28;
}
for(int i = 1;i <= 12;i++)//月份演变
{
if(i == 2)//二月特殊处理
{
if(n >= Fday)
{
n -= Fday;
nm++;
wd = (wd + Fday) % 7;//周数演变
}
else//加完了的情况
{
nd += n;
wd = (wd + n) % 7;
n = 0;
break;
}
continue;
}
if(n >= month[i])
{
n -= month[i];
nm++;
wd = (wd + month[i]) % 7;
}
else
{
nd += n;
wd = (wd + n) % 7;
n = 0;
break;
}
}
}
if(wd == 0)//周日的wd是余数0
wd = 7;
if(nm == 13)//刚刚好一年的情况,n=0时没有重置
{
nm = 1;
nowyear++;
}
cout<<nowyear<<'/'<<nm<<'/'<<nd<<' '<<week[wd]<<endl;
}
}
基本上第一题是最难的一题了…
B 最大体积
题意:
- 一个长方体长a,宽b,高c (a,b,c都是整数) , t为长方体的棱长,输入 t ,输出长方体体积的最大值
样例:
- 输入:20
- 输出:4
正确率:73.16%
两种做法,可以老老实实直接每一种情况都遍历,记录最大值,另一种做法是求( t / 4 ) mod 3 ,如果是0那么最大值是( t / 12) * ( t / 12) * ( t / 12) , 如果是1那么最大值是( t / 12 + 1) * ( t / 12) * ( t / 12),如果是2那么最大值是( t / 12 + 1) * ( t / 12 + 1) * ( t / 12)。这里给出第一种做法的ac代码:
#include<iostream>
using namespace std;
int main(){
int p;
cin>>p;
p=p/4;
int l,w,h;
int v1=1;
int v2;
for(l=1;l<p-1;l++){
for(w=1;w<p-l;w++){
h=p-l-w;
v2=l*w*h;
if(v2>v1) v1=v2;
}
}
cout<<v1<<endl;
return 0;
}
C 字符串题
题意:
- 输入一个字符串(只有a ~ z和A ~ Z),去掉重复字符,并按照ASCII码大小排序后输出。
样例:
- 输入:ddFFaac
- 输出:Facd
正确率:57.89%
这题按题意来做,首先string a储存字符串,先利用algorithm里的sort函数快速排序(或者自己写个冒泡排序,不过还是希望能掌握algorithm里的一些常用函数,因为真的好用),再string b,遍历a,如果 a[ i ] != a[ i - 1] , 那么b[bnum++] = a[ i ] 输出b。
但是,懒如我发现题目只给了英文字母的大小写那几个,直接用数组记录a到z和A到Z有没有出现过这个字母,然后一股脑输出就行了,根本没有排序(大家别学我)
ac代码:
#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
#include <algorithm>
#include <cctype>
#include <typeinfo>
#include <set>
using namespace std;
int main(){
string str;
cin >> str;
sort(str.begin(),str.end());
for(int i=0;i<str.size();i++){
if(i==0){
cout << str[0];
continue;
}
if(str[i]==str[i-1])
continue;
else
cout << str[i];
if(i==str.size()-1)
cout <<endl;
}
return 0;
}
D 平均值
题意:
- 测试数据有多组,每输入12个浮点数,输出一个平均值(格式:$0.00)注:保留两位小数
样例:
- 输入:
12.03
34.56
77.88
903.123
99.22
123.00
2
0.01
724.2
23.55
34.42
52.1 - 输出:$173.84
正确率:64.21%
这题主要考格式的输出。(可能是为了降低难度,不用处理多组数据也能ac)
可以用printf("%.2f",a);
也可以用cout<<setiosflags(ios::fixed)<<setprecision(2);
比较简单,不分析了:
#include<iostream>
#include<string.h>
#include<algorithm>
#include<iomanip>
using namespace std;
int main()
{double a[12];
for(int i=0;i<12;i++)
{
cin>>a[i];
}
double total=0;
double pingjun=0;
for(int i=0;i<12;i++)
{total+=a[i];
}
pingjun=total/12;
cout<<"$"<<setiosflags(ios::fixed)<<setprecision(2)<<pingjun;
}
E 字符统计
题意:
- 输入一个字符串(只有a ~ z和A ~ Z),统计最大连续重复字母的数量
样例:
- 输入:abbcccd
- 输出:3
正确率:50%
输入字符串,遍历数组,用max值记录最大连续个数,用now值记录目前连续个数,每次判断if(str[ i - 1] == str[ i ]),true则now++,false则now = 1
ac代码:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <queue>
#include <string>
#include <cmath>
using namespace std;
int main(int argv, char* argc[])
{
int cases;
cin>>cases;
while(cases--)
{
char x[101];
cin>>x;
int max= 1;
for (int i = 0;x[i];i++)
{
if(x[i+1] == x[i])
{
int j = 1;
for(;x[i+j]&&x[i+j]==x[i];j++);
max = (j>max)?j:max;
}
}
cout<<max<<endl;
}
return 0;
}
都看到这了,还不给个免费的赞吗QwQ