原来的代码
#include<iostream> using namespace std; int main(){ int num;//需要判断的数字 bool a,b,c,d;//四人喜欢与否 bool property1 = (num%2==0); bool property2 = (num>4&&num<=12);//两个性质 cin>>num; a = property1&&property2;//同时 b = property1||property2;//至少一个 c = (property1&&(!property2))||((!property1)&&property2);//刚好一个 d = (!property1)&&(!property2);//都不 cout<<a<<" "<<b<<" "<<c<<" "<<d; }AC的代码
#include<iostream> using namespace std; int main(){ int num;//需要判断的数字 int a,b,c,d;//四人喜欢与否 cin>>num; bool property1 = (num%2==0); bool property2 = (num>4&&num<=12);//两个性质 a = property1&&property2;//同时 b = property1||property2;//至少一个 c = (property1&&(!property2))||((!property1)&&property2);//刚好一个 d = (!property1)&&(!property2);//都不 cout<<a<<" "<<b<<" "<<c<<" "<<d; }num未赋值就计算了会出错
题目描述
津津上初中了。妈妈认为津津应该更加用功学习,所以津津除了上学之外,还要参加妈妈为她报名的各科复习班。另外每周妈妈还会送她去学习朗诵、舞蹈和钢琴。但是津津如果一天上课超过八个小时就会不高兴,而且上得越久就会越不高兴。假设津津不会因为其它事不高兴,并且她的不高兴不会持续到第二天。请你帮忙检查一下津津下周的日程安排,看看下周她会不会不高兴;如果会的话,哪天最不高兴。
输入格式
输入包括7行数据,分别表示周一到周日的日程安排。每行包括两个小于10的非负整数,用空格隔开,分别表示津津在学校上课的时间和妈妈安排她上课的时间。
输出格式
一个数字。如果不会不高兴则输出0,如果会则输出最不高兴的是周几(用1,2,3,4,5,6,7分别表示周一,周二,周三,周四,周五,周六,周日)。如果有两天或两天以上不高兴的程度相当,则输出时间最靠前的一天。
简洁的代码,我很满意。
#include<iostream> using namespace std; int main(){ int a,b;//上课时间 int max=0; //max储存大于8h的最大上课时间,若无大于8h即没有不高兴的星期则为0 int day=0; //day储存max对应的星期数,若max为0即没有不高兴的星期,则为0 for(int i=1;i<=7;i++){ cin>>a>>b; if((a+b)>8&&(a+b)>max){//上课时间大于8且大于已有max day=i; max=a+b; }//等于max的情况不会更换day,故输出时间是最靠前 } cout<<day; return 0; }但是还是比不上大佬
#include <iostream> using namespace std; int main() { int t1,t2; int maxf=0,t=0;//最大量,最大的天数 int now=0;//记录现在是哪天 cin>>t1>>t2; now++; if(t1+t2>maxf) maxf=t1+t2,t=now; cin>>t1>>t2; now++; if(t1+t2>maxf) maxf=t1+t2,t=now; cin>>t1>>t2; now++; if(t1+t2>maxf) maxf=t1+t2,t=now; cin>>t1>>t2; now++; if(t1+t2>maxf) maxf=t1+t2,t=now; cin>>t1>>t2; now++; if(t1+t2>maxf) maxf=t1+t2,t=now; cin>>t1>>t2; now++; if(t1+t2>maxf) maxf=t1+t2,t=now; cin>>t1>>t2; now++; if(t1+t2>maxf) maxf=t1+t2,t=now; cout<<t; return 0; }大佬原话:
这题目在顺序与分支里居然用了For
实在是太不科学了
所以本题的正解是一串if
O(1)复杂度,脚踩STD
大佬博客地址
题目描述
给出三条线段 a,b,c 的长度,均是不大于 10000 的整数。打算把这三条线段拼成一个三角形,它可以是什么三角形呢?
如果三条线段不能组成一个三角形,输出Not triangle; 如果是直角三角形,输出Right triangle; 如果是锐角三角形,输出Acute triangle; 如果是钝角三角形,输出Obtuse triangle; 如果是等腰三角形,输出Isosceles triangle; 如果是等边三角形,输出Equilateral triangle。如果这个三角形符合以上多个条件,请分别输出,并用换行符隔开。 输入格式
无 输出格式
无 输入输出样例
无 说明/提示
当两短边的平方和大于一长边的平方,说明是锐角三角形。
当两短边的平方和等于一长边的平方,说明是直角三角形。
当两短边的平方和小于一长边的平方,说明是钝角三角形。
80分代码
#include<iostream> #include<algorithm> using namespace std; int main(){ int a[3]; cin>>a[0]>>a[1]>>a[2]; sort(a,a+3);//排序得出短边和最长边,a[2]为最长边 if(a[0]+a[1]<=a[2]){ cout<<"Not triangle"<<endl; } if(a[0]*a[0]+a[1]*a[1]==a[2]*a[2]){ cout<<"Right triangle"<<endl; } else if(a[0]*a[0]+a[1]*a[1]>a[2]*a[2]){ cout<<"Acute triangle"<<endl; } else if(a[0]*a[0]+a[1]*a[1]<a[2]*a[2]){ cout<<"Obtuse triangle"<<endl; } if(a[0]==a[1]||a[1]==a[2]){//长边相等或短边相等 cout<<"Isosceles triangle"<<endl; } if(a[0]==a[2]){//最长边与最短边相等必定等边 cout<<"Equilateral triangle"<<endl; } return 0; }问题在
if(a[0]+a[1]<=a[2]){ cout<<"Not triangle"<<endl; }不是三角形就跳出来不要判断了
#include<iostream> #include<algorithm> using namespace std; int main(){ int a[3]; cin>>a[0]>>a[1]>>a[2]; sort(a,a+3);//排序得出短边和最长边,a[2]为最长边 if(a[0]+a[1]<=a[2]){ cout<<"Not triangle"<<endl; return 0; } if(a[0]*a[0]+a[1]*a[1]==a[2]*a[2]){ cout<<"Right triangle"<<endl; } else if(a[0]*a[0]+a[1]*a[1]>a[2]*a[2]){ cout<<"Acute triangle"<<endl; } else if(a[0]*a[0]+a[1]*a[1]<a[2]*a[2]){ cout<<"Obtuse triangle"<<endl; } if(a[0]==a[1]||a[1]==a[2]){//长边相等或短边相等 cout<<"Isosceles triangle"<<endl; } if(a[0]==a[2]){//最长边与最短边相等必定等边 cout<<"Equilateral triangle"<<endl; } return 0; }