读入一个整数a,如果a为偶数在屏幕上输出yes。
#include <iostream> using namespace std; int main(){ int a; cin >> a; if(a%2==0) cout << "yes" << endl; return 0; }
读入一个数,若这个数大于1并且小于100,则输出yes。
#include <iostream> using namespace std; int main(){ int a; cin >> a; if((a>1)&&(a<100)) cout << "yes" << endl; return 0; }
输入三个整数,按从大到小的顺序输出。
#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 << endl; return 0; }
输入温度t的值,判断是否适合晨练。(25<=t<=30,则适合晨练ok,否则不适合no)
#include <iostream> using namespace std; int main(){ int t; cin >> t; if( (t>=25) && (t<=30)) cout << "ok!" << endl; else cout << "on!" << endl; return 0; }
输入三个数,输出其中最大的数。
#include <iostream> using namespace std; int main(){ float a,b,c,maxn; cin >> a >> b >> c; if(a>b && a>c) maxn = a; else if ( b>a && b>c) maxn = b; else maxn = c; cout << maxn << endl; return 0; }
#include <iostream> using namespace std; int main(){ float a,b,c,maxn; cin >> a >> b >> c; maxn = a; if(b>maxn) maxn=b; if(c>maxn) maxn=c; cout << maxn << endl; return 0; }
乘坐飞机时,当乘客行李小于等于20公斤时,按每公斤1.68元收费,大于20公斤时,按每公斤1.98元收费,编程计算收费(保留2位小数)。
#include <iostream> using namespace std; int main(){ float w,s; scanf("%f",&w); if(w<=20){ s = w*1.68; }else{ s = w*1.98; } printf("%.2f\n",s); return 0; }
#include <iostream> using namespace std; int main() { int n; cin >> n; string s = "positive"; if (n ==0) s = "zero"; else if (n<0) s = "negative"; cout << s << endl; return 0; }
#include <iostream> using namespace std; int main() { double n; cin >> n; if (n<0) n = -n; printf("%.2f",n); return 0; }
#include <iostream> using namespace std; //03:奇偶数判断 int main() { int n; cin >> n; string s = "even"; if (n%2==1) s = "odd"; cout << s << endl; return 0; }
#include <iostream> using namespace std; //04:奇偶ASCII值判断 int main() { char c=getchar(); string s = "NO"; if (c%2==1) s = "YES"; cout << s << endl; return 0; }
#include <iostream> using namespace std; //05:整数大小比较 int main() { int x,y; cin >> x >> y; string s = ">"; if (x==y) s = "="; else if (x <y) s = "<"; cout << s << endl; return 0; }
#include <iostream> using namespace std; //06:判断是否为两位数 int main() { int i; cin >> i; if (i>=10 && i<=99) i = 1; else i = 0; cout << i << endl; return 0; }
#include <iostream> using namespace std; //07:收集瓶盖赢大奖 int main() { int x,y,z; cin >> x >> y; if (x>=10 || y >= 20) z = 1; else z = 0; cout << z << endl; return 0; }
#include <iostream> using namespace std; //08:判断一个数能否同时被3和5整除 int main() { int n; cin >> n; string s = "NO"; if (n % 3==0 && n % 5==0) s = "YES"; cout << s << endl; return 0; }
#include <iostream> using namespace std; int main(){ int a; //装整数的筐 cin >> a ; if(a%3==0 && a%5==0 && a%7==0){ cout << 3 << " " << 5 << " " << 7 << endl; }else if(a%3==0 && a%5==0){ cout << 3 << " " << 5 << endl; }else if(a%5==0 && a%7==0){ cout << 5 << " " << 7 << endl; }else if(a%3==0 && a%7==0){ cout << 3 << " " << 7 << endl; }else if(a%3==0){ cout << 3 << endl; }else if(a%5==0){ cout << 5 << endl; }else if(a%7==0){ cout << 7 << endl; }else{ cout << "n" << endl; } return 0; }
#include <iostream> using namespace std; //10:有一门课不及格的学生 int main() { int c1,c2; cin >> c1 >> c2; string s = "0"; if ((c1<60 && c2>=60 )|| (c1>=60 && c2 <60)) s = "1"; cout << s << endl; return 0; }
根据从键盘上输入的表示星期几的数字,对应输出它的英文名称。
#include <iostream> using namespace std; int main(){ int weekday; cin >> weekday; switch(weekday){ case 1: cout << "Monday" << endl; break; case 2: cout << "Tuesday" << endl; break; case 3: cout << "Wednessday" << endl; break; case 4: cout << "Thursday" << endl; break; case 5: cout << "Friday" << endl; break; case 6: cout << "Saturday" << endl; break; case 7: cout << "Sunday" << endl; break; default: cout << "Input error!" << endl; break; } return 0; }
一个最简单的计算器支持+,-,*,/四种运算。输入只有一行:两个参加运算的数和一个操作符(+,-,*,/)。输出运算表达式的结果。考虑下面两种情况:
输入样例:
34 56 +
输出样例:
90
样例程序:
#include <iostream> using namespace std; int main(){ float num1,num2; char opt; cin >> num1 >> num2 >> opt; switch(opt){ case '+': cout << num1+num2 << endl; break; case '-': cout << num1-num2 << endl; break; case '*': cout << num1*num2 << endl; break; case '/': if(num2!=0) cout << num1/num2 << endl; else cout << "Divided by zero!" << endl; break; default: cout << "Invalid operator!" << endl; } return 0; }
期末来临了,班长小Q决定将剩余的班费x元钱,用于购买若干支钢笔奖励给一些学习好、表现好的同学。
已知商店里有三种钢笔,它们的单价分别为6元,5元和4元。
小Q想买尽量多的笔(鼓励尽量多的同学),同时他又不想有剩余的钱。
请你编一个程序,帮小Q制定出一种买笔的方案。
#include <iostream> using namespace std; int main(){ int a,b,c,x,y; cin >> x; c=x/4; //能买4元的多少只 y=x%4; //都买4元的剩余多少钱 switch(y){ case 0: //都买4元的正好 a=0; b=0; break; case 1: //买4元的剩1元,少买一个4元的多买一个5元的 a=0; b=1; c--; break; case 2: //买4元的剩2元,少买一个4元的多买一个6元的 a=1; b=0; c--; break; case 3: //买4元的剩3元,少买两个4元的,多买一个5元的和一个6元的 a=1; b=1; c-=2; break; } cout << a << " " << b << " " << c << endl; return 0; }
#include <iostream> using namespace std; //11:晶晶赴约会 int main() { int d; cin >> d; string s = "YES"; if (d==1 || d==3 || d == 5 ) s = "NO"; cout << s << endl; return 0; }
#include <iostream> using namespace std; //12:骑车与走路 int main() { double m; cin >> m; string s = "All"; double bikeTime = m / 3.0 + 50; double walkTime = m / 1.2; if ( bikeTime < walkTime ) s = "Bike"; else if (bikeTime > walkTime) s = "Walk"; cout << s << endl; return 0; }
#include <iostream> using namespace std; //13:分段函数 int main() { double n,f=0; cin >> n; if ( n>=0 && n < 5 ) f = -n + 2.5; else if (n>=5 && n<10) f = 2 -1.5*(n-3)*(n-3); else if (n>=10 && n<20) f = n / 2 - 1.5; printf("%.3f",f); return 0; }
#include <iostream> #include <cmath> using namespace std; //14:计算邮资 int main() { double w; char c; int f; cin >> w >> c; if (w <= 1000) f = 8; else f = ceil((w-1000)/500)*4 + 8; if (c=='y') f = f + 5; printf("%d",f); return 0; }
#include <iostream> using namespace std; int main(){ int a,b,c; cin >> a >> b >> c; if(a>b && b>c){ cout << a << endl; }else if(b>c){ cout << b << endl; }else{ cout << c << endl; } return 0; }
#include <iostream> using namespace std; //16:三角形判断 int main() { int x,y,z; cin >> x >> y >>z; string s = "yes"; if (x + y <= z) s = "no"; else if(x + z <= y) s = "no"; else if(y + z <= x) s = "no"; cout << s << endl; return 0; }
#include <iostream> using namespace std; //17:判断闰年 int main() { int y; cin >> y; string s = "N"; if (y % 4 ==0 && !(y % 100==0 && y % 400 !=0) && !(y%3200==0)) s = "Y"; cout << s << endl; return 0; }
#include <iostream> using namespace std; //18:点和正方形的关系 int main() { int x,y; cin >>x >> y; string s = "no"; if (x<=1 && x>=-1 && y <=1 && y>=-1) s = "yes"; cout << s << endl; return 0; }
#include <iostream> using namespace std; //18:点和正方形的关系 int main() { int x,y; char c; int result = 0; cin >>x >> y >> c; string s = ""; switch(c) { case '+': result = x + y; break; case '-': result = x - y; break; case '*': result = x * y; break; case '/': if (y==0) s = "Divided by zero!"; else result = x / y; break; default: s = "Invalid operator!"; } if (s=="") cout << result << endl; else cout << s << endl; return 0; }
#include <iostream> #include <cmath> using namespace std; int main() { double a,b,c; cin >> a >> b >> c; double b2 = b * b; double dt = 4 * a * c; double a2 = 2 * a; if (b2 < dt) { double r = -b / a2; if (b==0){ r = 0; } double v = sqrt(dt-b2)/a2; printf("x1=%.5f+%.5fi;x2=%.5f-%.5fi",r,v,r,v); }else{ double x1 = (-b + sqrt(b2-dt))/a2; double x2 = (-b - sqrt(b2-dt))/a2; if (b2>dt) { printf("x1=%.5f;x2=%.5f",x1,x2); }else{ printf("x1=x2=%.5f",x1); } } return 0; }
#include <iostream> using namespace std; int main(){ int n; //苹果数量 int x; //虫子吃一个苹果花的时间 int y; //虫子吃了多少时间的苹果 cin >> n>> x >>y; //分别输入苹果个数,虫子吃一个苹果的时间,虫子吃了多长时间的苹果 if(n>0){ //如果箱子里有苹果 if(x>0){ if(y>0){ int m = y/x; //计算吃了几个整苹果 if(y%x!=0) m = m + 1; if(n-m>0) cout << n-m << endl; else cout << "0" << endl; }else{ cout << n << endl; } }else{ cout << n << endl; } }else{ //否则,箱子里没有苹果 cout << "0" << endl; } return 0; }