【新手上路】洛谷入门2:分支结构题单题解

归来笑拈梅花嗅,春在枝头已十分


P2433 【深基1-2】小学数学 N 合一

代码如下:

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main() {
    int T;
    cin >> T;
    if (T == 1) {
        cout << "I love Luogu!";
    } else if (T == 2) {
        cout << 2 + 4 << " " << 10 - 2 - 4;
    } else if (T == 3) {
    	cout<<3<<endl<<12<<endl<<2<<endl;
    } else if (T == 4) {
    	printf("%.3lf\n",500.0/3.0);
    } else if (T == 5) {
    	cout<<15<<endl;
    } else if (T == 6) {
    	cout<<sqrt(6*6+9*9)<<endl;
    } else if (T == 7) {
    	cout<<110<<endl<<90<<endl<<0<<endl;
    } else if (T == 8) {
    	double const pi=3.141593;
    	double const r=5;
    	cout<<pi*r*2<<endl<<pi*r*r<<endl<<4.0/3*pi*r*r*r<<endl;
    } else if (T == 9) {
    	cout<<22<<endl;
    } else if (T == 10) {
    	cout<<9<<endl;
    } else if (T == 11) {
    	cout<<100.0/(8-5)<<endl;
    } else if (T == 12) {
    	cout<<13<<endl<<"R"<<endl;
    } else if (T == 13) {
    	double const pi=3.141593;
    	double V=pi*4*4*4*4/3+pi*10*10*10*4/3;
    	cout<<floor(pow(V,1.0/3))<<endl;
    } else if (T == 14) {
    	cout<<50<<endl;
    }
    return 0;
}

P5709 【深基2.习6】Apples Prologue / 苹果和虫子

代码如下:

#include<iostream>
#include<math.h>
using namespace std;

int main()
{
    int m,t,s;
    cin >> m >> t >> s;
    int left = m - ceil(float(s)/t);
    if(t == 0)
    {
        cout << "0";
    }else
    {
        if(left < 0)
        {
            cout << "0";
        }else
        {
            cout << left;
        }
    }

}

P5710 【深基3.例2】数的性质

代码如下:

#include<iostream>
using namespace std;

int A(int x)
{
	int r = 0;
	if (x % 2 == 0)
	{
		if (x > 4 && x <= 12)
		{
			r = 1;
		}
	}
	return r;
}

int U(int x)
{
	int r = 0;
	if (x % 2 == 0)
	{
		r = 1;
	}
	if (x > 4 && x <= 12)
	{
		r = 1;
	}
	return r;
}

int B(int x)
{
	int r = 0;
	if (x % 2 == 0)
	{
		r = 1;
		if (x > 4 && x <= 12)
		{
			r = 0;
		}
	}
	if (x > 4 && x <= 12)
	{
		r = 1;
		if (x % 2 == 0)
		{
			r = 0;
		}
	}
	return r;
}

int Z(int x)
{
	int r = 1;
	if (x % 2 == 0)
	{
		r = 0;
	}
	if (x > 4 && x <= 12)
	{
		r = 0;
	}
	return r;
}

int main()
{
	int x;
	cin >> x;
	cout << A(x) << " " << U(x) << " " << B(x) << " " << Z(x);

}

P5711 【深基3.例3】闰年判断

代码如下:

#include<iostream>
using namespace std;

int is_leap_year(int n)
{
	int r = 0;
	if (n % 4 == 0)
	{
		r = 1;
		if (n % 100 == 0 && n % 400 != 0)
		{
			r = 0;
		}
	}
	return r;
}

int main()
{
	int n;
	cin >> n;
	cout << is_leap_year(n);

}

P5712 【深基3.例4】Apples

代码如下:

#include<iostream>
using namespace std;

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

P5713 【深基3.例5】洛谷团队系统

代码如下:

#include<iostream>
using namespace std;

string compare(int n)
{
	string r;
	if (5 * n < 3 * n + 11)
	{
		r = "Local";
	}
	else
	{
		r = "Luogu";
	}
	return r;
}

int main()
{
	int n;
	cin >> n;
	cout << compare(n);
}

P5714 【深基3.例7】肥胖问题

代码如下:

#include<iostream>
using namespace std;

int main()
{
    float m,h;
    cin >> m >> h;
    float BMI = m/(h*h);
    if(BMI < 18.5)
    {
        cout << "Underweight";
    }
    if(BMI >= 18.5 && BMI < 24)
    {
        cout << "Normal";
    }
    if(BMI >= 24)
    {
        cout << BMI << endl << "Overweight";
    }
}

P5715 【深基3.例8】三位数排序

代码如下:

#include<iostream>
using namespace std;


int main()
{
    int a,b,c,temp;
    cin >> a >> b >> c;
    if(a >= b)
    {
        temp = a;
        a = b;
        b = temp;
    }
    if(a >= c)
    {
        temp = a;
        a = c;
        c = temp;
    }
    if(b >= c)
    {
        temp = b;
        b = c;
        c = temp;
    }
    cout << a << " " << b << " " << c;
}

P5716 【深基3.例9】月份天数

代码如下:

#include<iostream>
using namespace std;

int is_leap_year(int y)
{
    int r = 0;
    if(y % 4 == 0)
    {
        r = 1;
        if(y % 100 == 0 && y % 400 != 0)
        {
            r = 0;
        }
    }
    return r; 
}




