1. 程式人生 > 實用技巧 >C++學習 第二章 判斷語句

C++學習 第二章 判斷語句

第二章 判斷語句

P665 倍數

#include<iostream>

using namespace std;


int main(){
    int a,b;
    cin >> a >> b;
    if(a%b==0 || b%a==0){
        cout << "Sao Multiplos" << endl;
    }else{
        cout << "Nao sao Multiplos" << endl;
    }
    
    return 0;
}

P660 零食

#include<cstdio>

int main(){
    
    int x, y;
    scanf("%d%d", &x, &y);
    
    double price;
    if(x==1) price = 4;
    else if (x == 2) price = 4.5;
    else if (x == 3) price = 5;
    else if (x == 4) price = 2;
    else price = 1.5;
    
    printf("Total: R$ %.2lf\n",price*y);
    
    return 0;
}

P659

#include<iostream>
using namespace std;

int main(){
    
    double x;
    cin >> x;
    
    if (x >= 0 && x <= 25) cout << "Intervalo [0,25]" << endl;
    else if (x > 25 && x <= 50) cout << "Intervalo (25,50]" << endl;
    else if (x > 50 && x <= 75) cout << "Intervalo (50,75]" << endl;
    else if (x > 75 && x <= 100) cout << "Intervalo (75,100]" << endl;
    else cout << "Fora de intervalo" << endl;
    
    
    
    return 0;
}

P664

#include <cstdio>
#include<iostream>

using namespace std;

int main(){
    
    
    double a,b,c;
    cin>>a>>b>>c;
    if(a+b>c&&a+c>b&&b+c>a){
       printf("Perimetro = %.1lf\n", a + b + c); 
    }else{
        printf("Area = %.1lf\n", (a + b) * c / 2);
    }
    
    
    return 0;
    
    
}

P667 遊戲時間

#include <cstdio>
using namespace std;

int main(){
    
    int a, b;
    scanf("%d%d", &a, &b);

    int res;
    if (a < b) res = b - a;
    else  res = b - a + 24;

    printf("O JOGO DUROU %d HORA(S)\n", res);
    
    
    return 0;
}

P669 加薪

#include<cstdio>


int main(){
    
    
    double salary;
    scanf("%lf", &salary);

    double x;

    if (salary <= 400) x = 0.15;
    else if (salary <= 800) x = 0.12;
    else if (salary <= 1200) x = 0.1;
    else if (salary <= 2000) x = 0.07;
    else x = 0.04;

    printf("Novo salario: %.2lf\n", salary * (1 + x));
    printf("Reajuste ganho: %.2lf\n", salary * x);
    printf("Em percentual: %.0lf %%", x * 100);



    
    return 0;
}

P657

#include <iostream>

using namespace std;

int main()
{
    int a, b, c, d;
    cin >> a >> b >> c >> d;

    if (b > c && d > a && c + d > a + b && c > 0 && d > 0 && a % 2 == 0)
        cout << "Valores aceitos" << endl;
    else
        cout << "Valores nao aceitos" << endl;

    return 0;
}