首页 > 其他分享 >Task3

Task3

时间:2022-11-10 22:34:56浏览次数:44  
标签:Task3 int long char result printf include

任务1

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#define N 80
void print_text(int line, int col, char text[]); // 函数声明
void print_spaces(int n); // 函数声明
void print_blank_lines(int n); // 函数声明
int main() {
int line, col, i;
char text[N] = "hi, November~";
srand(time(0)); // 以当前系统时间作为随机种子
for (i = 1; i <= 10; ++i) {
line = rand() % 25;
col = rand() % 80;
print_text(line, col, text);
Sleep(1000); // 暂停1000ms
}
return 0;
}
// 打印n个空格
void print_spaces(int n) {


int i;
for (i = 1; i <= n; ++i)
printf(" ");
}
// 打印n行空白行
void print_blank_lines(int n) {
int i;
for (i = 1; i <= n; ++i)
printf("\n");
}
// 在第line行第col列打印一段文本
void print_text(int line, int col, char text[]) {
print_blank_lines(line - 1); // 打印(line-1)行空行
print_spaces(col - 1); // 打印(col-1)列空格
printf("%s", text); // 在第line行、col列输出text中字符串
}

任务2

#define _CRT_SECURE_NO_WARNINGS


#include <stdio.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! = %lld\n", i, fac(i));
return 0;
}

// 函数定义
long long fac(int n) {
static long long p = 1; 

printf("p=%11d\n", p);
p = p * n;
return p;
}

 

任务3

#include <stdio.h>
long long func(int n); 
int main() {
    int n;
    long long f;
    while (scanf_s("%d", &n) != EOF) {
        f = func(n);
        printf("n = %d, f = %lld\n", n, f);
    }
    return 0;
}
long long func(int n) {
    double result;
    if (n == 1)
        result = 1;
    else
        result = 2 * func(n - 1) + 1;
    return result;
}

任务4

#include <stdio.h>
int func(int n, int m);
int main() {
    int n, m;
    while (scanf_s("%d%d", &n, &m) != EOF)
        printf("n = %d, m = %d, ans = %d\n", n, m, func(n, m));
    return 0;
}
int func(int n, int m) {
    int result;
    if (n == m || m == 0)
        result = 1;
    else if (n < m)
        result = 0;
    else
        result = func(n - 1, m) + func(n - 1, m - 1);
    return result;
}

任务5

#include <stdio.h>
#include<stdlib.h>
int mul(int n, int m);
int main() {
    int n, m;
    while (scanf_s("%d%d", &n, &m) != EOF)
        printf("%d * %d = %d\n", n, m, mul(n, m));
    return 0;
}
int mul(int n, int m) {
    int result;
    if (n == 0 || m == 0) {
        result = 0;
    }
    else if (n > m) {
        result = mul(n, m - 1) + n;
    }
    else {
        result = mul(n - 1, m) + m;
    }
    return result;
}

任务6

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
void moveplate(unsigned int n, char from, char to);
void hanoi(unsigned int n, char from, char temp, char to);
int count = 0;
int main()
{
unsigned int n;
printf("请输入你需要进行移动的盘子数:\n");
while (scanf_s("%d", &n) != EOF)
{
count = 0;
printf("请输入你需要进行移动的盘子数:\n");
hanoi(n, 'A', 'B', 'C');
printf("移动盘子的总次数为:%d\n", count);
}

 

return 0;

 

}
void moveplate(unsigned int n, char from, char to)
{
printf("%u:%c-->%c\n", n, from, to);
count++;
}
void hanoi(unsigned int n, char from, char temp, char to)
{
if (n == 1)
moveplate(n, from, to);
else
{
hanoi(n - 1, from, to, temp);
moveplate(n, from, to);
hanoi(n - 1, temp, from, to);
}
}

 

任务7

#include< stdio.h>
#include<stdlib.h>
#include<math.h>
int is_prime(int n);
int main() {
    int m;
    while (scanf_s("%d", &m) != EOF)
        is_prime(m);
    return 0;
}
int is_prime(int n) {
    int i, p_flag, q_flag,p,q;
    
    if ((n % 2 == 1) || n < 4) {
        printf("数据输入错误\n");
        exit (0);
    }
    p = 1;
    do
    {
        p += 1;
        q = n - p;
        p_flag = 1;
        for (i = 2; i <= sqrt(1.0 * p); i++) 
        {
            if (p % i == 0) 
            {
                p_flag = 0;
                break;
            }
        }
        q_flag = 1;
        for (i = 2; i <= sqrt(1.0 * q); i++)
        {
            if (q % i == 0) 
            {
                q_flag = 0;
                break;
            }
        
        }

    } while (p_flag * q_flag == 0);
    printf("%d = %d + %d", n, p, q);
    
}

 

标签:Task3,int,long,char,result,printf,include
From: https://www.cnblogs.com/wzy4818/p/16879005.html

相关文章

  • task3
    1.#include<stdio.h>#include<time.h>#include<stdlib.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidprint_spaces(intn);......
  • task3
    #include<stdio.h>#include<math.h>intmain(){unsignedintsum;intn,a1,q;scanf_s("%d",&n);a1=1;q=2;sum=a1*(1-pow(q......
  • task3.c
    #include<stdio.h>intmain(){intn;printf("n=");scanf_s("%lld",&n);intsum=0,i=0,t=1;while(i<=n){sum+=t;......