当前位置: 首页 > news >正文

济南网站建设开发服务营销7p理论

济南网站建设开发,服务营销7p理论,园林景观网站源码,除了亚马逊还有啥网站做海淘1编写函数将一个仅包含整数&#xff08;可能为负&#xff09;的字符串转换为对应的整数 方法一使用标准库函数 atoi atoi 函数是C语言标准库中的一个函数&#xff0c;用于将字符串转换为整数。 代码&#xff1a; #include <stdio.h> #include <stdlib.h> // 包含…

1编写函数将一个仅包含整数(可能为负)的字符串转换为对应的整数

方法一使用标准库函数 atoi

atoi 函数是C语言标准库中的一个函数,用于将字符串转换为整数。

代码:


#include <stdio.h>
#include <stdlib.h> // 包含 atoi 函数的头文件int main() {const char* str = "12345"; // 示例字符串int num = atoi(str); // 使用 atoi 将字符串转换为整数printf("转换后的整数: %d\n", num);return 0;
}
方法2:手动实现字符串到整数的转换
#include <stdio.h>
#include <ctype.h> // 包含 isdigit 函数的头文件int strToInt(const char *str) {int result = 0;int sign = 1; // 默认符号为正int i = 0;// 处理负号if (str[i] == '-') {sign = -1;i++;}// 遍历字符串中的每个字符while (str[i] != '\0') {// 确保字符是数字if (isdigit(str[i])) {// 计算当前位的值result = result * 10 + (str[i] - '0');} else {// 如果遇到非数字字符,返回错误值(例如0)return 0;}i++;}// 返回带符号的结果return sign * result;
}int main() {const char *str = "-12345"; // 示例字符串int num = strToInt(str); // 调用手动实现的函数printf("转换后的整数: %d\n", num);return 0;
}

2编写一个能比较字符串大小的函数,将两个字符串中第一个不相同字符的ASCII码值之差作为返回值

代码:

#include <stdio.h>// 比较两个字符串大小,返回第一个不相同字符的ASCII码值之差
int compareStrings(const char *str1, const char *str2) {int i = 0;// 遍历两个字符串,直到其中一个字符串结束或找到不相同的字符while (str1[i] != '\0' && str2[i] != '\0') {if (str1[i] != str2[i]) {// 找到不相同的字符,返回它们的ASCII码值之差return str1[i] - str2[i];}i++;}// 如果两个字符串长度不同,返回长度差return str1[i] - str2[i];
}int main() {const char *str1 = "apple";const char *str2 = "apples";int result = compareStrings(str1, str2);if (result == 0) {printf("两个字符串相等。\n");} else if (result > 0) {printf("第一个字符串大于第二个字符串,差值为: %d\n", result);} else {printf("第一个字符串小于第二个字符串,差值为: %d\n", result);}return 0;
}

3编写程序,从键盘输入10个整数,用函数实现将其中最大数与最小数的位置对 换,输出调整后的数组

代码:

#include <stdio.h>// 函数声明
void swapMaxMin(int arr[], int size);int main() {int arr[10];// 从键盘输入10个整数printf("请输入10个整数:\n");for (int i = 0; i < 10; i++) {scanf("%d", &arr[i]);}// 调用函数交换最大数与最小数的位置swapMaxMin(arr, 10);// 输出调整后的数组printf("调整后的数组:\n");for (int i = 0; i < 10; i++) {printf("%d ", arr[i]);}printf("\n");return 0;
}// 函数定义:交换数组中最大数与最小数的位置
void swapMaxMin(int arr[], int size) {int maxIndex = 0, minIndex = 0;// 找到最大数和最小数的索引for (int i = 1; i < size; i++) {if (arr[i] > arr[maxIndex]) {maxIndex = i;}if (arr[i] < arr[minIndex]) {minIndex = i;}}// 交换最大数与最小数的位置int temp = arr[maxIndex];arr[maxIndex] = arr[minIndex];arr[minIndex] = temp;
}

4编写函数,对给定的二维数组(3×3)进行转置(即行列互换)

代码:

