牛客网140道编程题源代码(51—100)
题解51| # 三角形判断 #
https://www.nowcoder.com/practice/689ec1e742394e09b1059556fc167b65
#include <stdio.h> int main() { int a = 0; int b = 0; int c = 0; while(~scanf("%d %d %d",&a,&b,&c)) { if((a+b>c)&&(a+c>b)&&(b+c>a)) { if((a==b&&a!=c)||(a==c&&a!=b)||(b==c&&b!=a)) { printf("Isosceles triangle!\n"); } else if (a==b&&b==c) { printf("Equilateral triangle!\n"); } else { printf("Ordinary triangle!\n"); } } else { printf("Not a triangle!\n"); } } return 0; }
题解52| # 衡量人体胖瘦程度 #
https://www.nowcoder.com/practice/4d604603fae94a26b59b7bc18f363bc0
#include <stdio.h> int main() { int weight = 0; int hight = 0; while (scanf("%d %d", &weight, &hight) != EOF) { double BMI = weight / ((hight / 100.0) * (hight / 100.0)); if (BMI < 18.5) { printf("Underweight\n"); } else if (BMI >= 18.5 && BMI <= 23.8) { printf("Normal\n"); } else if (BMI > 23.9 && BMI <= 27.9) { printf("Overweight\n"); } else { printf("Obese\n"); } } return 0; }
题解53| # 计算一元二次方程 #
https://www.nowcoder.com/practice/7da524bb452441b2af7e64545c38dc26
#include <stdio.h> #include <math.h> int main() { double a = 0.0; double b = 0.0; double c = 0.0; while (scanf("%lf %lf %lf", &a, &b, &c) != EOF) { if (a == 0) { printf("Not quadratic equation\n"); } else { double disc = b * b - 4 * a * c; if (disc == 0) { if (b == 0 && c == 0) { printf("x1=x2=0.00\n"); } else printf("x1=x2=%.2f\n", (-b) / (2 * a)); } else if (disc > 0) { printf("x1=%.2lf;x2=%.2lf\n", (-b - sqrt(disc)) / (2 * a), (-b + sqrt(disc)) / (2 * a)); } else { double real = (-b) / (2 * a); double image = sqrt(-disc) / (2 * a); printf("x1=%.2lf-%.2lfi;x2=%.2lf+%.2lfi\n", real, image, real, image); } } } return 0; }
题解54| # 获得月份天数 #
https://www.nowcoder.com/practice/13aeae34f8ed4697960f7cfc80f9f7f6
#include <stdio.h> int main() { int year = 0; int month = 0; int days[12]={31,28,31,30,31,30,31,31,30,31,30,31}; while(~scanf("%d %d",&year,&month)) { int day = days[month-1]; if((year%4==0&&year%100!=0)||(year%400==0)) { if(month==2) { day+=1; } } printf("%d\n",day); } return 0; }
题解55| # 简单计算器 #
https://www.nowcoder.com/practice/b8f770674ba7468bb0a0efcc2aa3a239
#include <stdio.h> int main() { double n1 = 0; double n2 = 0; char op = 0; scanf("%lf %c %lf",&n1,&op,&n2); switch(op) { case '+': printf("%.4lf+%.4lf=%.4lf\n",n1,n2,n1+n2); break; case '-': printf("%.4lf-%.4lf=%.4lf\n",n1,n2,n1-n2); break; case '*': printf("%.4lf*%.4lf=%.4lf\n",n1,n2,n1*n2); break; case '/': if(n2==0.0) { printf("Wrong!Division by zero!\n"); } else { printf("%.4lf/%.4lf=%.4lf\n",n1,n2,n1/n2); } break; default: printf("Invalid operation!\n"); break; } return 0; }
题解56| # 线段图案 #
#include <stdio.h> int main() { int n = 0; while(scanf("%d",&n)!=EOF) { int i = 0; for(i=0;i<n;i++) { printf("*"); } printf("\n"); } return 0; }
题解57| # 正方形图案 #
#include <stdio.h> int main() { int n = 0; while(scanf("%d",&n)!=EOF) { int i = 0; for(i=0;i<n;i++) { int j = 0; for(j = 0;j<n;j++) { printf("* "); } printf("\n"); } } return 0; }
题解58| # 直角三角形图案 #
#include <stdio.h> int main() { int n = 0; while (scanf("%d", &n)!=EOF) { int i = 0; for (i = 1; i <= n; i++) { int j = 0; for (j = 1; j <= i; j++) { printf("* "); } printf("\n"); } } return 0; }
题解59| # 翻转直角三角形图案 #
#include <stdio.h> int main() { int n = 0; while (scanf("%d", &n) != EOF) { int i = 0; for (i = 0; i < n; i++) { int j = 0; for (j = 0; j < n - i; j++) { printf("* "); } printf("\n"); } } return 0; }
题解60| # 带空格直角三角形图案 #
#include <stdio.h> int main() { int n = 0; while(~scanf("%d",&n)) { int i = 0; for(i = 1;i<=n;i++) { int j = 0; for(j = 1;j<=n-i;j++) { printf(" "); } for(j = 1;j<=i;j++) { printf("* "); } printf("\n"); } } return 0; }
#include <stdio.h> int main() { int n = 0; while (~scanf("%d", &n)) { int i = 0; for (i = 0; i < n; i++) { int j = 0; for (j = 0; j < n - 1 - i; j++) { printf(" "); } for (j = 0; j <= i; j++) { printf("* "); } printf("\n"); } } return 0; }
#include <stdio.h> int main() { int n = 0; while(~scanf("%d",&n)) { int i = 0; for(i = 1;i<=n;i++) { int j = 0; for(j = 1;j<=n-i;j++) { printf(" "); } for(j = 1;j<=i;j++) { printf("* "); } printf("\n"); } } return 0; }
题解61| # 金字塔图案 #
#include <stdio.h> int main() { int n = 0; while(scanf("%d",&n)!=EOF) { int i = 0; for(i = 1;i<=n;i++) { int j = 0; for(j=1;j<=n-i;j++) { printf(" "); } for(j=1;j<=i;j++) { printf("* "); } printf("\n"); } } return 0; }
题解62| # 翻转金字塔图案 #
#include <stdio.h> int main() { int n = 0; while (~scanf("%d", &n)) { int i = 0; for (i = 0; i < n; i++) { int j = 0; for (j = 0; j < i; j++) { printf(" "); } for (j = 0; j < n - i; j++) { printf("* "); } printf("\n"); } } return 0; }
题解63| # 菱形图案 #
#include <stdio.h> int main() { int n = 0; while(scanf("%d",&n)!=EOF) { int i = 0; for(i=0;i<n;i++) { int j = 0; for(j = 0;j<n-i;j++) { printf(" "); } for(j=0;j<=i;j++) { printf("* "); } printf("\n"); } for(i = 0;i<n+1;i++) { int j = 0; for(j = 0;j<i;j++) { printf(" "); } for(j = 0;j<n+1-i;j++) { printf("* "); } printf("\n"); } } return 0; }
题解64| # K形图案 #
#include <stdio.h> int main() { int n = 0; while(scanf("%d",&n)!=EOF) { int i = 0; for(i = 0;i<n;i++) { int j = 0; for(j = 0;j<n+1-i;j++) { printf("* "); } printf("\n"); } for(i = 0;i<n+1;i++) { int j = 0; for(j = 0;j<=i;j++) { printf("* "); } printf("\n"); } } return 0; }
题解65| # 箭形图案 #
#include <stdio.h> int main() { int n = 0; while(~scanf("%d",&n)) { int i= 0; for(i = 0 ;i < n ;i++) { int j = 0; for(j = 0;j<n-i;j++) { printf(" "); } for(j = 0;j<=i;j++) { printf("*"); } printf("\n"); } for(i = 0;i<n+1;i++) { int j = 0; for(j=0;j<i;j++) { printf(" "); } for(j=0;j<n+1-i;j++) { printf("*"); } printf("\n"); } } return 0; }
题解66| # 反斜线形图案 #
#include <stdio.h> int main() { int n = 0 ; while(scanf("%d",&n)!=EOF) { int i = 0; for(i = 0;i<n;i++) { int j = 0; for(j = 0;j<i;j++) { printf(" "); } printf("*\n"); } } return 0; }
#include <stdio.h> int main() { int n = 0 ; while(scanf("%d",&n)!=EOF) { int i = 0; for(i = 0;i<n;i++) { int j = 0; for(j = 0;j<n;j++) { if(i == j) { printf("*"); } else { printf(" "); } } printf("\n"); } } return 0; }
题解67| # 正斜线形图案 #
#include <stdio.h> int main() { int n = 0; while(scanf("%d",&n)!=EOF) { int i = 0; for(i = 0;i<n;i++) { int j = 0; for(j = 0;j<n-1-i;j++) { printf(" "); } printf("*\n"); } } return 0; }
#include <stdio.h> int main() { int n = 0; while (scanf("%d", &n) != EOF) { int i = 0; for (i = 0; i < n; i++) { int j = 0; for (j = 0; j < n; j++) { if (i + j == n - 1) printf("*"); else printf(" "); } printf("\n"); } } return 0; }
题解68| # X形图案 #
#include <stdio.h> int main() { int n = 0; while(scanf("%d",&n)!=EOF) { int i = 0; for(i = 0;i<n;i++) { int j = 0; for(j = 0;j<n;j++) { if(i==j||i+j==n-1) { printf("*"); } else { printf(" "); } } printf("\n"); } } return 0; }
#include <stdio.h> int main() { int n = 0; while(scanf("%d",&n)!=EOF) { int i = 0; for(i = 0;i<n;i++) { int j = 0; for(j = 0;j<n;j++) { if(i==j) { printf("*"); } else if(i+j==n-1) { printf("*"); } else { printf(" "); } } printf("\n"); } } return 0; }
题解69| # 空心正方形图案 #
#include <stdio.h> int main() { int n = 0; while (scanf("%d", &n) != EOF) { int i = 0; for (i = 0; i < n; i++) { int j = 0; for (j = 0; j < n; j++) { if (i == 0 || i == n - 1) { printf("* "); } else if (j == 0 || j == n - 1) { printf("* "); } else { printf(" "); } } printf("\n"); } } return 0; }
题解70| # 空心三角形图案 #
#include <stdio.h> int main() { int n = 0; while (scanf("%d", &n) != EOF) { int i = 0; for (i = 0; i < n; i++) { int j = 0; for (j = 0; j < n; j++) { if (j == 0 || i == j || i == n - 1) { printf("* "); } else { printf(" "); } } printf("\n"); } } return 0; }
题解71| # 新年快乐 #
#include <stdio.h> int main() { printf("Happy New Year*2019*\n"); return 0; }
题解72| # 平均身高 #
#include <stdio.h> int main() { double hight[5] = { 0 }; int i = 0; double sum = 0; double div = 0; for (i = 0; i < 5; i++) { scanf("%lf", &hight[i]); sum = sum + hight[i]; } div = sum / 5; printf("%.2lf\n", div); return 0; }
题解73| # 挂科危险 #
#include <stdio.h> int main() { int credit = 0; scanf("%d", &credit); if (credit >= 0 && credit <= 3) { printf("Good\n"); } else if (credit >= 4 && credit <= 9) { printf("Danger\n"); } else { printf("Danger++\n"); } return 0; }
题解74| # HTTP状态码 #
#include <stdio.h> int main() { int n = 0; while (scanf("%d", &n) != EOF) { if (n == 200) { printf("OK\n"); } else if (n == 202) { printf("Accepted\n"); } else if (n == 400) { printf("Bad Request\n"); } else if (n == 403) { printf("Forbidden\n"); } else if (n == 404) { printf("Not Found\n"); } else if (n == 500) { printf("Internal Server Error\n"); } else if (n == 502) { printf("Bad Gateway\n"); } } return 0; }
题解75| # 数字三角形 #
#include <stdio.h> int main() { int n = 0; while (scanf("%d", &n) != EOF) { int i = 0; for (i = 1; i <= n; i++) { int j = 0; for (j = 1; j <= i; j++) { printf("%d ", j); } printf("\n"); } } return 0; }
题解76| # 公务员面试 #
#include <stdio.h> int main() { int score[7] = { 0 }; while (~scanf("%d %d %d %d %d %d %d", &score[0], &score[1], &score[2], &score[3], &score[4], &score[5], &score[6])) { int i = 0; int max = 0; int sum = 0; int min = 100; for (i = 0; i < 7; i++) { sum += score[i]; if (score[i] > max) { max = score[i]; } if (score[i] < min) { min = score[i]; } } printf("%.2lf\n", (sum - min - max) / 5.0); } return 0; }
题解77| # 有序序列插入一个数 #
题解78| # #
题解79| # #
题解80| # #
题解81| # #
题解82| # #
题解83| # #
题解84| # #
题解85| # #
题解86| # #
题解87| # #
题解88| # #
题解89| # #
题解90| # #
题解91| # #
题解92| # #
题解93| # #
题解94| # #
题解95| # #
题解96| # #
题解97| # #
题解98| # #
题解99| # #
题解100| # #
题解101| # 班级成绩的输入输出 #
#include <stdio.h> int main() { double score[5] = { 0.0 }; int i = 0; for (i = 0; i < 5; i++) { int j = 0; double sum = 0.0; for (j = 0; j < 5; j++) { scanf("%lf", &score[j]); printf("%.1lf ", score[j]); sum += score[j]; } printf("%.1lf\n", sum); } return 0; }
#include <stdio.h> int main() { double score[5]={0.0}; int i = 0; while(i<5) { int j = 0; double sum = 0.0; while(j<5) { scanf("%lf",&score[j]); sum +=score[j]; j++; } j = 0; while(j<5) { printf("%.1lf ",score[j]); j++; } printf("%.1lf\n",sum); i++; } return 0; }