针对C语言入门OJ题(牛客网)
(简单)BC25-计算体重指数_牛客题霸_牛客网 (nowcoder.com)
解析:
要得到浮点数,要进行浮点数除法,这里就得保证 / 两端的操作数至少有一个数是浮点数。
参考代码:
//代码一 #include <stdio.h> int main() { //输入 int weight = 0; int height = 0; double bmi = 0.0; scanf("%d%d", &weight, &height); //计算BMI bmi = weight/((double)height*height/10000); //输出 printf("%.2lf\n", bmi); return 0; }
//代码二 #include <stdio.h> int main() { //输入 int weight = 0; int height = 0; double bmi = 0.0; scanf("%d%d", &weight, &height); //计算BMI bmi = weight/((height/100.0)*(height/100.0)); //输出 printf("%.2lf\n", bmi); return 0; }
(简单)BC26- 计算三角形的周长和面积_牛客题霸_牛客网 (nowcoder.com)
解析:
2. sqrt()函数 - 开平方的。
参考代码:
//代码一 #include <stdio.h> #include <math.h> int main() { int a = 0; int b = 0; int c = 0; scanf("%d %d %d", &a, &b, &c); float len = a+b+c;//周长 float p = (a+b+c)/2.0; float area = sqrt(p*(p-a)*(p-b)*(p-c));//面积 printf("circumference=%.2f area=%.2f\n", len, area); return 0; }
//代码二 #include <stdio.h> #include <math.h> int main() { double a = 0; double b = 0; double c = 0; double circumference = 0.0;//周长 double area = 0.0;//面积 //输入 scanf("%lf %lf %lf", &a, &b, &c); //计算 circumference = a + b + c; double p = circumference / 2; area = sqrt(p*(p-a)*(p-b)*(p-c)); //输出 printf("circumference=%.2lf area=%.2lf\n", circumference, area); return 0; }
(简单)BC27-计算球体的体积_牛客题霸_牛客网 (nowcoder.com)
解析:
- 注意输入输出,照着公式写代码。
- 这个题目,如果使用 float 来求解,答案的精度是不够的,所以在试错后,使用 double 类型合适。
- 这里要知道 float 和 double 为啥精度有区别。
参考代码:
//代码一 #include <stdio.h> int main() { double pi = 3.1415926; double r = 0.0; scanf("%lf", &r); double v = (4.0/3)*pi*(r*r*r); printf("%.3f\n", v); return 0; }
//代码二 #include <stdio.h> int main() { double r = 0.0;//半径 double V = 0.0;//体积 double pi = 3.1415926; //输入 scanf("%lf", &r); //计算 V = 4.0 /3 *pi*r*r*r; //输出 printf("%.3lf\n", V); return 0; }
(简单)BC10-成绩输入输出_牛客题霸_牛客网 (nowcoder.com)
解析:
按照格式输入输出就行。
参考代码:
//方法1 #include <stdio.h> int main() { int s1 = 0; int s2 = 0; int s3 = 0; scanf("%d %d %d", &s1, &s2, &s3); printf("score1=%d,score2=%d,score3=%d\n", s1, s2, s3); return 0; }
//方法2 #include <stdio.h> int main() { int i = 0; int score[3] = {0}; for(i=0; i<3; i++) { scanf("%d", &score[i]); } printf("score1=%d,score2=%d,score3=%d\n", score[0],score[1],score[2]); return 0; }
//方法3 #include <stdio.h> int main() { int i = 0; int score[3] = {0}; for(i=0; i<3; i++) { printf("score%d=%d", i+1, score[i]); if(i<2) printf(","); } return 0; }
(简单)BC38-变种水仙花_牛客题霸_牛客网 (nowcoder.com)
解析:
分析题目后发现, lily number 求和的每一项都是对同一个数( 10 / 100 / 1000... )的取模或者整除取商。 这样的话,产生10, 100,1000,10000 这些数字,分别对被判断的数字取模或者取商,然后再乘起来,计算和,最后再判断即可。
参考代码:
#include <stdio.h> int main() { int i = 0; for(i=10000; i<=99999; i++) { //判断i是否为lily number int j = 10; int sum = 0; for(j=10; j<=10000; j*=10) { sum += (i/j)*(i%j); } if(sum == i) printf("%d ", i); } return 0; }
(简单)BC20-kiki算数_牛客题霸_牛客网 (nowcoder.com)
解析:
理解 %(取模) 使用后的效果,按照题目所给的要求,理清思路即可。
参考代码:
//代码1 #include <stdio.h> int main() { int a = 0; int b = 0; //输入 scanf("%d %d", &a, &b); //计算 int sum = (a%100+b%100)%100; //输出 printf("%d\n", sum); return 0; }
//代码2 #include <stdio.h> int main() { int a = 0; int b = 0; //输入 scanf("%d %d", &a, &b); //计算 int sum = (a+b)%100; //输出 printf("%d\n", sum); return 0; }
(简单)BC21-浮点数的个位数字_牛客题霸_牛客网 (nowcoder.com)
解析:
方法1:采用强制类型转换,使得浮点数转换成一个整数。然后 %10 得到的余数就是个位数。
方法2:直接采用 %d 的形式读取这个浮点数,其实读取到的就是一个整数。然后 %10 得到余数就是个位数。
参考代码:
//代码一 #include <stdio.h> int main() { double d = 0; scanf("%lf", &d); int n = (int)d;//强制类型转换 printf("%d\n", n%10);//个数 - 取余数 return 0; }
//代码二 #include <stdio.h> int main() { int n = 0; scanf("%d", &n); printf("%d\n", n%10);//个数 - 取余数 return 0; }
(简单)BC22-你能活多少秒_牛客题霸_牛客网 (nowcoder.com)
解析:
- 这里的3.156乘以10的7次方,如果在C语言中表示要知道3.156e7。
- 计算机结果是秒数,可能太多,取long long类型来接收。
参考代码:
#include <stdio.h> int main() { int age = 0; //输入 scanf("%d", &age); //计算 long long seconds = age*3.156e7; //输出 printf("%lld", seconds); return 0; }