int main()
{
    int y,m;
    cin >> y >> m;
    if(m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
    {
        cout << "31";
    }
    if(m == 4 || m == 6 || m == 9 || m == 11)
    {
        cout << "30";
    }
    if(m == 2)
    {
        if(is_leap_year(y) == 1)
        {
            cout << "29";
        }
        if(is_leap_year(y) == 0)
        {
            cout << "28";
        }
        
    }
    
}

P1085 [NOIP 2004 普及组] 不高兴的津津

代码如下:

#include<bits/stdc++.h>
using namespace std;
int main(){
    int hour[7],maxx = 0,ans=0;
    for(int i=0;i<7;++i) {
        int a,b;
        cin >> a >> b;
        hour[i] = a+b;
        maxx = max(a+b,maxx);
    }
    
    for(int i=0;i<7;++i) {
        if((hour[i] > 8) && (hour[i] == maxx)) {
            ans = i+1;
            break;
        }
    }
    cout << ans;
}

P1909 [NOIP 2016 普及组] 买铅笔

代码如下:

#include<bits/stdc++.h>
using namespace std;
int main(){
    int index = 1000000000;
    float n;
    cin >> n;
    for(int i=0;i<n;++i){
        float q;
        int pri;
        cin >> q >> pri;
        index = min(index,int(ceil(n/q)*pri));
    }
    cout << index << endl;
}

P5717 【深基3.习8】三角形分类

代码如下:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int l[3];
    cin >> l[0] >> l[1] >> l[2];
    // 调整长度
    sort(l, l + 3, greater<int>()); // 大到小
    if (l[1] + l[2] <= l[0]){
          cout << "Not triangle" << endl;
    }else {
        if (l[1] * l[1] + l[2] * l[2] == l[0] * l[0]) cout << "Right triangle" << endl;
        if (l[1] * l[1] + l[2] * l[2] > l[0] * l[0]) cout << "Acute triangle" << endl;
        if (l[1] * l[1] + l[2] * l[2] < l[0] * l[0]) cout << "Obtuse triangle" << endl;
        if (l[1] == l[2] || l[0] == l[1]) cout << "Isosceles triangle" << endl;
        if (l[0] == l[2]) cout << "Equilateral triangle" << endl;
    }
}

P1422 小玉家的电费

代码如下:

#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
    int n;
    cin >> n;
    if (n <= 150)
    {
        printf("%.1f", 0.4463 * n);
    }
    else if (n <= 400)
    {
        printf("%.1f", 0.4463 * 150 + (n - 150) * 0.4663);
    }
    else
    {
        printf("%.1f", 0.4463 * 150 + 0.4663 * 250 + (n - 400) * 0.5663);
    }
}

P1424 小鱼的航程(改进版)

代码如下:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int x, n;
    cin >> x >> n;
    if (x > 5)
    {
        n -= (7 - x + 1);
        int n_week_time = n / 7 * 1250;
        int left;
        if (n % 7 > 5)
        {
            left = 1250;
        }
        else
        {
            left = n % 7 * 250;
        }
        cout << n_week_time + left;
    }
    else
    {
        n = n - (7 - x + 1);
        int first_week = (5 - x + 1) * 250;
        int n_week_time = n / 7 * 1250;
        int left;
        if (n % 7 > 5)
        {
            left = 1250;
        }
        else
        {
            left = n % 7 * 250;
        }
        cout << first_week + n_week_time + left;
    }
}

P1888 三角函数

代码如下:

#include <bits/stdc++.h>
using namespace std;

int gcd(int big, int small)
{
    return 0 == small ? big : gcd(small, big % small);
}
int main()
{
    int a[3];
    cin >> a[0] >> a[1] >> a[2];
    sort(a, a + 3);
    // 最小锐角正弦值应该是最小边比上最大边
    cout << a[0] / gcd(a[2], a[0]) << "/" << a[2] / gcd(a[2], a[0]);
}

P1046 [NOIP 2005 普及组] 陶陶摘苹果

代码如下:

#include<bits/stdc++.h>
using namespace std;
int main(){
    int apple[11]={0};
    for(int i=0;i<11;++i) {
        cin >> apple[i];
    }
    int cnt=0,high = apple[10]+30;
    for(int i=0;i<10;++i) {
        if(apple[i] <= high) ++cnt;
    }
    cout << cnt;
}

P4414 [COCI 2006/2007 #2] ABC

代码如下:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int num[3];
    cin >> num[0] >> num[1] >> num[2];
    sort(num, num + 3);
    string s;
    cin >> s;
    cout << num[int(s[0] - 65)] << " " << num[int(s[1] - 65)]
         << " " << num[int(s[2] - 65)];
}

P1055 [NOIP 2008 普及组] ISBN 号码

代码如下:

#include<iostream>
#include<stdio.h>
using namespace std;
int main(){
    int a1,a2,a3,a4,a5,a6,a7,a8,a9;
    char a0;
    scanf("%1d-%1d%1d%1d-%1d%1d%1d%1d%1d-%c",&a1, &a2, &a3, &a4, &a5, &a6, &a7, &a8, &a9, &a0);
    int a = (a1+a2*2+a3*3+a4*4+a5*5+a6*6+a7*7+a8*8+a9*9)%11;
    if(int(a0-'0')==a || (a==10 && a0 == 'X')) cout << "Right";
    else if(a==10) printf("%d-%d%d%d-%d%d%d%d%d-X",a1,a2,a3,a4,a5,a6,a7,a8,a9);
    else printf("%d-%d%d%d-%d%d%d%d%d-%d",a1,a2,a3,a4,a5,a6,a7,a8,a9,a);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值