C语言基础习题50例(三)11-15

你们看出什么了吗

你们看出神马了吗(*^_^*)

习题11

有一对兔子,从出生后第 3 个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少

实现思路: 从第1个月起,兔子对数分别为1、1、2、3、5、8、13、21...,显然是斐波拉契数列。

代码如下:

1#include<stdio.h> 2 3int main(){ 4 int i, f1 = 1, f2 = 1; 5 for(i = 1; i < 21; i++){ 6 printf("%10d%10d", f1, f2); 7 f1 += f2; 8 f2 += f1; 9 if(i % 2 == 0){ 10 printf("\n"); 11 } 12 } 13 14 return 0; 15}

打印:

1 1 1 2 3 2 5 8 13 21 3 34 55 89 144 4 233 377 610 987 5 1597 2584 4181 6765 6 10946 17711 28657 46368 7 75025 121393 196418 317811 8 514229 832040 1346269 2178309 9 3524578 5702887 9227465 14930352 10 24157817 39088169 63245986 102334155 11

习题12

. 判断101-200之间有多少个素数,并输出所有素数。

实现思路: 可以用一个函数来判断一个数是否是素数,是则输出。

代码如下:

1#include <stdio.h> 2#include <math.h> 3 4int main(){ 5 int isPrime(int n); 6 int i, count = 0; 7 for(i = 100; i < 201; i++){ 8 if(isPrime(i)){ 9 printf("%6d", i); 10 count++; 11 if(count % 5 == 0){ 12 printf("\n"); 13 } 14 } 15 } 16 17 return 0; 18} 19 20int isPrime(int n){ 21 int i, prime = 1; 22 for(i = 2; i <= sqrt(n); i++){ 23 if(n % i == 0){ 24 prime = 0; 25 break; 26 } 27 } 28 return prime; 29}

打印:

1 101 103 107 109 113 2 127 131 137 139 149 3 151 157 163 167 173 4 179 181 191 193 197 5 199

习题13

打印出所有的水仙花数。 水仙花数是指一个三位数,其各位数字立方和等于该数本身。 例如: 153是一个水仙花数,因为 153=1^3^ + 5 ^3^ + 3^3^。

实现思路: 通过函数来判断一个数是否是水仙花数,并通过循环来判断所有的数。

代码如下:

1#include<stdio.h> 2#include <math.h> 3 4int main(){ 5 int isNarci(int n); 6 int i, count = 0; 7 for(i = 100; i < 1000; i++){ 8 if(isNarci(i)){ 9 printf("%6d", i); 10 count++; 11 if(count % 5 == 0){ 12 printf("\n"); 13 } 14 } 15 } 16 17 return 0; 18} 19 20int isNarci(int n){ 21 int i, j, k, temp, narci = 0; 22 i = n / 100; 23 temp = n % 100; 24 j = temp / 10; 25 k = temp % 10; 26 if(n == (pow(i, 3) + pow(j, 3) + pow(k, 3))){ 27 narci = 1; 28 } 29 return narci; 30}

打印:

1 153 370 371 407

练习14

将一个正整数分解质因数。 例如:输入90,打印出 90=2*3*3*5。

实现思路: 使用嵌套循环,逐步分解。

代码如下:

1#include<stdio.h> 2 3int main(){ 4 static int i, num; 5 printf("Please input a integer:\n"); 6 scanf("%d", &num); 7 printf("%d=", num); 8 while(num){ 9 for(i = 2; i <= num; i++){ 10 if(num % i == 0){ 11 printf("%d", i); 12 num /= i; 13 if(num != 1){ 14 printf("*"); 15 } 16 break; 17 } 18 } 19 if(num == 1){ 20 break; 21 } 22 } 23 24 return 0; 25}

打印:

1Please input a integer: 2120 3120=2*2*2*3*5

外层还可以使用for循环,如下:

1#include <stdio.h> 2 3int main(){ 4 int n,i; 5 printf("Please input a integer:\n"); 6 scanf("%d",&n); 7 printf("%d=",n); 8 for(i=2; i <= n; i++){ 9 while(n % i == 0){ 10 printf("%d", i); 11 n /= i; 12 if(n!=1){ 13 printf("*"); 14 } 15 } 16 } 17 printf("\n"); 18 19 return 0; 20}

习题15

利用条件运算符的嵌套来完成此题: 学习成绩 >=90分的同学用A表示, 60-89分之间的用B表示, 60分以下的用C表示。

实现思路: 条件判断嵌套实现。

代码如下:

1#include <stdio.h> 2 3int main(){ 4 int score, i; 5 char level; 6 printf("Please input your scoore:\n"); 7 scanf("%d", &score); 8 level = (score >= 90) ? 'A' : (score >= 60 ? 'B' : 'C'); 9 printf("Your level is %c", level); 10 11 return 0; 12}

打印:

1Please input your scoore: 265 3Your level is B

除了条件运算符,还可以如下:

1#include <stdio.h> 2 3int main(){ 4 int score, i; 5 char level; 6 printf("Please input your scoore:\n"); 7 scanf("%d", &score); 8 if(score >= 60){ 9 if(score >= 90){ 10 level = 'A'; 11 } 12 else{ 13 level = 'B'; 14 } 15 } 16 else{ 17 level = 'C'; 18 } 19 printf("Your level is %c", level); 20 21 22 return 0; 23}

本文原文首发来自博客专栏C语言实战,由本人转发至https://www.helloworld.net/p/0l4speSlMtdg,其他平台均属侵权,可点击https://blog.csdn.net/CUFEECR/article/details/106454371查看原文,也可点击https://blog.csdn.net/CUFEECR浏览更多优质原创内容。

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java.util.ConcurrentModificationException异常原因及解决方法

今天在做一个很简单的Java练习题的时候遇到这个问题。题目:一对兔子在出生第三个月的时候开始,每个月会生一对小兔子。当小兔子在它们第三个月的时候,同理。问:每个月的兔子总数。这是一个很简单的题,前6个月的兔子数量是,1,1,2,3,5,8.斐波拉契数列。按这个规律就可以得出结果。但是我是用创建对象的方法来做。代码:publiccla

java基础编程练习题

1、題目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?123456711235813这就是斐波那契数列(Fibonaccisequence),又称黄金分割数列、因数学家列昂纳多·斐波那契(Le

KVM调整cpu和内存

一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid