当前位置:   article > 正文

C语言 判断是不是字母|三个数中的最大值|变种水仙花

C语言 判断是不是字母|三个数中的最大值|变种水仙花

1.判断是不是字母

1.1描述

KiKi想判断输入的字符是不是字母,请帮他编程实现。

1.2输入描述:

多组输入,每一行输入一个字符。

1.3输出描述:

针对每组输入,输出单独占一行,判断输入字符是否为字母,输出内容详见输出样例。

1.4示例

输入:
A
6
输出:
A is an alphabet.
6 is not an alphabet.

1.5不使用函数的

  1. #include <stdio.h>
  2. int main()
  3. {
  4. int n = 0;
  5. while (scanf("%c", &n))
  6. {
  7. if ((n >= 'A' && n <= 'Z') || (n >= 'a' && n <= 'z'))
  8. printf("%c is an alphabet.\n", n);
  9. else
  10. printf("%c is not an alphabet.\n",n);
  11. getchar();
  12. }
  13. return 0;
  14. }

1.6使用函数的

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. int main()
  4. {
  5. int n = 0;
  6. while (scanf("%c", &n))
  7. {
  8. if (isalpha(n))
  9. printf("%c is an alphabet.\n", n);
  10. else
  11. printf("%c is not an alphabet.\n", n);
  12. getchar();
  13. }
  14. return 0;
  15. }

2.判断三个数字中的最大值

  1. #include <stdio.h>
  2. int main()
  3. {
  4. int i = 0;
  5. int max = 0;//先初始化max,假设最大值为0
  6. int score = 0;
  7. for (i = 0; i < 3; i++)
  8. {
  9. scanf("%d", &score);
  10. if (score > max)
  11. max = score;
  12. }
  13. printf("%d\n", max);
  14. return 0;
  15. }

3.变种水仙花

3.1描述

变种水仙花数 - Lily Number:把任意的数字,从中间拆分成两个数字,比如1461 可以拆分成(1和461), (14和61), (146和1), 如果所有拆分后的乘积之和等于自身,则是一个Lily Number。

例如:

655 = 6 * 55 + 65 * 5

1461 = 1 * 461 + 14 * 61 + 146 * 1

求出 5位数中的所有 Lily Number。

3.2输入描述:

3.3输出描述:

一行,5位数中的所有 Lily Number,每两个数之间间隔一个空格。

  1. #include <stdio.h>
  2. #include <math.h>
  3. int main()
  4. {
  5. int i = 0;
  6. for (i = 10000; i < 100000; i++)
  7. {
  8. int sum = 0;
  9. int j = 0;
  10. for (j = 1; j <= 4; j++)
  11. {
  12. int k = (int)pow(10, j);
  13. sum += (i % k) * (i / k);
  14. }
  15. if (sum == i)
  16. printf("%d ", i);
  17. }
  18. return 0;
  19. }

 4.结语

今天的经验分享就到这里,有喜欢的朋友可以点赞➕评论➕收藏➕关注,如果有不懂的地方可以咨询博主,谢谢大家支持博主!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/347674
推荐阅读
相关标签
  

闽ICP备14008679号