1.输出一个十行的杨辉三角。
提示:当前数据 = 上一行的当前列 + 上一行的前一列
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
2.编写atoi函数
3.
求斐波那契数列的第n项。
1、1、2、3、5、8、13、21、34、......,第一项和第二项 时候都是输出1
公式:f(n) = f(n-1)+f(n-2); 例如第20项,6765
4.将自己编写好的strlen strcpy strcmp strcat 打包成函数。
mystrlen , mystrcpy mystrcmp mystrcat.
mystrcpy mystrcmp mystrcat不需要返回值,因为指针没讲,无法返回数组
1.代码:
#include<stdio.h>
#include<string.h>
const int N = 110;
int main(){
int max = -1;
int a[N][N]={0};
a[0][0] = 1;
for(int i = 0;i < 11; i++)
{
for(int j = 1;j < 12; j++){
if(i >= j)
a[i][j] = a[i-1][j] + a[i-1][j-1];
}
}
for(int i = 0;i < 11; i++){
for(int j = 1;j < 12; j++)
if(i >= j)
printf("%-4d",a[i][j]);
printf("\n");
}
return 0;
}
2.代码
#include<stdio.h>
#include<math.h>
const int N = 110;
void atoi(char a[N]){
char b[N] = {0};
int i,con = 0;
//去空格
for(int i = 0;i < N; i++){
if(a[i+con] == ' ')
{
con++;
while(a[i + con] == ' '){
con++;
}
a[i] = a[i+con];
}
else{
a[i] = a[i+con];
}
}
b[i] = a[i];
//有符号
if(a[i] == '+' || a[i] == '-'){
int num = 0;
for(i = 1;a[i] >= '0' && a[i] <= '9'; ++i){
b[i] = a[i];
}
int j = 1,flag=0;
while(b[j] != '\0'){
num *= 10;//拓展位数
num += b[j] - '0';
flag++;
if(flag == 9){
printf("太大了,你把握不住,给你个小的:\n");
break;
}
j ++;
}
num = - num;
printf("%d",num);
}
//无符号
else if(a[i] >= '0' && a[i] <= '9'){
int num = 0;
for(i = 1;a[i] >= '0' && a[i] <= '9'; i++ ){
b[i] = a[i];
}
int j = 0;
while(b[j] != '\0'){
num *= 10;//拓展位数
num += b[j] - '0';
j ++;
}
printf("%d",num);
}
else
printf("返回0");
}
int main(){
char a[N];
gets(a);
atoi(a);
return 0;
}
3.代码:
#include<stdio.h>
#include<string.h>
const int N = 110;
int feibo(int n){
if(n == 1)
return 1;
else if(n == 2)
return 1;
else
n = feibo(n-1) + feibo(n-2);
}
int main(){
int n ;
scanf("%d",&n);
int sum = feibo(n);
printf("%d",sum);
return 0;
}
4.代码
#include<stdio.h>
#include<string.h>
const int N = 110;
int mystrlen(char a[]){
int con;
for(int i = 0;a[i] != '\0'; i++)
con++;
return con;
}
void mystrcpy(char str1[],char str2[]){
int con = 0;
for(int i = 0; str2[i] != 0; i++)
con++;
for(int i = 0; i <= con; i++)
str1[i] = str2[i];
for(int i = 0; i <= N; i++)
printf("%c",str1[i]);
}
void mystrcmp(char str1[],char str2[]){
int s1 = mystrlen(str1);
int s2 = mystrlen(str2);
if(s1 < s2)
s1 = s2;
for(int i = 0; i < s1; i++)
if(str1[i] == str2[i]);
else{
printf("%d",str1[i]-str2[i]);
break;
}
}
void mystrcat(char str1[],char str2[]){
int s1 = strlen(str1);
int s2 = strlen(str2);
int res = strcmp(str1,str2);
int sum = s1 + s2;
if(s1 == s2){
if(res< 0){
for(int i = s1; i < sum; i++)
str1[i] = str2[i-s1];
printf("%s",str1);
}
else{
for(int i = s2; i < s1+s2; i++)
str2[i] = str1[i-s2];
printf("%s\n",str2);
}
}
else{
if(res> 0){
for(int i = s1; i < s1+s2; i++)
str1[i] = str2[i-s1];
printf("%s\n",str1);
}
else{
for(int i = s2; i < s1+s2; i++)
str2[i] = str1[i-s2];
printf("%s\n",str2);
}
}
}
int main(){
char arr[N] = {0};
// gets(arr);
// int c;
// c = mystrlen(arr);
// printf("有效长度为:%d",c);
char str1[N] = {"aba"};
char str2[N] = {"afaer"};
// mystrcpy(str1,str2);
// mystrcmp(str1,str2);
mystrcat(str1,str2);
return 0;
}