1、计算并输出1到100之间所有偶数之和;
#include
int main(){
int sum = 0;
for(int i = 2; i <= 100; i+ = 2) {
sum+ = i;
}
printf("sum = :%d\n", sum);
return 0;
}
2、招数数组中的最大值和最小值;
#include
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
#include
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
#include
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
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
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
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
#include
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、判断一个给定的字符串是否是有效的括号序列(左右括号数量是否一致);
#include
#include
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) {
rerturn 0;
}
}
return (count == 0);
}
int main() {
char str[] = "((()))";
if (vaild(str)) {
printf("%s 是一个有效的括号\n", str);
} else {
printf("%s 不是一个有效的括号\n", str);
}
return 0;
}
10、将一个给定的字符串中的所有空格替换为“%20”;
#include
#include
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;
}