绪论
- 设某个学生班级共有n名同学,要求计算该班至少有两位同学具有相同生日的概率是多少(要求保留3位小数,假设一年有365天)?
#include<stdio.h> int main() { int n; scanf("%d",&n); int i = 0; double sum = 1.0; for(i=0;i<n;i++) { sum *= (365.0-i)/365.0; } printf("%.3f\n",1-sum); return 0; }
- 输出这两行语句。
// printf("Hello World!\n"); // cout<<"Hello World!"<<endl;
#include<stdio.h> int main() { printf("printf(\"Hello World!\\n\");\n"); printf("cout<<\"Hello World!\"<<endl;\n"); return 0; }
- 输入一个3位整数,输出该数各位上的数字之和。
#include<stdio.h> int main() { int a; scanf("%d",&a); int sum = 0; int i = 0; for(i=0;i<3;i++) { sum += a % 10; a = a / 10; } printf("%d\n",sum); return 0; }
- 输入一个四位数,将其加密后输出。方法是将该数每一位上的数字加9,然后除以10取余,作为该位上的新数字,最后将千位和十位上的数字互换,百位和个位上的数字互换,组成加密后的新四位数。
#include<stdio.h> #include<math.h> int main() { int n; scanf("%d",&n); int i = 0; int t = 0; int a = 0; for(i=0;i<4;i++) { t = n % 10; t = (t+9)%10; a += t * int(pow(10,i)); n = n / 10; } int thousand = a/1000; int hundred = a/100%10; int ten = a/10%10; int one = a%10; int temp; temp = thousand; thousand = ten; ten = temp; temp = hundred; hundred = one; one = temp; a = 1000*thousand+100*hundred+10*ten+one; printf("%d\n",a); return 0; }
- 体质指数BMI = 体重kg ÷ (身高m)^2
#include<stdio.h> int main() { int height,weight; double BMI = 0; scanf("%d %d",&weight,&height); BMI = weight/(height/100.0)/(height/100.0); printf("%.2lf\n",BMI); return 0; }
- 输入三个小写字母,将其转换成对应的大写字母。
#include<stdio.h> int main() { char a,b,c; scanf("%c%c%c",&a,&b,&c); printf("%c%c%c\n",a-32,b-32,c-32); return 0; }
分支结构
- 输入一个年份,判断该年份是否是闰年,若是闰年则输出1,否则输出0。年份能被4整除并且不能被100整除或者能被400整除的是闰年。
#include<stdio.h> int main(void) { int n; scanf("%d",&n); if((n%4==0 && n%100!=0)||n%400==0) { printf("1\n"); } else { printf("0\n"); } return 0; }
- 给定平面上任意三个点的坐标(x1,y1)、(x2,y2)、(x3,y3),检验它们能否构成三角形。如果这3个点不能构成三角形,则在一行中输出“Impossible”;若可以,则在一行中输出该三角形的周长和面积。
#include<stdio.h> #include<math.h> int main() { double x1,y1,x2,y2,x3,y3; scanf("%lf %lf %lf %lf %lf %lf",&x1,&y1,&x2,