码迷,mamicode.com
首页 > 编程语言 > 详细

C,C++语法基础 | 判断语句

时间:2020-11-08 17:27:35      阅读:17      评论:0      收藏:0      [点我收藏+]

标签:输出   names   uil   宽度   最大   进制   include   c++   ios   

判断语句

printf的格式输出

可以使用%5d这样来补空格,还有就是%05d这样子可以补0,还有%-5d是从右边补0

int a = 1,b=12,c=123;
printf("%5d\n",a); //     1
printf("%05d\n",a); // 00012
printf("%-5d\n",a); // 123  

同时浮点数也是可以的这么操作的,但是要注意的是浮点数的第一个数字表示的是总的宽度.

double f = 12.45;
printf("%05.1lf\n",f); 
printf("%-5.1lf\n",f);

习题二

倍数

技术图片

#include<cstdio>
#include<iostream>

using namespace std;

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

其实没有必要先找出一个最大的, 就是一个或者条件就可以a%b==0 || b%a==0

#include<cstdio>
#include<iostream>

using namespace std;

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

零食

技术图片

#include<cstdio>
#include<iostream>
using namespace std;

int main(){
    double a[6] = {0,4,4.5,5,2,1.5};
    int x,y;
    cin >> x >> y;
    printf("Total: R$ %.2lf",a[x]*y);
    return 0;
}

区间

技术图片

#include<cstdio>
#include<iostream>
using namespace std;

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

三角形

技术图片

#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;
}

三角形的判断条件: 任意两边之和大于第三边

游戏时间

技术图片

#include<cstdio>
#include<iostream>
using namespace std;

int main(){
    
    int a,b;
    cin >> a >> b;
    printf("O JOGO DUROU %d HORA(S)",a==b?24:(24 - a + b)%24);
    
    return 0;
}

加薪

技术图片

#include<cstdio>
#include<iostream>

using namespace std;

int main(){
    
    double salary;
    cin >> salary;
    
    double x,y;
    int z;
    if(salary<=400){
        z = 15;
        
    }else if(salary<=800){
        z = 12;
        
    }else if(salary<=1200){
        z = 10;
    }else if(salary<=2000){
        z = 7;
    }else{
        z = 4;
    }
        
    x = salary + salary * z / 100.;
    y = x - salary;
    printf("Novo salario: %.2lf\nReajuste ganho: %.2lf\nEm percentual: %d %\n",x,y,z);
    
    return 0;
}

要注意的是%是一个特殊字符,如果要输出的话需要进行转义%%,这个是比较特殊的.

动物

技术图片

#include <iostream>

using namespace std;

int main()
{
    string a, b, c;

    cin >> a >> b >> c;

    if (a == "vertebrado")
    {
        if (b == "ave")
        {
            if (c == "carnivoro") cout << "aguia" << endl;

            else if(c == "onivoro") cout << "pomba" << endl;
        }

        if (b == "mamifero")
        {
            if (c == "onivoro") cout << "homem" << endl;
            else if (c == "herbivoro") cout << "vaca" <<endl;
        }
    }
    if (a == "invertebrado")
    {
        if (b == "inseto")
        {
            if (c == "hematofago") cout << "pulga" << endl;
            else if (c == "herbivoro") cout <<"lagarta" << endl;
        }
        if (b == "anelideo")
        {
            if (c == "hematofago") cout << "sanguessuga" << endl;
            else if (c == "onivoro") cout << "minhoca" << endl;
        }
    }
    return 0;
}

选择练习1

技术图片

#include<iostream>
#include<cstdio>

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;
}

DDD

技术图片

#include<cstdio>
#include<iostream>

using namespace std;


int main(){
    string citys[100];
    citys[61]= "Brasilia";
    citys[71]= "Salvador";
    citys[11]= "Sao Paulo";
    citys[21]= "Rio de Janeiro";
    citys[32]= "Juiz de Fora";
    citys[19]= "Campinas";
    citys[27]= "Vitoria";
    citys[19]= "Campinas";
    citys[31]= "Belo Horizonte";
    
    int x;
    cin >> x;
    cout << (citys[x]==""? "DDD nao cadastrado" :citys[x]) << endl;
    return 0;
}

点的坐标

技术图片

#include<iostream>
using namespace std;


int main(){
    double a,b;
    cin >> a >> b;
    if(a>0 && b>0)cout << "Q1" << endl;
    else if(a<0 && b>0)cout << "Q2" << endl;
    else if(a<0 && b<0)cout << "Q3" << endl;
    else if(a>0 && b<0)cout << "Q4" << endl;
    else if(a==0 && b==0) cout << "Origem" << endl;
    else if(b == 0)cout << "Eixo X" << endl;
    else if(a == 0)cout << "Eixo Y" << endl;
    
    return 0;
}