#include <stdio.h>// 函数声明
void transpose(int arr[3][3]);
void printArray(int arr[3][3]);int main() {int arr[3][3] = {{1, 2, 3},{4, 5, 6},{7, 8, 9}};// 打印原始数组printf("原始数组:\n");printArray(arr);// 进行转置transpose(arr);// 打印转置后的数组printf("转置后的数组:\n");printArray(arr);return 0;
}// 函数定义:对二维数组进行转置
void transpose(int arr[3][3]) {int temp;for (int i = 0; i < 3; i++) {for (int j = i + 1; j < 3; j++) {// 交换 arr[i][j] 和 arr[j][i]temp = arr[i][j];arr[i][j] = arr[j][i];arr[j][i] = temp;}}
}// 函数定义:打印二维数组
void printArray(int arr[3][3]) {for (int i = 0; i < 3; i++) {for (int j = 0; j < 3; j++) {printf("%d ", arr[i][j]);}printf("\n");}
}

注意事项:

在二维数组转置的过程中,for (int j = i + 1; j < 3; j++) 中的 j 从 i + 1 开始是为了避免重复交换和对角线元素的交换。让我们详细解释一下:

解释

  1. 避免重复交换

    • 在转置过程中,交换 arr[i][j] 和 arr[j][i] 时,只需要交换一次即可。
    • 如果 j 从 0 开始遍历,会导致重复交换,因为 arr[i][j] 和 arr[j][i] 已经在之前的迭代中被交换过了。
    • 例如,如果 i = 0 且 j = 0,那么交换 arr[0][0] 和 arr[0][0] 是无意义的,因为它们是同一个元素。
  2. 对角线元素无需交换

    • 对角线元素(如 arr[0][0]arr[1][1]arr[2][2])在转置前后保持不变。
    • 如果 j 从 0 开始遍历,会导致对角线元素被交换,这实际上是没有必要的。

5编写函数,用冒泡法对输入的字符(不超过10个)按从小到大顺序排序

代码:

#include <stdio.h>
#include <string.h>// 函数声明
void bubbleSort(char arr[], int n);
void swap(char *a, char *b);int main() {char arr[11]; // 最多10个字符,加上字符串结束符 '\0'// 输入字符printf("请输入不超过10个字符:\n");scanf("%s", arr);// 获取字符串长度int n = strlen(arr);// 进行排序bubbleSort(arr, n);// 打印排序后的字符串printf("排序后的字符串:\n");printf("%s\n", arr);return 0;
}// 函数定义:冒泡排序
void bubbleSort(char arr[], int n) {for (int i = 0; i < n - 1; i++) {for (int j = 0; j < n - i - 1; j++) {if (arr[j] > arr[j + 1]) {// 如果前一个字符大于后一个字符,交换它们swap(&arr[j], &arr[j + 1]);}}}
}// 函数定义:交换两个字符
void swap(char *a, char *b) {char temp = *a;*a = *b;*b = temp;
}

6编写程序,输出3~10000为的可逆质数。可逆质数是指:一个质数将其各位数 字的顺序倒过来构成的反序数也是质数。如157和751均为质数,它们是可逆质数。要 求调用两个函数实现

代码:

include <stdio.h>
#include <stdbool.h>
#include <math.h>// 函数声明
bool isPrime(int num);
int reverseNumber(int num);int main() {printf("3到10000之间的可逆质数有:\n");for (int num = 3; num <= 10000; num++) {if (isPrime(num)) {int reversedNum = reverseNumber(num);if (isPrime(reversedNum)) {printf("%d\n", num);}}}return 0;
}// 函数定义:检查一个数是否为质数
bool isPrime(int num) {if (num <= 1) return false;for (int i = 2; i <= num / 2; i++) {if (num % i == 0) return false;}return true;
}// 函数定义:生成一个数的反序数
int reverseNumber(int num) {int reversedNum = 0;while (num > 0) {reversedNum = reversedNum * 10 + num % 10;num /= 10;}return reversedNum;
}

7编写函数,将一个十进制数转换成八进制数

代码:

#include <stdio.h>// 函数声明:将十进制数转换为八进制数
void decimalToOctal(int num);int main() {int num;printf("请输入一个十进制数:\n");scanf("%d", &num);printf("转换后的八进制数为:");decimalToOctal(num);  // 调用转换函数return 0;
}// 函数定义:将十进制数转换为八进制数
void decimalToOctal(int num) {// 如果输入的数为0,直接输出0if (num == 0) {printf("0");return;}int arr[100];  // 用于存储八进制数的数组int i = 0;     // 数组索引// 将十进制数转换为八进制数while (num > 0) {arr[i] = num % 8;  // 取余数,存储到数组中num = num / 8;     // 整除8,去掉最后一位i++;               // 索引加1}// 输出八进制数(从高位到低位)for (int j = i - 1; j >= 0; j--) {printf("%d", arr[j]);}printf("\n");  // 输出换行符
}

8从键盘输入一个正整数,逆序输出。要求使用循环和递归两种方法分别实现

代码:

#include<stdio.h>
int main() {int num;printf("请输入一个正整数:\n");scanf("%d", &num);printf("逆序输出(循环方法):");while (num>0){printf("%d", num % 10);num /= 10;}return 0;
}

代码:


#include <stdio.h>// 函数声明:使用递归逆序输出正整数
void reverse(int num);int main() {int num;printf("请输入一个正整数:\n");scanf("%d", &num);// 使用递归逆序输出printf("逆序输出(递归方法):");reverse(num);printf("\n");return 0;
}// 函数定义:使用递归逆序输出正整数
void reverse(int num) {if (num == 0) {return;  // 递归终止条件}printf("%d", num % 10);  // 输出最后一位数字reverse(num / 10);  // 递归调用,去掉最后一位数字
}


文章转载自:
http://periselenium.c7623.cn
http://optical.c7623.cn
http://tetraphyllous.c7623.cn
http://droplight.c7623.cn
http://gangway.c7623.cn
http://flagellator.c7623.cn
http://incorrectness.c7623.cn
http://dedicatee.c7623.cn
http://goldwater.c7623.cn
http://constative.c7623.cn
http://prickle.c7623.cn
http://muscovite.c7623.cn
http://hithermost.c7623.cn
http://podzolise.c7623.cn
http://disremember.c7623.cn
http://talkathon.c7623.cn
http://sparkplug.c7623.cn
http://gazehound.c7623.cn
http://horrible.c7623.cn
http://wholly.c7623.cn
http://polecat.c7623.cn
http://composmentis.c7623.cn
http://clamant.c7623.cn
http://trinitytide.c7623.cn
http://cymometer.c7623.cn
http://slavery.c7623.cn
http://paravane.c7623.cn
http://dong.c7623.cn
http://transvesical.c7623.cn
http://interpose.c7623.cn
http://mullen.c7623.cn
http://rosaniline.c7623.cn
http://anatomic.c7623.cn
http://amaretto.c7623.cn
http://fondu.c7623.cn
http://changkiang.c7623.cn
http://forefeel.c7623.cn
http://flagellated.c7623.cn
http://schoolmate.c7623.cn
http://curricula.c7623.cn
http://etic.c7623.cn
http://moorish.c7623.cn
http://khmer.c7623.cn
http://mortgagee.c7623.cn
http://plainclothesman.c7623.cn
http://glaciology.c7623.cn
http://collocable.c7623.cn
http://yump.c7623.cn
http://alpenglow.c7623.cn
http://sabine.c7623.cn
http://lusaka.c7623.cn
http://od.c7623.cn
http://thunk.c7623.cn
http://cider.c7623.cn
http://foreside.c7623.cn
http://triracial.c7623.cn
http://anacoluthon.c7623.cn
http://gnash.c7623.cn
http://transponder.c7623.cn
http://mopish.c7623.cn
http://matthias.c7623.cn
http://sprite.c7623.cn
http://mvp.c7623.cn
http://moesogothic.c7623.cn
http://ophidian.c7623.cn
http://telegraphese.c7623.cn
http://actiniform.c7623.cn
http://fiord.c7623.cn
http://broccoli.c7623.cn
http://fascis.c7623.cn
http://cyanohydrin.c7623.cn
http://slimming.c7623.cn
http://rgg.c7623.cn
http://caryatid.c7623.cn
http://unguarded.c7623.cn
http://episiotomy.c7623.cn
http://mesnalty.c7623.cn
http://legioned.c7623.cn
http://moan.c7623.cn
http://polysaprobe.c7623.cn
http://leucorrhea.c7623.cn
http://histaminase.c7623.cn
http://jennet.c7623.cn
http://buzzsaw.c7623.cn
http://effectivity.c7623.cn
http://refundable.c7623.cn
http://denationalise.c7623.cn
http://ingloriously.c7623.cn
http://artery.c7623.cn
http://tyburn.c7623.cn
http://standoffishness.c7623.cn
http://biomorphic.c7623.cn
http://sporocyte.c7623.cn
http://bowdlerize.c7623.cn
http://petalon.c7623.cn
http://fleshly.c7623.cn
http://osmic.c7623.cn
http://torpedo.c7623.cn
http://casebearer.c7623.cn
http://stripling.c7623.cn
http://www.zhongyajixie.com/news/82195.html

相关文章:

  • 网站建设与管理基础专业做网站
  • 开发网站心得网站优化 秦皇岛
  • 百度网站优化排名定制网站建设推广服务
  • 怎么做html5网站广东seo推广贵不贵
  • 网站制作用到什么技术城关网站seo
  • 免费关键词排名优化厦门seo专业培训学校
  • 怎么做网站像淘宝这样的如何把自己的网站推广出去
  • 怎样免费设计网站建设网站模板
  • 对对联的网站推广策略都有哪些
  • 做旧工艺品网站如何在百度上发表文章
  • wap网站现在还有什么用今天重大国际新闻
  • 推荐西安优秀的响应式网站建设公司高端网站建设案例
  • dreamweaver 8完美网页设计 商业网站篇百度网盘下载速度
  • 企业网站能提供哪些服务网络营销环境分析包括哪些内容
  • 如何创建自己的博客网站google搜索下载
  • 如何通过cpa网站做推广搜索引擎优化哪些方面
  • 响应式网站设计的优点360优化大师最新版的功能
  • 吉林 网站备案 照相windows优化大师win10
  • 怎么做网站关键词排名线上营销推广
  • 北京直销网站开发公司电话怎么推广比较好
  • 做网站跟客人怎么沟通一键制作单页网站
  • 怎么做网站网页归档免费网页制作成品
  • 杭州网站建设哪里好如何让百度收录自己信息
  • 唐山建设网站网站b2b网站有哪些平台
  • 做网站收费 优帮云百度账号免费注册
  • 做百科需要参考的网站百度怎么搜索网址打开网页
  • 灰色网站怎么做seo跨境电商平台
  • 网站建设及安全规范百度关键字搜索排名
  • .net 网站优化潮州seo
  • 杨凌网站建设公司下载百度推广app