本文主要是介绍C++判断语句(基础速通)ac-wing,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
倍数
#include <iostream>
using namespace std;
int a, b;
int main()
{cin >> a >> b;if (a % b == 0 || b % a == 0) cout << "Sao Multiplos";else cout << "Nao sao Multiplos";return 0;
}
零食
#include <iostream>
using namespace std;
int main() {double priceList[6] = {0, 4.00, 4.50, 5.00, 2.00, 1.50};int no, num;scanf("%d%d", &no, &num);printf("Total: R$ %.2lf\n", priceList[no] * num);return 0;
}
三角形
#include<iostream>
#include<cmath>
using namespace std;
int main()
{double a,b,c;cin>>a>>b>>c;//判断三边是否可构成三角形 两边之和>第三边 两边之差的绝对值<第三边if(a+b>c && fabs(a-b)<c) printf("Perimetro = %.1lf",a+b+c);else printf("Area = %.1lf",(a+b)*c/2);return 0;
}
游戏时间
#include<bits/stdc++.h>
using namespace std;
int s,e;
int ans;
int main()
{scanf("%d%d",&s,&e);if(e<=s){e=e+24;}ans=e-s;printf("O JOGO DUROU %d HORA(S)\n",ans);return 0;
}
DDD
#include<iostream>
#include<string>
using namespace std;
int main()
{int x;cin >> x ;int a[] ={61,71,11,21,32,19,27,31};//务必在定义数组时确保两数组对应位置相同string b[]={"Brasilia","Salvador","Sao Paulo","Rio de Janeiro","Juiz de Fora","Campinas","Vitoria","Belo Horizonte"};string t="a"; //工具变量t,存在目的:确定输入的x是否在a数组中;int i ;for(i=0;i<8;i++) {if(x == a[i]) {t = b[i]; //将a与b联系起来break; //程序运行过程中满足if则运行break结束循环,在这里结束for循环;提高运行效率。}}if(t == "a") printf("DDD nao cadastrado");else cout<<t;return 0;
}
- 普通解法
#include <iostream>
using namespace std;
int main()
{int x;cin>>x;if (x==61) cout<< "Brasilia" <<endl;else if(x==71) cout<< "Salvador" <<endl;else if(x==11) cout<< "Sao Paulo" <<endl;else if(x==21) cout<< "Rio de Janeiro" <<endl;else if(x==32) cout<< "Juiz de Fora" <<endl;else if(x==19) cout<< "Campinas" <<endl;else if(x==27) cout<< "Vitoria" <<endl;else if(x==31) cout<< "Belo Horizonte" <<endl;else cout << "DDD nao cadastrado" <<endl;return 0;
}
游戏时间
#include <iostream>using namespace std;int main()
{int a, b, c, d, minute1, minute2;cin >> a >> b >> c >> d;minute1 = a * 60 + b;minute2 = c * 60 + d;int hour = 0, minute = 0;int dec = minute2 - minute1;if(dec == 0){hour = 24;minute = 0;}else{if(dec < 0) //如 22:00 到 5:00 { dec += 24 * 60; //dec计算的是5点到22点的时间,共17小时。故应用一天的分钟(24*60)减去,才是正确经历的分钟}hour = dec / 60;minute = dec % 60;}cout << "O JOGO DUROU " << hour << " HORA(S) E " << minute << " MINUTO(S)";return 0;
}
简单排序
#include<cstdio>
#include<iostream>
using namespace std;
int main()
{int a,b,c;cin>>a>>b>>c;int x,y,z;x=max(a,max(b,c));y=min(a,min(b,c));z=a+b+c-x-y;cout<<y<<endl;cout<<z<<endl;cout<<x<<endl;cout<<endl;cout<<a<<endl;cout<<b<<endl;cout<<c<<endl;return 0;
}
这篇关于C++判断语句(基础速通)ac-wing的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!