三角形类型

技术图片

#include<iostream>
#include<algorithm>

using namespace std;

int main(){
    double a[10];
    cin >> a[0] >> a[1] >> a[2];
    sort(a,a+3,greater<double>());
    if(a[0] >= a[1] + a[2]) cout << "NAO FORMA TRIANGULO" << endl;
    else if(a[0]*a[0] == a[1]*a[1] + a[2]*a[2]) cout << "TRIANGULO RETANGULO" << endl;
    else if(a[0]*a[0] > a[1]*a[1] + a[2]*a[2]) cout << "TRIANGULO OBTUSANGULO" << endl;
    else if(a[0]*a[0] < a[1]*a[1] + a[2]*a[2]) cout << "TRIANGULO ACUTANGULO" << endl;
    if(a[0] == a[1] && a[0] == a[2]) cout << "TRIANGULO EQUILATERO" << endl;
    else if(a[0]==a[1] || a[0]==a[2] || a[1]==a[2])cout << "TRIANGULO ISOSCELES" << endl; // 之前等边的情况已经过滤了
    
    return 0;
}

这个注意了,钝角,锐角,直角三角形是一个if...else,然后等腰和等边又是一个if...else

游戏时间2

技术图片

这种进制转换的题目,统一转换为最小单位进行处理. 如果转为分钟处理,就可以不用理会那些借位的问题. (这种基础题还是很值得再写的)

#include<iostream>
using namespace std;

int main(){
    int a1,a2,b1,b2;
    cin >> a1 >> a2 >> b1 >> b2;
    // 进制转换统一转换为最小单位进行处理
    int start = a1 * 60 + a2;
    int end = b1 * 60 + b2;
    int t = end - start;
    if(t<=0)t+=24*60;
    printf("O JOGO DUROU %d HORA(S) E %d MINUTO(S)",t/60,t%60);
    
    
    return 0;
}

技术图片

#include<iostream>

using namespace std;

int main(){
    double x;
    cin >> x;
    if(x<=2000)cout << "Isento" << endl;
    if(x>2000 && x<=3000)printf("R$ %.2lf",(x-2000)*0.08);
    if(x>3000 && x<=4500)printf("R$ %.2lf",1000*0.08 + (x-3000)*0.18);
    if(x>4500)printf("R$ %.2lf",1000*0.08 + 1500*0.18 + (x-4500)*0.28);
    
    
    return 0;
}

抓住一点,之前的税率是已经写好的了.

简单排序

技术图片

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;

int main(){
    int a[10],b[10];
    for(int i=0;i<3;i++)cin>>a[i];
    memcpy(b,a,sizeof(a));
    sort(a,a+3);
    for(int i=0;i<3;i++)cout<<a[i]<<endl;
    cout << endl;
    for(int i=0;i<3;i++)cout<<b[i]<<endl;
    
    return 0;
}

一元二次方程公式

技术图片

#include<iostream>
#include<cmath>
using namespace std;


int main(){
    double a,b,c;
    cin >> a >> b >> c;
    if(a==0 || b*b - 4*a*c < 0)cout << "Impossivel calcular";
    else{
        double r1,r2;
        r1 = (-b-sqrt( b*b - 4*a*c))/(2*a),r2=(-b+sqrt( b*b - 4*a*c))/(2*a);
        printf("R1 = %.5lf\n",r1);
        printf("R2 = %.5lf\n",r2);
    }
    
    return 0;
}

平均数3

技术图片

#include<iostream>
using namespace std;


int main(){
    double a,b,c,d;
    cin >> a >> b >> c >> d;
    double media = (a*2+b*3+c*4+d)/10;
    if(media>=7.0){
        printf("Media: %.1lf\n",media);
        cout << "Aluno aprovado." << endl;
    }else if(media >=5.0){
        printf("Media: %.1lf\n",media);
        cout << "Aluno em exame." << endl;
        double e;
        cin >> e;
        printf("Nota do exame: %.1lf\n",e);
        media = (media+e)/2;
        cout << (media>=5.0?"Aluno aprovado.":"Aluno reprovado.") << endl;
        printf("Media final: %.1lf\n",media);
    }else{
        printf("Media: %.1lf\n",media);
        cout << "Aluno reprovado." << endl;
    }
    
    
    return 0;
}

C,C++语法基础 | 判断语句

标签:输出   names   uil   宽度   最大   进制   include   c++   ios   

原文地址:https://www.cnblogs.com/Rowry/p/13943387.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!