1、计算并输出1到100之间所有偶数之和;
#include <stdio.h>
int main(){
int sum = 0;
for(int i = 2; i <= 100; i+ = 2) {
sum+ = i;
}
printf("sum = :%d\n", sum);
return 0;
}
2、招数数组中的最大值和最小值;
#include <stdio.h>
int main() {
int a[] = {2, 1, 4, 6, 5};
int size = sizeof(a) / sizeof(a[0]);
int max = a[0];
int min = a[0];
for (int i = 0; i < size; i++) {
if (a[i] > max) {
max = a[i];
}
if (a[i] < min) {
min = a[i];
}
}
printf("max = :%d\n", max);
printf("min = :%d\n", min);
return 0;
}
3、将给定的字符串反转;
#include <stdio.h>
#include <string.h>
void reverse(char str[]) {
int len = strlen(str);
for (int i = 0; i < len / 2; i++) {
char temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}
}
int main() {
char str[] = "abcd";
reverse(str);
printf("reverse:%s\n", str);
return 0;
}
4、判定一个给定的字符串是否是回文字符串;
#include <stdio.h>
#include <string.h>
int palindrome(char str[]) {
int len = strlen(str);
for (int i = 0; i < len / 2; i++) {
if(str[i] != str[len - i - 1]) {
return 0;
}
}
return 1;
}
int main() {
char str[] = "level";
if(palindrome(str)) {
printf("%s is palindrome\n", str);
} else {
printf("%s is not a palindrome\n", str);
}
return 0;
}
5、计算一个给定数字的阶乘;
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
}
return n * factorial(n - 1);
}
int main() {
int num = 5;
int result = factorial(num);
printf("factorialof %d is %d\n", num, result);
return 0;
}
6、找出一个对给定数组中的所有重复元素;
#include <stdio.h>
void findDuplicates(int a[], int size) {
for (int i = 0; i < size; i++) {
for(int j = i + 1; j < size; j++) {
if (a[i] == a[j]) {
printf("%d is aduplicate\n", a[i]);
}
}
}
}
int main() {
int a[] = {2, 3, 4, 2, 5, 6, 4, 3};
int size = sizeof(a) / sizeof(a[0]);
findDuplicates(a, size);
return 0;
}
7、将一个给定的整数数组按升序排序;
#include <stdio.h>
void bubbleSort(int a[], int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
int main() {
int a[] = {5, 2, 9, 1, 7};
int size = sizeof(a) / sizeof(a[0]);
bubbleSort(a, size);
printf("Sorted array in ascending order: ");
for (int i = 0; i < size; i++) {
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
8、判断一个给定的字符串是否是有效的ip地址;
#include <stdio.h>
#include <string.h>
int ipAddress(char str[]) {
int count = 0;
char *token = strtok(str, ".");
while (token != NULL) {
int num = atoi(token);
if (num < 0 || num > 255) {
retturn 0;
}
count++;
token = strtok(NULL, ".");
}
return (count == 4);
}
int main() {
char str[] = "192.168.0.1";
if(ipAddress(str)) {
printf("%s 是一个有效IP地址\n", str);
} else {
printf("%s 不是一个有效的IP地址\n", str);
}
return 0;
}
9、判断一个给定的字符串是否是有效的括号序列(左右括号数量是否一致);文章来源:https://www.toymoban.com/news/detail-628829.html
#include <stdio.h>
#include <string.h>
int vaild(char str[]) {
int len = strlen(str);
int count = 0;
for (int i = 0; i < len; i++) {
if (str[i] == '(') {
count++;
} else if (str[i] == ')') {
count--;
}
if (count < 0) {
return 0;
}
}
return (count == 0);
}
int main() {
char str[] = "((()))";
if (vaild(str)) {
printf("%s 是一个有效的括号\n", str);
} else {
printf("%s 不是一个有效的括号\n", str);
}
return 0;
}
10、将一个给定的字符串中的所有空格替换为“%20”;文章来源地址https://www.toymoban.com/news/detail-628829.html
#include <stdio.h>
#include <string.h>
void spaces(char str[]) {
int len = strlen(str);
int count = 0;
for (int i = 0; i < len; i++) {
if (str[i] = ' ') {
count++;
}
}
int newlen = len + 2 * count;
str[newlen] = '\0';
for(int i = len - 1; i >= 0; i--) {
if (str[i] == ' ' ) {
str[newlen - 1] = '0';
str[newlen - 2] = '2';
sre[newlen - 3] = '%';
newlen--;
} else {
str[newlen - 1] = str[i];
newlen--;
}
}
}
int main () {
char str[] = "hello world";
spaces(str);
printf("空格替换后的字符串:%s\n", str);
return 0;
}
到了这里,关于C语言基础算法题的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!