实验三

task1

#include <stdio.h>
long long fac(int n);
int main()
{
int i, n;
printf("Enter n: ");
scanf_s("%d", &n);
for (i = 1; i <= n; ++i)
printf("%d! = %lld\n", i, fac(i));
return 0;
}

long long fac(int n)
{
static long long p = 1;
printf("p = %lld\n", p);
p = p * n;
return p;
}

 

 

#include <stdio.h>
int func(int, int); 
int main()
{
    int k = 4, m = 1, p1, p2;
    p1 = func(k, m); 
    p2 = func(k, m); 
    printf("%d,%d\n", p1, p2);
    return 0;
}
int func(int a, int b)
{
    static int m = 0, i = 2;
    i += m + 1;
    m = i + a + b;
    return (m);
}

 

task2

#include <stdio.h>
void printSymbol(int n, char symbol);
int main()
{
  int n;
  char symbol;
  while( scanf("%d %c", &n, &symbol) != EOF )
 {
    printSymbol(n, symbol); 
    printf("\n");
 }
  return 0;
}

void printSymbol(int n,char symbol)
{
    int i=0;
    for(;i<n;i++){
    printf("%c",symbol);
    }
}

 

task3

#include <stdio.h>
long long fun(int n); // 函数声明
int main()
{
  int n;
  long long f;
  while (scanf("%d", &n) != EOF)
 {
    f = fun(n); 
    printf("n = %d, f = %lld\n", n, f);
}
  return 0;
}

long long fun(int a)
{
     long long result;
     if(a==1)
     result=1;
     else
     result=(fun(a-1)+1)*2-1;
     return result;
}

 

task4

#include <stdio.h>
#include<math.h>
int isprime(int a);
int main()
{
    int a, n;
    int k = 0;
    for (a = 101; a <= 200; a++) 
    {
        n = isprime(a);
        if (n == 1) {
            printf("%d", a);
            printf(" ");
            k++;
        }
    }
    printf("\n101~200之间一共有%d个非素数", k);
    return 0;
}


int isprime(int a)
{
    double m;
    int i;
    m = sqrt(a * 1.0);
    for (i = 2; i <= m; i++)
    {
        if (a % i == 0)
            break;
    }
    if (i > m && a > 1)
        return 0;
    else
        return 1;
}

 

 task5

#include <stdio.h>
long fun(long s);
int main()
{
    long s, t;
    printf("Enter a number: ");
    while (scanf_s("%ld", &s) != EOF)
    {
        t = fun(s);
        printf("new number is: %ld\n\n", t);
        printf("Enter a number: ");
    }
    return 0;
}
long fun(long x) {
    long a, b = 1, c = 0;
    while (x > 0)
    {
        a = x % 10;
        if (a % 2 != 0)
        {
            c += a * b;
            b *= 10;
        }
        x /= 10;
    }
        return c;
}

 

 task6

#include <stdio.h>
double fun(int n);
double jc(int n);
int main() {
int n;
double s;
printf("Enter n(1~10): ");
while (scanf_s("%d", &n) != EOF)
{
s = fun(n);
printf("n = %d, s= %f\n\n", n, s);
printf("Enter n(1~10): ");
}
return 0;
}
double fun(int n)
{
int b, a = 1;
double sum = 0;
for (b = 1; b <= n;b++) {
sum += 1 / jc(b) * a;
a *= -1;

}
return sum;
}
double jc(int n) {
if (n == 1)
return 1;
else
return n * jc(n - 1);
}

 

 

posted @ 2021-11-24 11:05  霍金磊  阅读(10)  评论(2编辑  收藏  举报