Hello World


Welcome to my blog ! have a good time ! ^ v ^

1

//
// Created by 唐然 on 2022/11/24.
//
#include <stdio.h>
long fact(int n);

long fact(int n) {
  long product = 1;
  for (int i = 1 ; i <= n ; ++i) product *= i;
  return product;
}
int main() {
  int n;
  long factor;
  printf("请输入要求其乘阶的自然数:");
  scanf("%d", &n);
  while (n >= 0) {
	printf("%d的阶乘是%ld\n", n, fact(n));
	printf("请输入要求其乘阶的自然数:");
	scanf("%d", &n);
  }
  return 0;
}

2

//
// Created by 唐然 on 2022/11/24.
//
#include <stdio.h>
#define PI 3.1415926

typedef struct circle {
  void getcircumference() {
	this -> c = this -> r * 2 * PI;
  }

  void getarea() {
	this -> area = this -> r * this -> r * PI;
  }

  void show() const {
	printf("circumference = %.2f, area = %.2f\n", this -> c, this -> area);
  }
  float r;
private:
  float c;
  float area;
}circle;


int main() {
  circle c;
  printf("输入半径:>");
  scanf("%f", &c.r);
  c.getcircumference();
  c.getarea();
  c.show();
  return 0;
}

3

//
// Created by 唐然 on 2022/11/24.
//
#include <stdio.h>

void f(const int score) {
  char ch;
  switch (score / 10) {
	case 9:
	  ch = 'A';
	  break;
	case 8:
	  ch = 'B';
	  break;
	case 7:
	  ch = 'C';
	  break;
	case 6:
	  ch = 'D';
	  break;
	default:
	  ch = 'E';
  }
  printf("等级:\t\t%c\n", ch);
}
int main() {
  int score;
  printf("请输入分数:\t");
  scanf("%d", &score);
  while (score >= 0 && score <= 100) {
	f(score);
	printf("请输入分数:\t");
	scanf("%d", &score);
  }
  return 0;
}

4

//
// Created by 唐然 on 2022/11/23.
//
#include <stdio.h>

int max(const int a, const int b) {
  return a > b ? a : b;
}

int min(const int a, const int b) {
  return a > b ? b : a;
}

int maxofthree(const int a, const int b, const int c) {
  return max(max(a, b), c);
}

int minofthree(const int a, const int b, const int c) {
  return min(min(a, b), c);
}
int main() {
  int a, b, c;
  printf("输入三个数:>");
  scanf("%d %d %d", &a, &b, &c);
  printf("max = %d, min = %d\n", maxofthree(a, b, c), minofthree(a, b, c));
  return 0;
}

5

//
// Created by 唐然 on 2022/11/24.
//
#include <stdio.h>

int gys(int a, int b) {
  int c = b;
  int tmp = a % c;
  while (tmp) {
	a = c;
	c = tmp;
	tmp = a % c;
  }
  return c;
}

int gbs(int a, int b) {
  return (a * b) / gys(a, b);
}

int main() {
  int x, y, z;
  printf("请输入两个正整数:>");
  scanf("%d %d", &x, &y);
  z = gys(x, y);
  printf("%d与%d的最大公约数是%d\n", x, y, z);
  printf("%d与%d的最小公倍数是%d\n", x, y, gbs(x, y));
  return 0;
}

6

//
// Created by 唐然 on 2022/11/23.
//
#include <stdio.h>
#include <stdbool.h>

bool f(int n) {
  if (n == 1) return false;
  int i;
  for (i = 2 ; i <= n / 2; ++i) {
	if (n % i == 0) return false;
  }
  return true;
}

int main() {
  int sum = 0, m, n, cnt = 0;
  printf("input m&n:>");
  while (scanf("%d %d", &m, &n) != 2 || m < 1 || n > 500) {
	printf("try again!\n");
	printf("input m&n:>");
  }
  for (int i = m ; i <= n ; i++) {
	if (f(i)) {
	  cnt++;
	  sum += i;
	}
  }
  printf("%d到%d的素数有%d个, 和为:%d", m, n, cnt, sum);
  return 0;
}

7

//
// Created by 唐然 on 2022/11/23.
//
#include <stdio.h>
#include <math.h>
#include <stdbool.h>

bool f(int n) {
  int tmp = n, sum = 0, w = 1;
  while (tmp > 9) {
	++w;
	tmp /= 10;
  }
  tmp = n;
  while (tmp) {
	sum += pow(tmp % 10, w);
	tmp /= 10;
  }
  return sum == n;
}
int main() {
  printf("3位、4位水仙花数包括:\n");
  for (int i = 100 ; i <= 10000; ++i) {
	if (f(i)) {
	  printf("%d\n", i);
	}
  }
  return 0;
}

8

//
// Created by 唐然 on 2022/11/23.
//
#include <stdio.h>

void f(int n) {
  if (n < 10) printf("%d", n);
  else {
	printf("%d", n % 10);
	f(n / 10);
  }
}
int main() {
  int x;
  printf("input:> ");
  scanf("%d", &x);
  f(x);
  return 0;
}

9

//
// Created by 唐然 on 2022/11/24.
//
#include <stdio.h>

int fib(int n) {
  if (n < 2) return n;
  else return fib(n - 1) + fib(n - 2);
}

int main() {
  int m, n;
  printf("input m&n:>");
  while (scanf("%d %d", &m, &n) != 2 || m < 1 || n < 1 || n <= m) {
	printf("try again!\n");
	printf("input m&n:>");
  }
  for (int i = m ; i <= n ; ++i) {
	printf("%d ", fib(i));
  }
  return 0;
}

10

//
// Created by 唐然 on 2022/11/24.
//
#include <stdio.h>

void printbinary(short x) {
  short b[16] = {0};
  int idx = 0;
  while (x) {
	b[idx++] = x % 2;
	x /= 2;
  }
  for (int i = 15 ; i >= 0 ; --i) {
	printf("%d", b[i]);
  }
  printf("\n");
}

short bitpoerate(short a, short b, char c) {
  short z = 0;
  switch (c) {
	case '&':
	  z = a & b;
	  break;
	case '|':
	  z = a | b;
	  break;
	case '^':
	  z = a ^ b;
	  break;
	case '<':
	  z = a << b;
	  break;
	case '>':
	  z = a >> b;
	  break;
	default:
	  ;
  }
  return z;
}

void printformulas(short a, short b, char c) {
  short z;
  printf("\t");
  printbinary(a);
  if (c == '<' || c == '>') printf("%c%c\t", c, c);
  else printf("%c\t", c);
  printbinary(b);
  printf("=\t");
  z = bitpoerate(a, b, c);
  printbinary(z);
  printf("\n");
}

int main() {
  short x = 6289, y = 169;
  printformulas(x, y, '&');
  printformulas(x, y, '|');
  printformulas(x, y, '^');
  x = 100, y = 3;
  printformulas(x, y, '<');
  printformulas(x, y, '>');
  return 0;
}

文章作者: tang ran
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 tang ran !
  目录