洛谷题单代码参考【入门2 分支结构】

洛谷题单代码参考【入门2 分支结构】

  • 数的性质
  • 闰年判断
  • Apples
  • 洛谷团队系统
  • 肥胖问题
  • 三位数排序
  • 月份天数
  • 不高兴的津津
  • 买铅笔
  • ISBN号码 (普及题)
  • 小玉家的电费
  • 小鱼的航程(改进版)
  • 三角函数
  • 陶陶摘苹果
  • 三角形分类(普及题)【注意先判断是否为三角形,再去判断是哪一类或两类或三类三角形】
  • ABC
#include 
using namespace std;

int main(){
	int n, flag = 1;
	cin >> n;
	int isEven = !(n & 1);
	int property2 = (n > 4 && n < 13);
	cout << (isEven & property2) << " "
	<< (isEven || property2) << " "
	<< (isEven && !property2 || !isEven && property2)<< " "
	<< ((!isEven) && (!property2)) << endl;
	
	return 0;
} 

#include 
using namespace std;

int isLeapYear(int year);
int main(){
	int year;
	cin >> year;
	
	cout << isLeapYear(year) << endl;
	
	return 0;
}
int isLeapYear(int year){
	if((!(year & 3) && (year % 100)) || (year % 400 == 0)){//被4整除且不被100整除 or 被四百整除 
		return 1;
	} 
	return 0; 
}

#include 
using namespace std;

int main(){
	int x;
	cin >> x;
	if(x < 2){
		cout << "Today, I ate " << x << " apple.";
	} else {
		cout << "Today, I ate " << x << " apples.";
	}
	
	
	return 0;
}

#include 
using namespace std;

int main(){
	int n;
	cin >> n;
	int local = 5 * n;
	int luogu = 3 * n + 11;
	cout << (local < luogu ? "Local" : "Luogu") << endl;
	
	return 0; 
}

#include 
#include 
using namespace std;

//BMI:体重(kg)/身高的平方(m) 
int main(){
	double m, n;
	cin >> m >> n;
	double bmi = m / pow(n, 2);
	if(bmi < 18.5){
		cout << "Underweight" << endl;
	} else if(bmi < 24){
		cout << "Normal" << endl;
	} else {
		cout << bmi << endl << "Overweight" << endl;
	}
	
	return 0;
}

#include 
#include  
using namespace std;

int main(){
	int num[4] = {0};
	for(int i = 0; i < 3; i++){
		cin >> num[i];
	}
	sort(num, num+3);
	for(int i = 0; i < 3; i++){
		cout << num[i] << " ";
	}
	
	
	return 0;
}

#include 
using namespace std;

