第三届“传智杯”全国大学生IT技能大赛练习赛-题解

比赛地址:https://www.luogu.com.cn/contest/37420

A 各数字之和

第三届“传智杯”全国大学生IT技能大赛练习赛-题解_第1张图片
暴力即可

#include 
using namespace std;
typedef long long ll;

bool check(int i){
     
	int sum = 0;
	while(i != 0){
     
		sum += i % 10;	
		i /= 10;
	}
	return sum == 9;

}

int main() {
     
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	int n;
    cin >> n;
    int cnt = 0;
	for(int i = 1;i<=n;++i){
     
		if(check(i)){
     
			++cnt;
		}
	}
    cout << cnt << endl;
	return 0;
}

B 直角三角形

第三届“传智杯”全国大学生IT技能大赛练习赛-题解_第2张图片
也是暴力

#include 
using namespace std;
typedef long long ll;

int main() {
     
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	int c;
    cin >> c;
    int a,b;
    double t;
    for(a = 1;a <= c; ++a){
     
        b = c * c - a * a;
        t = sqrt(b);
        if(t == (int)t){
     
            b = t;
            break;
        }
    }
    cout << a << ' ' << b << endl;
	return 0;
}

C 单位转换

第三届“传智杯”全国大学生IT技能大赛练习赛-题解_第3张图片
这题的关键就在于怎么读取到那些数据,根据题目条件计算即可

#include 
using namespace std;
typedef long long ll;
int main() {
     
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int origin;
	double after = 0.0;
    char cs[10];
    scanf("%d%s",&origin,cs);
    if(cs[0] == 'B'){
     
        if(cs[3] == 'B'){
     
            after = origin;
        }else if(cs[3] == 'K'){
     
            after = origin / pow(2,10);
        }else if(cs[3] == 'M'){
     
            after = origin / pow(2,20);
        }else if(cs[3] == 'G'){
     
            after = origin / pow(2,30);
        }
    }else if(cs[0] == 'K'){
     
        if(cs[4] == 'B'){
     
            after = origin * pow(2,10);
        }else if(cs[4] == 'K'){
     
            after = origin;
        }else if(cs[4] == 'M'){
     
            after = origin / pow(2,10);
        }else if(cs[4] == 'G'){
     
            after = origin / pow(2,20);
        }
    }else if(cs[0] == 'M'){
     
        if(cs[4] == 'B'){
     
            after = origin * pow(2,20);
        }else if(cs[4] == 'K'){
     
            after = origin * pow(2,10);
        }else if(cs[4] == 'M'){
     
            after = origin;
        }else if(cs[4] == 'G'){
     
            after = origin / pow(2,10);
        }
    }else if(cs[0] == 'G'){
     
        if(cs[4] == 'B'){
     
            after = origin * pow(2,30);
        }else if(cs[4] == 'K'){
     
            after = origin * pow(2,20);
        }else if(cs[4] == 'M'){
     
            after = origin * pow(2,10);
        }else if(cs[4] == 'G'){
     
            after = origin;
        }
    }
    printf("%.06lf\n",after);
	return 0;
}

D 评委打分

第三届“传智杯”全国大学生IT技能大赛练习赛-题解_第4张图片
遍历的时候同时记录最高和最低,再用当前总和-当前最高-当前最低除以剩下的数

#include 
using namespace std;
typedef long long ll;

int main() {
     
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	int n,t,ma,mi,su;
    cin >> n;
    ma = 0;
    mi = 100;
    for(int i = 0;i<n;++i){
     
        cin >> t;
        ma = max(ma,t);
        mi = min(mi,t);
        su += t;
        if(i >= 2){
     
            double d = (su - ma - mi) / (i - 1.0);
            printf("%.02lf\n",d);
        }
    }
	return 0;
}

E 儒略历

第三届“传智杯”全国大学生IT技能大赛练习赛-题解_第5张图片
第三届“传智杯”全国大学生IT技能大赛练习赛-题解_第6张图片

#include 
using namespace std;
typedef long long ll;
int run[] = {
     31,29,31,30,31,30,31,31,30,31,30,31};
int notrun[] = {
     31,28,31,30,31,30,31,31,30,31,30,31};

bool isRunNian(int i){
     
    if(i < 1582){
     
        return i % 4 == 0;
    }else{
     
        return i % 4 == 0 &&( (i % 100 == 0 && i % 400 == 0) || i % 100 != 0);
    }
}


int calcularYear(int year){
     
    int res = 0;
    for(int i = 1;i<year;++i){
     
        if(i < 1582){
     
            if(i % 4 == 0){
     
                res += 366;
            }else{
     
                res += 365;
            }
        }else{
     
            if(i % 4 == 0 &&( (i % 100 == 0 && i % 400 == 0) || i % 100 != 0)){
     
                res += 366;
            }else{
     
                res += 365;
            }
        }
        if(i == 1582){
     
            res -= 10;
        }
    }
    return res;
}

int calcularDay(int year,int month,int day){
     
    int res = 0;
    if(isRunNian(year)){
     
        for(int i = 1;i<month;++i){
     
            res += run[i-1];
        }
    }else{
     
        for(int i = 1;i<month;++i){
     
            res += notrun[i-1];
        }
    }
    res += day;
    if(year == 1582 && ((month == 10 && day >= 15) || month >= 11)){
     
        res -= 10;
    }
    return res;
}

int main() {
     
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	int day,month,year;
    char c1,c2,c3;
    scanf("%d%c%c%c%d",&day,&c1,&c2,&c3,&year);
    if(c1 == 'J' && c2 == 'A' && c3 == 'N'){
     
        month = 1;
    }else if(c1 == 'F' && c2 == 'E' && c3 == 'B'){
     
        month = 2;
    }else if(c1 == 'M' && c2 == 'A' && c3 == 'R'){
     
        month = 3;
    }else if(c1 == 'A' && c2 == 'P' && c3 == 'R'){
     
        month = 4;
    }else if(c1 == 'M' && c2 == 'A' && c3 == 'Y'){
     
        month = 5;
    }else if(c1 == 'J' && c2 == 'U' && c3 == 'N'){
     
        month = 6;
    }else if(c1 == 'J' && c2 == 'U' && c3 == 'L'){
     
        month = 7;
    }else if(c1 == 'A' && c2 == 'U' && c3 == 'G'){
     
        month = 8;
    }else if(c1 == 'S' && c2 == 'E' && c3 == 'P'){
     
        month = 9;
    }else if(c1 == 'O' && c2 == 'C' && c3 == 'T'){
     
        month = 10;
    }else if(c1 == 'N' && c2 == 'O' && c3 == 'V'){
     
        month = 11;
    }else if(c1 == 'D' && c2 == 'E' && c3 == 'C'){
     
        month = 12;
    }else{
     
        month = 0;
    }
    cout << calcularYear(year) + calcularDay(year,month,day) - 1 << endl;
	return 0;
}

你可能感兴趣的:(算法比赛)