---
# System prepended metadata

title: 11/4
tags: [程式作業]

---

---
tags: 程式作業
---
# 11/4
[TOC]

## 1
![](https://i.imgur.com/63qY0OC.png)

```

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int x1;
    int x2;
    int y1;
    int y2;
    printf("x1 x2 y1 y2 = ");
    scanf("%d %d %d %d",&x1,&x2,&y1,&y2);
    if(y1<x2 && y1>x1)
    printf("The two lines overlap");
    else if(y2<x2 && y2>x1)
    printf("The two lines overlap");
    else 
    printf("The two lines do not overlap");
}
```
## 2
![](https://i.imgur.com/qfTjOCd.png)

```

#include<stdio.h>
#include<stdlib.h>
int fun(int n)
{
    if(n==1)
    {
        return 0;
    }
    if(n%2==0)
    {
        n=n/2;
        return n+fun(n);
    }
    else
    {
        n=3*n+1;
        return n+fun(n);
    }   
}
int main()
{
    int n;
    printf("Please enter the number: ");
    scanf("%d",&n);
    if(n==1)
        printf("reslut = %d\n",n);
    else
    {
        while(n<0 || n>=231)
        {
            printf("Please enter the number again: ");
            scanf("%d",&n);
        }
        n=n+fun(n);
        printf("reslut = %d\n",n);
    }
}
```

## 3-1
```
#include<stdio.h>
#include<stdlib.h>
int f(int n)
{
    if(n==0)
        return 0;
    else if(n==1)
        return 1;
    else
        return f(n-1)+f(n-2);
}
int main()
{
    int n;
    printf("n = ");
    scanf("%d",&n);
    while(n<=0)
    {
        printf("n = ");
        scanf("%d",&n);
    }
    printf("f(n) = %d\n",f(n));
}
```

## 3-2
```#include<stdio.h>
#include<stdlib.h>
int main()
{
    int n;
    int n0=0;
    int n1=1;
    int n2=1;
    int i=3;
    printf("n = ");
    scanf("%d",&n);
    while(n<=0)
    {
        printf("n = ");
        scanf("%d",&n);
    }
    if(n==0)
        printf("resulat = %d\n",n0);
    else if(n==1)
        printf("reslut = %d\n",n1);
    else if(n==2)
        printf("reslut = %d\n",n2);
    else
    {
        while(n>=i)
        {
            n0=n1;
            n1=n2;
            n2=n1+n0;
            i++;
        }
        printf("reslut = %d\n",n2);
    }
}
```

## 3-3
```#include<stdio.h>
#include<stdlib.h>
int f(int i)
{
   if(i==0)
      return 0;
   else if(i==1)
      return 1;
   else
      return f(i-1)+f(i-2);
}
int main()
{
   int n;
   int arr[999];
   printf("n = ");
   scanf("%d",&n);
   while(n<=0)
   {
      printf("n = ");
      scanf("%d",&n);
   }
   int i;
   for(i=0;i<=n;i++)
   {
      printf("f(%d) = %d\n",i,f(i));  
   } 
}

```

## 4
```
#include<stdio.h>
#include<stdlib.h>
int main()
{
    int n;
    printf("Please enter number: ");
    scanf("%d",&n);
    int p[n];
    int r[n];
    int f[n];
    for(int i=0;i<n;i++)
    {
        printf("Enter population,factor of year,food production: ");
        scanf("%d %d %d",&p[i],&r[i],&f[i]);
    }
    for(int i=0;i<n;i++)
    {
        int b=1;
        while(p[i]*r[i]*b<f[i])
        {
            b++;
        }
        printf("%d\n",b);
    }
}
```