int isLeapYear(int year){
	if((!(year & 3) && (year % 100)) || (year % 400 == 0)){
		return 1;
	}
	return 0;
} 
int main(){
	int year, month;
	int months[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	cin >> year >> month;
	if(isLeapYear(year)){
		months[2] += 1;
	}
	cout << months[month] << endl;
	
	return 0;
}

#include 
using namespace std;

int main(){
	int school[8], spareTime[8];
	int sum = 0, time = 0, isFlag = 1;
	for(int i = 0; i < 7; i++){
		cin >> school[i] >> spareTime[i];
	}
	sum = school[0] + spareTime[0];
	if(sum < 9){
		for(int i = 1; i < 8; i++){
		time = school[i - 1] + spareTime[i - 1];
		if((time > 8)&& (time > sum)){
			sum = school[i - 1] + spareTime[i - 1];
			cout << i << endl;
			isFlag = 0;
			break;
			}
		}
	} else {
		cout << 1 << endl;
		isFlag = 0;
	}
	if(isFlag){
		cout << 0 << endl;
	}
	
	return 0;
}

#include 
#include 
using namespace std;
typedef unsigned long long ull;

//用除法会更简单,这里自己绕蒙了,明白的时候不想改了 
int main(){
	int n; //需要的铅笔数量 
	cin >> n;
	int one, oneMoney, two, twoMoney, three, threeMoney;
	int oneTotal = 0, twoTotal = 0, threeTotal = 0;
	cin >> one >> oneMoney
	>> two >> twoMoney
	>> three >> threeMoney;
	int temp = one;
	for(int i = 1; i <= n; i++){
		if(temp >= n){
			oneTotal = i * oneMoney;
			break;
		}
		temp += one;
	}
	temp = two;
	for(int i = 1; i <= n; i++){
		if(temp >= n){
			twoTotal = i * twoMoney;
			break;
		}
		temp += two;
	}
	temp = three;
	for(int i = 1; i <= n; i++){
		if(temp >= n){
			threeTotal = i * threeMoney;
			break;
		}
		temp += three;
	}
	cout << min(oneTotal, min(twoTotal, threeTotal)) << endl;
	 
	
	return 0;
}

#include 
#include 
using namespace std;

int main(){
	string str;
	int sum = 0, index = 1, result = 0;
	int isFlag = 0;
	cin >> str;
	for(int i = 0; i < str.length() - 2; i++){
		if(i == 1 || i == 5){
			continue;
		}
		sum += (str[i] - '0') * index++;
	}
	
	result = sum % 11;
	isFlag = result == 10 ? 1 : 0;
	if((result == (str[12] - '0')) || ((isFlag) && (str[12] == 'X'))){
		cout << "Right" << endl;
	} else {
		str[12] = isFlag ? 'X' : result + '0';
		cout << str << endl;
	}
	
	return 0;
} 

#include 
#include 
using namespace std;

int main(){
	double total;
	cin >> total;
	if(total <= 150){
		cout << fixed << setprecision(1) << total * 0.4463 << endl;
	} else if(total <= 400){
		double free = 0.0;
		free = (total - 150) * 0.4663 + 150 * 0.4463;
		cout << fixed << setprecision(1) << free << endl;
	} else {
		double free = 0.0;
		free = (total - 400) * 0.5663 + (400 - 150) * 0.4663 + 150 * 0.4463;
		cout << fixed << setprecision(1) << free << endl;
	}
	
	
	return 0;
} 

#include 
using namespace std;

typedef unsigned long long ull;

int main(){
	ull sum = 0;
	int x, n, i = 0, temp = 0;
	cin >> x >> n;
	for(int i = 0; i < n; i++){
		if(x == 6){
			++x;
			continue;
		}
		if(x == 7){
			x = 1;
			continue;
		}
		sum += 250;
		++x;
	}
	cout << sum << endl;
	
	return 0;
}

#include 
#include 
using namespace std;

typedef unsigned long long ull;

//输入一组勾股数 a,b,c(a\neq b\neq c)a,b,c(a=b=c),
//用分数格式输出其较小锐角的正弦值。(要求约分。) 
//最小锐角的值就是最短直角边/斜边 
//约分可以使用求最大公约数 
int gcd(int a, int b);
int main(){
	ull a, b, c, nMax = 0, nMin = 0;
	int divisor = 0, dividend = 0;
	cin >> a >> b >> c;
	nMax = max(a, max(b, c));
	nMin = min(a, min(b, c));
	dividend = nMin / gcd(nMax, nMin);
	divisor = nMax / gcd(nMax, nMin);
	
	cout << dividend << "/" << divisor << endl;
	
	return 0;
}
int gcd(int a, int b){
	return b == 0 ? a : gcd(b, a % b);
}

#include 
using namespace std;

int applesHeight[20] = {0};

int main(){
	int tao, count = 0;
	for(int i = 0; i < 10; i++){
		cin >> applesHeight[i];
	}
	cin >> tao;
	tao += 30;
	for(int i = 0; i < 10; i++){
		if(tao >= applesHeight[i]){
			++count;
		}
	}
	cout << count << endl;
	
	return 0;
}

#include 
#include 
using namespace std;

int main(){
	int a, b, c, nMin = 0, nMax = 0, temp = 0, isFlag = 0;
	unsigned long long shortSide = 0, longSide = 0;
	cin >> a >> b >> c;
	//确保abc为由小到大的顺序 
	if(a > b){
		temp = a; 
		a = b;
		b = temp;
	}
	if(b > c){
		temp = b;
		b = c;
		c = temp;
	}
	if(a > b){
		temp = a; 
		a = b;
		b = temp;
	}
	
	shortSide = a * a + b * b;
	longSide = c * c;
	
	if(a + b <= c){//不是三角形 
		cout << "Not triangle" << endl;
	}
	else if(shortSide == longSide){ //直角三角形 
		cout << "Right triangle" << endl;
		isFlag = 1;
	} else if(shortSide > longSide){//锐角三角形 
		cout << "Acute triangle" << endl;
		isFlag = 1;
	} else if(shortSide < longSide){//钝角三角形 
		cout << "Obtuse triangle" << endl;
		isFlag = 1;
	} 
	if((a == b || b == c) && isFlag){//等腰三角形 
		cout << "Isosceles triangle" << endl;
	} 
	if((a == b) && (b == c) && isFlag){//等边三角形 
		cout << "Equilateral triangle" << endl;
	}
	
	return 0;
}

#include 
#include 
#include 
using namespace std;

int main(){
	int num[4] = {0};
	char ch[4] = {0};
	for(int i = 0; i < 3; i++){
		cin >> num[i];
	}
	for(int i = 0; i < 3; i++){
		cin >> ch[i];
	}
	sort(num, num + 3);
	for(int i = 0; i < 3; i++){
		cout << num[ch[i] - 'A'] << " " ;
	}
	
	
	return 0;
}

你可能感兴趣的:(洛谷算法题,c++,算法,c算法)