c语言经典题目

  • 格式:docx
  • 大小:13.11 KB
  • 文档页数:7

c语言经典题目

C语言经典题目-数学问题_c语言经典数学问题编程例题-CSDN博客

1、一个整数x的全部因子(包括1,不包括x本身)之和等于y;并且正数y的全部因子(包括1,不包括y本身)之和等于x,则将整数x和y称为亲密数,求3000以内的全部的亲密数

#include

int getDivisorSum(int n) { // 计算n的所有因子之和

int sum = 0;

for (int i = 1; i <= n / 2; i++) {

if (n % i == 0) {

sum += i;

}

}

return sum;

}

int main() {

for (int i = 1; i <= 3000; i++) {

int y = getDivisorSum(i); // 计算i的因子和

if (getDivisorSum(y) == i && i < y && y <=

3000) { // 判断i和y是否为亲密数

printf("(%d, %d)\n", i, y); // 输出亲密数

}

}

return 0;

} 2、c语言:判断1-200之间的素数

#include

#include

bool isPrime(int n) { // 判断n是否为素数的函数

if (n <= 1) {

return false;

}

for (int i = 2; i * i <= n; i++) {

if (n % i == 0) {

return false;

}

}

return true;

}

int main() {

for (int i = 1; i <= 200; i++) {

if (isPrime(i)) { // 判断i是否为素数

printf("%d ", i); // 输出素数

}

}

printf("\n");

return 0;

}

3、求最大公约数和最小公倍数

//辗转相除法

#include

int main()

{

int a = 0; int b = 0;

scanf("%d %d", &a, &b);

int z = a * b;

//求最大公约数:

int c = 0;

while (a % b != 0)

{

c = a % b;

a = b;

b = c;

}

printf("最大公约数为:%d\n", b);

//求最小公倍数:

int min = z / b;

printf("最小公倍数:%d\n", min);

return 0;

}

4、进制转换

进制转换是人们利用符号来计数的方法。进制转换由一组数码符号和两个基本因素“基数”与“位权”构成。基数是指进位计数制中所采用的数码(数制中用来表示“量”的符号)的个数。位权是指进位制中每一固定位置对应的单位值。

• 将p进制数x转换为十进制数y

int y = 0,product = 1;

while(x != 0)

{

y = y + (x % 10)*product; //(x%10)获取个位数 x = x / 10; //去掉个位数

product = product * p; //p代表位权

}

• 将十进制数y转换为Q进制数

int array[111];

int num = 0;//余数的位数

do{

array[num++] = y%q;//除基取余

y /= q;

} while(y != 0) //当商不为0时进行循环

//然后再注意逆序输出数组即可

5、c语言:给你两个字符串:ransomNote 和 magazine ,判断 ransomNote 能不能由 magazine 里面的字符构成。

如果可以,返回 true ;否则返回 false 。

magazine 中的每个字符只能在 ransomNote 中使用一次

#include

#include

bool canConstruct(char * ransomNote, char * magazine){

int count[26] = {0}; // 使用数组记录magazine中每个字符出现的次数

while (*magazine != '\0') { // 遍历magazine字符串,统计每个字符出现的次数

count[*magazine - 'a']++;

magazine++; }

while (*ransomNote != '\0') { // 遍历ransomNote字符串,检查每个字符是否可以使用

if (count[*ransomNote - 'a'] > 0) {

count[*ransomNote - 'a']--;

} else {

return false;

}

ransomNote++;

}

return true;

}

int main() {

char ransomNote[] = "aa", magazine[] = "aab";

bool result = canConstruct(ransomNote, magazine);

if (result) {

printf("ransomNote字符串可以由magazine字符串中的字符构成\n");

} else {

printf("ransomNote字符串无法由magazine字符串中的字符构成\n");

}

return 0;

}

注释:在C语言中,字符是一个整数类型,每个字符都对应一个ASCII码值,该值在0到127之间。其中,小写字母a到z对应的ASCII码值为97到122。因此,当我们使用表达式

*magazine - 'a' 时,实际上是将magazine指针指向的字符减去字符'a'的ASCII码值,得到一个0到25之间的整数,这个整数就可以用来作为一个数组的下标,用来记录magazine中每个字符出现的次数。例如,当magazine中出现了一个字符'b'时,我们可以使用表达式 count['b'-'a']++ 将count数组中下标为1的元素加1,表示字符'b'在magazine中出现了一次。

6、c语言:给你单链表的头结点 head ,请你找出并返回链表的中间结点。

如果有两个中间结点,则返回第二个中间结点

#include

#include

struct ListNode {

int val;

struct ListNode *next;

};

struct ListNode* middleNode(struct ListNode* head) {

struct ListNode *slow = head, *fast = head;

while (fast != NULL && fast->next != NULL) { // 快慢指针遍历链表

slow = slow->next;

fast = fast->next->next;

}

return slow;

}

int main() {

// 构造一个单链表

struct ListNode *head = (struct ListNode*)

malloc(sizeof(struct ListNode));

head->val = 1;

head->next = (struct ListNode*)

malloc(sizeof(struct ListNode)); head->next->val = 2;

head->next->next = (struct ListNode*)

malloc(sizeof(struct ListNode));

head->next->next->val = 3;

head->next->next->next = (struct ListNode*)

malloc(sizeof(struct ListNode));

head->next->next->next->val = 4;

head->next->next->next->next = (struct ListNode*)

malloc(sizeof(struct ListNode));

head->next->next->next->next->val = 5;

head->next->next->next->next->next = NULL;

// 输出链表中间结点的值

struct ListNode *middle = middleNode(head);

printf("链表中间结点的值为:%d\n", middle->val);

return 0;

}