数组(第5天)

1、求斐波那契数列的第n项。

1、1、2、3、5、8、13、21、34、......,n=1和n=2的时候都是输出1

公式:f(n) = f(n-1)+f(n-2); 例如第20项,6765

(1)代码

 //作业,菲波拉契数列数列的第n项
  int m,line,sum2=0;
  int arr8[20];
  printf("请输入要求的项数:");
  scanf("%d",&line);
  for(m=0;m<line;m++)
  {
      if(m<2)
      {
          arr8[m] = 1;
      }
      else
      {
          arr8[m] = arr8[m-1]+arr8[m-2];
          sum2 = arr8[m];
      }
   
  }                                       

  printf("%d\n",sum2);

(2)运行结果

 请输入要求的项数:20
6765

2、输入一串字符,计算其中空格的个数。

(1)代码

//输入一串字符,计算其中空格的个数。
 char str[]="";
 int k,count;
 printf("请输入一串字符:");
 gets(str);
 for(k=0;str[k]!='\0';k++)
 {
     if(str[k] == ' ')
     {
         count++;
     }
 }
 printf("字符串中的空格数为:%d\n",count);

(2)运行结果

ubuntu@ubuntu:C$ gcc 1-12-test2.c
1-12-test2.c: In function ‘main’:
1-12-test2.c:33:2: warning: implicit declaration of function ‘gets’; did you mean ‘fgets’? [-Wimplicit
-function-declaration]
  gets(str);
  ^~~~
  fgets
/tmp/ccChhClv.o:在函数‘main’中:
1-12-test2.c:(.text+0x68): 警告: the `gets' function is dangerous and should not be used.
ubuntu@ubuntu:C$ ./a.out
请输入一串字符:dsc dc
字符串中的空格数为:1

3、输入一串字符判断输入字符的大写个数,小写个数,数字个数,符号个数

(1)代码

//输入一串字符串,判断字符串中的大写字符,小写字符,数字字符,符号个数
   char str1[100]="";
   int m;
   int count1=0,count2=0,count3=0,count4=0;
   printf("请输入一串字符串:");
   gets(str1);                                                               
   for(m=0;str1[m]!='\0';m++)
   {   
       if(str1[m]>=65 && str1[m] <=90)
       {   
           count1++;
       }
       else if(str1[m]>=97 && str1[m] <= 122)
       {
           count2++;
       }
       else if(str1[m]>=48 && str1[m] <= 57)
       {
           count3++;
       }
       else
       {
           count4++;
       }
   }
   printf("大写字母个数为:%d\n",count1);
   printf("小写字母个数为:%d\n",count2);
   printf("数字个数为:%d\n",count3);
   printf("符号个数为:%d\n",count4);

(2)运行结果

gcc 1-12-test2.c
1-12-test2.c: In function ‘main’:
1-12-test2.c:51:2: warning: implicit declaration of function ‘gets’; did you mean ‘fgets’? [-Wimplicit-function-declaration]
  gets(str1);
  ^~~~
  fgets
/tmp/ccH6H0Nk.o:在函数‘main’中:
1-12-test2.c:(.text+0x8e): 警告: the `gets' function is dangerous and should not be used.
ubuntu@ubuntu:C$ ./a.out
请输入一串字符串:cccVVV;;;123
大写字母个数为:3
小写字母个数为:3
数字个数为:3
符号个数为:3

4、附加:请写一个函数,完成如下功能:输入是整数,输出是十六进制字符串

(1)代码

 #include <stdio.h>
 
 void decimalToHex(int num) {
     char hex[10];                                                                                          
     int i = 0;
     do {
         int remainder = num % 16;//取一个变量,将整数取余16的值赋值给变量
         if (remainder < 10) {
             hex[i] = remainder + '0';  //通过加上'0'的ascii值将十进制数字转换为十六进制数字
         } else {
             hex[i] = remainder + 55; // 大于10的数通过加55,利用ascii表值将数字转换为大写字母A~F
         }
         i++;
         num = num / 16;
     } while (num != 0);
 
     printf("Hexadecimal representation: ");
     for (int j = i - 1; j >= 0; j--) {
         printf("%c", hex[j]);
     }
     printf("\n");
 }
 
 int main() {
     int number;
     printf("Enter an integer: ");
     scanf("%d", &number);
     decimalToHex(number);
     return 0;
 }
 

(2)运行结果

ubuntu@ubuntu:C$ gcc test.c
ubuntu@ubuntu:C$ ./a.out
Enter an integer: 10
Hexadecimal representation: A
ubuntu@ubuntu:C$ 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值