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

A 各数字之和

题目描述
在这里插入图片描述

思路:一开始想的是回溯算法,后来想到暴力也不会超时就用的暴力

代码

#include <iostream>

using namespace std;


int n;
int cnt;
int sum;

bool check(int k){
    
    
	sum = 0;
	while(k){
    
    
		if(sum > 9)
			return false;
		sum += k % 10;
		k /= 10;
	}
	
	if(sum == 9)
		return true;
	return false;
}

int main(){
    
    
	
	cin>>n;
   
    for(int i = 9;i<=n;i++){
    
    
    	if(check(i)){
    
    
    		cnt++;
		}
	}
	 
	cout<<cnt<<endl;
	
	return 0;
}

B 直角三角形

题目描述
在这里插入图片描述
思路:暴力枚举

#include <iostream>
#include <cmath>

using namespace std;


int a,b,c;

int main(){
    
    
	
	cin>>c;
   	int s = c * c;
    for(int a = 1; a * a * 2 <= s;a++){
    
    
    	b = sqrt(s - a * a);
    	if(b * b + a * a == c * c){
    
    
    		cout<<a<<" "<<b<<endl;
    		return 0;
		}
	}

	return 0;
}

C 单位转换

题目描述

在这里插入图片描述
思路:字符串分离操作

代码

#include <stdio.h>
#include <string.h>

double p[15];

char str[20];

double res, t;
int main(){
    
    
	
	p[0] = 1;
	p[9] = 1024;
	p[11] = 1024 * 1024;
	p[5] = 1024 * 1024 * 1024;
	scanf("%s",str);
	int i = 0;
	int len = strlen(str);
	while(str[i] >= '0' && str[i] <= '9') res = res * 10 + (str[i] - '0'), i++;
	char c = str[i];
	char c2;
	if(str[len - 2] >= 'A' && str[len - 2] <= 'Z')
		c2 = str[len- 2];
	else
		c2 = str[len - 1];
	
	res *= p[c - 'B'];   //转换为B;
	res /=  (p[c2 - 'B']) * 1.0;
	
	printf("%.6f",res);
	
	return 0;
}

D 评委打分

题目描述
在这里插入图片描述

思路:题目说的很清楚

#include <stdio.h>
#include <string.h>

double avg;
int n, sum, num;
int max_value, min_value;

int max(int a, int b){
    
    
	return a > b ? a : b;
}

int min(int a, int b){
    
    
	return a < b ? a : b;
}
int main(){
    
    
	
	max_value = 0;
	min_value = 100;
	scanf("%d",&n);
	for(int i = 0;i<n;i++){
    
    
		scanf("%d",&num);
		sum += num;
		max_value = max(max_value,num);
		min_value = min(min_value,num);
		if(i >= 2){
    
    
			sum = sum - max_value - min_value;
			avg = sum / ((i - 1) * 1.0);
			printf("%.2f\n",avg);
			sum = sum + max_value + min_value;
		}
	
	}
	return 0;
}

E 儒略历

题目描述
在 1582 年之前,以 4 为倍数的年份为闰年。正常情况下,一年中一月到十二月的天数分别是 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 天。如果这年是闰年,那么二月则有 29 天。

但某位皇帝发现这么做其实不够准确,会造成误差,因此规定从 1582 年开始,以 4 为倍数的年份,除了以 100 为倍数且不为 400 的倍数年份,才是闰年。同时为了消除误差,规定 1582 年 10 月 4 日的下一天是 1582 年 10 月 15 日,中间的日期就当作不存在了。

现在给出日期,计算这个日期到公元 1 年 1 月 1 日经过的天数。

在这里插入图片描述
在这里插入图片描述

这道题目读了挺久才看懂,意思大概是:在1582年之前判断闰年是i % 4 == 0就行,而在之后,判断闰年(i % 4 == 0 && i % 100 != 0) || i % 400 == 0,而1582年10.15已及以后都要减去10天

思路:直接先枚举 i - 1年之前判断是否为闰年,确定加365还是366,而第i年就直接枚举月份,最后加上尾巴上的天数,然后如果是1582年要特判是否减10

代码

#include <iostream>
#include <map>
#include <string>
using namespace std;


int month1[12] = {
    
    31,29,31,30,31,30,31,31,30,31,30,31};
int month2[12] = {
    
    31,28,31,30,31,30,31,31,30,31,30,31};

string str;
int year, month, day;
int res;

map<string,int> mp;

int main(){
    
    
	
	mp["JAN"] = 1;
	mp["FEB"] = 2;
	mp["MAR"] = 3;
	mp["APR"] = 4;
	mp["MAY"] = 5;
	mp["JUN"] = 6;
	mp["JUL"] = 7;
	mp["AUG"] = 8;
	mp["SEP"] = 9;
	mp["OCT"] = 10;
	mp["NOV"] = 11;
	mp["DEC"] = 12;
	cin>>str;
	int i = 0;
	while(str[i] >= '0' && str[i] <= '9')	day = day * 10 + (str[i] - '0'),i++;
	string str1 = str.substr(i,3);
	month = mp[str1];
	i += 3;
	while(i < str.size())  year = year * 10 + (str[i] - '0'),i++;
	
	
	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)
                res += 366;
            else
                res += 365;
            
		}
		if(i == 1582){
    
    
            res -= 10;
        }
	}
	
	if(year < 1582){
    
    
		if(year % 4 == 0){
    
    
			for(int i = 0;i < month - 1 ;i++)
				res += month1[i];
		}else{
    
    
			for(int i = 0;i < month - 1 ;i++)
				res += month2[i];
		}
		
	}else{
    
    
		if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
    
    
			for(int i = 0;i < month - 1 ;i++)
				res += month1[i];
		}else{
    
    
			for(int i = 0;i < month - 1 ;i++)
				res += month2[i];
		}
	}
	
	res += day;
	
	if(year == 1582 && ((month == 10 && day >= 15) || month >= 11)){
    
    
        res -= 10;
    }
    
    cout<<res - 1<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/fjd7474/article/details/109904882