有3个整数 a,b,c,由键盘输入,输出此中最大的数- //有3个整数 a,b,c,由键盘输入,输出其中最大的数
- #include <stdio.h>
- int main(void)
- {
- int a, b, c;
- scanf("a=%d b=%d c=%d", &a, &b, &c);
- if (a > b)
- {
- int temp = a;
- a = b;
- b = temp;
- }//a < b
- if (a > c)
- {
- int temp = a;
- a = c;
- c = temp;
- }//a < c
- if (b > c)
- {
- int temp = b;
- b = c;
- c = temp;
- }//b < c
- printf("max = %d", c);
- return 0;
- }
复制代码 在VS编译器内会报C4996错误,解决见下文:(下同)
C4996 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. - EricsT - 博客园 (cnblogs.com)
运行结果:
从键盘输入一个小于1000的正数,要求输出它的平方根(如平方根不是整数,则输出其整数部门)要求在输入数据后先对其进行检查是否为小于1000的正数。若不是,则要求重新输入。- //从键盘输入一个小于1000的正数
- // 要求输出它的平方根(如平方根不是整数,则输出其整数部分)要求在输入数据后先对其进行检查是否为小于1000的正数。
- // 若不是,则要求重新输入。
- #include <stdio.h>
- #include <math.h>
- int main(void)
- {
- int a;
- printf("请输入小于1000的正数\n");
- scanf("%d", &a);
- while ((a >= 1000) || (a <= 0))
- {
- printf("请重新输入\n");
- scanf("%d", &a);
- }
- double sq = sqrt(a);
- int iSq = (int)sq;
- printf("%d", iSq);
- return 0;
- }
复制代码 运行结果:
结果一: 结果二: 结果三:
给出一百分制的成绩,要求输出成绩等级‘A’‘B’‘C’‘D’‘E’90以上为‘A’[80,89]为‘B’[70,79]为‘C’[60, 69]为‘D’60以下为‘E’- //有一个函数,当 x < 1 时,y = x;当 x >= 1 且 x < 10 时,y = 2x - 1;当 x >= 10时,y = 3x - 11;写程序,输入 x 的值,输出 y 相应的值
- #include <stdio.h>
- int main(void)
- {
- double x;
- scanf("%lf", &x);
- double y;
- if (x < 1)
- y = x;
- else if (x < 10)
- y = 2 * x - 1;
- else
- y = 3 * x - 11;
- printf("y=%lf\n", y);
- return 0;
- }
复制代码 运行结果:
结果一: 结果二: 结果三: 结果四: 结果五:
给出一个不多于5位的正整数,要求:
- 求出它是几位数
- 分别输出每一位数字
- 按逆序输出各位数字
- //给出一百分制的成绩,要求输出成绩等级‘A’‘B’‘C’‘D’‘E’90以上为‘A’[80,89]为‘B’[70,79]为‘C’[60, 69]为‘D’60以下为‘E’
- #include <stdio.h>
- int main(void)
- {
- int a;
- scanf("%d", &a);
- if (a < 60)
- {
- printf("E");
- return 0;
- }
- int i = a / 10;
- switch (i)
- {
- case 6:
- printf("D");
- break;
- case 7:
- printf("C");
- break;
- case 8:
- printf("B");
- break;
- case 9:
- case 10:
- printf("A");
- break;
- }
- return 0;
- }
复制代码 运行结果:
- //求出它是几位数
- #include <stdio.h>
- int main(void)
- {
- int a;
- scanf("%d", &a);
- int i = 0;
- while (a > 0)
- {
- a = a / 10;
- i++;
- }
- printf("是%d位数", i);
- return 0;
- }
复制代码 运行结果:
- //分别输出每一位数字
- #include <stdio.h>
- int main(void)
- {
- int a;
- scanf("%d", &a);
- while (a > 0)
- {
- printf("%d\n", a % 10);
- a = a / 10;
- }
- return 0;
- }
复制代码 运行结果:
- //按逆序输出各位数字
- #include <stdio.h>
- int main(void)
- {
- int a;
- scanf("%d", &a);
- while (a > 0)
- {
- printf("%d", a % 10);
- a = a / 10;
- }
- return 0;
- }
复制代码 运行结果:
[code]#include #includeint main(void){ double x, y, d; scanf("%lf %lf", &x, &y); if (x < 0) { if (y < 0)//-2, -2 d = sqrt(pow(x - (-2), 2) + pow(y - (-2), 2)); else//-2, 2 d = sqrt(pow(x - (-2), 2) + pow(y - 2, 2)); } else { if (y < 0)//2, -2 d = sqrt(pow(x - 2, 2) + pow(y - (-2), 2)); else//2, 2 d = sqrt(pow(x - 2, 2) + pow(y - 2, 2)); } if (d |