C语言第7天作业 7月18日

1.将strlen strcpy strcmp strcat打包成函数

a. mystrlen, mystrcpy mystrcmp mystrcat.

b.mystrcpy mystrcmp mystrcat不需要返回值,因为指针没讲,无法返回数组

1.1mystrlen

#include <stdio.h>
int myStrlen(char str[]);
int main(int argc, const char *argv[])
{
	char arr[100];
	gets(arr);
	printf("%d\n",myStrlen(arr));
	return 0;
}
int myStrlen(char str[]){
	int i=0;
	for(;str[i]!='\0';i++);
	return i;
}

1.2mystrcpy

#include <stdio.h>
void myStrcpy(char str[],char str1[]);
int main(int argc, const char *argv[])
{
	char arr[100];
	char arr1[100];
	gets(arr);
	gets(arr1);
	myStrcpy(arr,arr1);
	return 0;
}void myStrcpy(char str1[],char str2[]){
	for(int i=0;;i++){
		str1[i]=str2[i];
		if(str2[i]=='\0') 
			break;
	}
	puts(str1);
}

1.3mystrcmp

#include <stdio.h>
int myStrcmp(char str[],char str1[]);
int main(int argc, const char *argv[])
{
	char arr[100];
	char arr1[100];
	gets(arr);
	gets(arr1);
	printf("%d\n",myStrcmp(arr,arr1));
	return 0;
}
int myStrcmp(char str1[],char str2[]){
	int i=0,res;
	for(;;i++){
		res=str1[i]-str2[i];
		if(res!=0)
			break;
		else if(str2[i]=='\0') 
			break;
	}
	return res;
}

1.4mystrcat

#include <stdio.h>
void myStrcat(char str[],char str1[]);
int main(int argc, const char *argv[])
{
	char arr[100];
	char arr1[100];
	gets(arr);
	gets(arr1);
	myStrcat(arr,arr1);
	return 0;
}void myStrcat(char str1[],char str2[]){
	int i;
	for(i=0;str1[i]!=0;i++);
	for(int j=0;str2[j]!='\0';i++,j++){
		str1[i]=str2[j];
	}
	str1[i]='\0';
	puts(str1);
}

2.求斐波那契数列的第n项。

1、1、2、3、5、8、13、21、34、...,第一项和第二项时候都是输出1

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

#include <stdio.h>
int FS(int n);
int main(int argc, const char *argv[])
{
	printf("%d\n",FS(20));
	return 0;
}

int FS(int n){
	if(n==1 || n==2) return 1;
	else return FS(n-1)+FS(n-2);

}

 

3.输出一个十行的杨辉三角

提示:当前数据=上一行的当前列+上一行的前一列

#include <stdio.h>
int main(int argc, const char *argv[])
{
	int arr[10][10]={0};
	for(int i=0;i<10;i++){
		for(int j=0;j<10;j++){
			if(j==0) arr[i][j]=1;
			else if(i>0&&j!=0)
				arr[i][j]=arr[i-1][j-1]+arr[i-1][j];
			if(arr[i][j]>0) printf("%d\t",arr[i][j]);
		}
		printf("\n"); 
	}
	return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C语言代码: ```c #include <stdio.h> int main() { int n; scanf("%d", &n); if (n > 31) { n -= 31; } int days = (n - 1) * 7 + 3; int year = 2022 + days / 366; int remain_days = days % 366; int month, day; if (remain_days < 31) { month = 1; day = remain_days + 1; } else if (remain_days < 59) { month = 2; day = remain_days - 30; } else if (remain_days < 90) { month = 3; day = remain_days - 58; } else if (remain_days < 120) { month = 4; day = remain_days - 89; } else if (remain_days < 151) { month = 5; day = remain_days - 119; } else if (remain_days < 181) { month = 6; day = remain_days - 150; } else if (remain_days < 212) { month = 7; day = remain_days - 180; } else if (remain_days < 243) { month = 8; day = remain_days - 211; } else if (remain_days < 273) { month = 9; day = remain_days - 242; } else if (remain_days < 304) { month = 10; day = remain_days - 272; } else if (remain_days < 334) { month = 11; day = remain_days - 303; } else { month = 12; day = remain_days - 333; } printf("%d年%d%d是", year, month, day); int day_of_week = (days + 5) % 7; switch (day_of_week) { case 0: printf("星期"); break; case 1: printf("星期一"); break; case 2: printf("星期二"); break; case 3: printf("星期三"); break; case 4: printf("星期四"); break; case 5: printf("星期五"); break; case 6: printf("星期六"); break; } return 0; } ``` 当输入n时,程序会先判断n是否大于31,如果大于31,则将n减去31。 然后,程序会根据n算出对应的期。首先,算出2022年318是当年的第77。每增加一个学号,就增加7。假设当前年份是2022年,那么每年的数是366(因为是闰年)。所以,当超过366时,需要将年份加1,并把当前已经过的数减去366。 通过算出对应的期,接下来再计算对应是星期几。由于已知2022年318是星期五,所以可以按照一周七推算出对应星期几。最后,输出对应的期和星期几即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值