Java教程

task3

本文主要是介绍task3,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.

#include<stdio.h>
#include<stdlib.h>
long long fac(int n);
int main()

{
    int i,n;
    printf("Enter n: ");
    scanf("%d",&n);
    for(i=1;i<=n;++i)
        printf("%d!=%11d\n",i,fac(i));
    system("pause");
    return 0;

}
long long fac(int n)
{
static long long p=1;
p=p*n;
return p;
}

 

#include<stdio.h>
#include<stdlib.h>
long long fac(int n);
int main()

{
    int i,n;
    printf("Enter n: ");
    scanf("%d",&n);
    for(i=1;i<=n;++i)
        printf("%d!=%11d\n",i,fac(i));
    system("pause");
    return 0;

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

 

#include<stdio.h>
#include<stdlib.h>
int fun(int,int);
int main()

{
    int k=4,m=1,p1,p2;
    p1=fun(k,m);
    p2=fun(k,m);
    printf("%d,%d\n",p1,p2);
    system("pause");
    return 0;
    
}
int fun(int a,int b)
{
static int m=0,i=2;
i+=m+1;
m=i+a+b;
return (m);

}

 

 

 2.

#include<stdio.h>
#include<stdlib.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");

    }
    system("pause");
    return 0;
}
void printsymbol(int n,char symbol)

{
    int i;
for(i=0;i<n;i++)
    printf("%c",symbol);
return ;

}

 

int n;
    char symbol;
    while(scanf("%d %c",&n,&symbol)!=EOF)
    
    {
        printsymbol(n,symbol);
        printf("\n");

    }
    system("pause");
    return 0;
}
void printsymbol(int n,char symbol)

{

if(n==1)
    printf("%c",symbol);
else
{
    printf("%c",symbol);
printsymbol(n-1,symbol);
}
return;
}

 

 3.

#include<stdio.h>
long long fun(int n);

int main()
{
    int n;
    long long f;
    
    while (scanf("%d", &n) != EOF)
    {
        f = fun(n)-1;
        printf("n=%d,f=%lld\n", n, f);
    }
    return 0;
}
long long fun(int n)
{
    int sum;
    sum = 2;
    if (n == 1)
        sum == 2;
    else
        sum = 2* fun(n - 1);
    return sum;
}

 

4.

 

#include<stdio.h>
int isPrime(int n);
int main()
{
    int n, res, i,num;
    n = 101;
    num = 0;
    for (i = 201; n < i; n++)
    {
        res = isPrime(n);
        if (res == 0) {
            printf(" %d ", n);
            num++;
        }
    
    }
    printf("\n");
    printf("101~200之间有%d个素数", num);
    return 0;
}
int isPrime(int n)
{
    int i,ret;
    ret = 1;
    for (i = 2; i < n; i++)
    {
        if (n % i ==0)
        {
            ret = 0;
            break;
        }
    }
        return ret;
        
    
}

 

 

5.

#include<stdio.h>
long fun(long s);
int main()
{
    long s, t;

    printf("Enter a number:");
    while (scanf("%ld", &s) != EOF)
    {
        t = fun(s);
        printf("new number is:%ld\n\n", t);
        printf("Enter a number:");
    }
    return 0;
}
long fun(long s)
{
    int res, n, i,m;
    res = 0;
    n = 10;
    m = 1;
    for (i = 0; i < 10; i++)
    {
        if ((s % n) % 2 != 0)
        {
            res = (s % n) * m + res;
            m = m * 10;
            s = (s - s % 10) / 10;
        }
        else
            s = (s - s % 10) / 10;

    }
    return res;
}

 

 6.

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

 

 

 

 

 

 

 

.

这篇关于task3的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!