目录
前言
本文讲解:迭代求根、字符串加法、字符串拆分
🏠我的主页:我的主页
📚系列专栏:系列专栏
![](https://i-blog.csdnimg.cn/direct/77004313c3d740a3a78bd69dee912c35.png)
一、程序填空📝 --- 迭代求根
难度:⭐⭐
题目📃
输入一个实数 a,根据迭代公式 x1=(x0 + a/x0)/2,计算并输出其平方根。
要求误差不超过 1e-6(即 0.000001)
代码如下:
在1️⃣2️⃣3️⃣处填空
cpp
#include <stdio.h>
main( )
{
double a,x0,x1;
printf("输入一个实数, 本程序计算并输出其平方根\n");
do
/**********************found***********************/
1️⃣
while(a<=0);
x1=a/2.0;
do
{
/**********************found***********************/
2️⃣
x1=(x0+a/x0)/2.0;
/**********************found***********************/
}while((x1-x0)>1e-6 3️⃣ );
printf("%f 的平方根是 %f\n",a,x1);
}
分析🧐
这里提到迭代公式,就要先到值的覆盖
例如这个公式,这里的x1代表当前的值,x0代表上一次的值
现在依次分析下代码
- 填写 scanf("%lf", &a);
由于在这个do - while循环里,又根据这个判断条件
如果小于等于0就循环,也就是如果不满足条件就循环
直到满足条件为止 - 填写 x0 = x1;
这里就要注意,迭代了,x0是上一次的值
x1是当前的值,所以要这样赋值 - 填写 || (x1-x0) < -(1e-6)
由题目可知误差不超过1e-6
所以这个循环就是让误差不超过1e-6为止
当不满足这个条件就一直循环
解答代码如下:
cpp
#include <stdio.h>
main( )
{
double a,x0,x1;
printf("输入一个实数, 本程序计算并输出其平方根\n");
do
/**********************found***********************/
1️⃣scanf("%lf", &a);
while(a<=0);
x1=a/2.0;
do
{
/**********************found***********************/
2️⃣x0 = x1;
x1=(x0+a/x0)/2.0;
/**********************found***********************/
}while((x1-x0)>1e-6 3️⃣|| (x1-x0) < -(1e-6) );//超出了这个误差
printf("%f 的平方根是 %f\n",a,x1);
}
二、程序修改🛠️ --- 字符串加法
难度:⭐
题目📃
函数 char *fun (char * s1,char *s2, char *r) 的功能:
将两个长度相等的纯数字字符串当作两个加数,求其代表的数值之和并以字符串的形式存入 r 所指内存且返回。
如:s1 是 "723",s2 是 "567",则返回的字符串为 "1290"。要考虑进位。 请改正程序中的错误,使它能得出正确的结果。 注意:不要改动 main 函数,不得增行或删行,也不得更改程序的结构!
代码如下:
在代码中找出3个错误并修改
cpp
#include <stdio.h>
#include <string.h>
char *fun(char * s1,char *s2,char *r)
{
int len,cy=0,md;
char *p,*q,*t;
len=strlen(s1);
p=s1+len-1;
q=s2+len-1;
t=r+len;
/**********************found***********************/
*t='\0';
while(p>=s1)
{
/**********************found***********************/
md=*p+*q-'0'+cy;
if(md>=10) cy=1;
else cy=0;
*t=md%10+'0';
p--;
q--;
t--;
}
/**********************found***********************/
if(cy=1)
*t='1';
else
{
while(*r++=*++t);
}
return r;
}
void main()
{
char s1[100],s2[100],s3[101];
strcpy(s1,"65743");
strcpy(s2,"85339");
fun(s1,s2,s3);
printf("%s+%s=%s\n",s1,s2,s3);
}
分析🧐
这道题,看着题目感觉有点难实现,但是看程序挖的空,还是很容易发现的
- 第12行
改成 *(t + 1)='\0';
由上方的指针p和q就可以知道,这样指针是指向
对应字符串的末尾(因为加法是从个位开始的) - 第16行
改成 md=*p-'0'+*q-'0'+cy;
这里很容易发现,字符要变成数字
就要减去字符零 - 第25行
改成 if(cy==1)
这里也很容易发现,是判断相等的
解答代码如下:
cpp
#include <stdio.h>
#include <string.h>
char *fun(char * s1,char *s2,char *r)
{
int len,cy=0,md;
char *p,*q,*t;
len=strlen(s1);
p=s1+len-1;//末尾
q=s2+len-1;
t=r+len;
/**********************found***********************/
1️⃣*(t+1)='\0';
while(p>=s1)
{
/**********************found***********************/
2️⃣md=*p-'0'+*q-'0'+cy;//转成数字
if(md>=10) cy=1;
else cy=0;
*t=md%10+'0';
p--;
q--;
t--;
}
/**********************found***********************/
3️⃣if(cy==1)
*t='1';
else
{
while(*r++=*++t);
}
return r;
}
void main()
{
char s1[100],s2[100],s3[101];
strcpy(s1,"65743");
strcpy(s2,"85339");
fun(s1,s2,s3);
printf("%s+%s=%s\n",s1,s2,s3);
}
三、程序设计💻 --- 字符串拆分
难度:⭐⭐
题目📃
编写函数 char *fun (char * s0,char *s1,char *s2, char *s3)
要求实现:将 s0 所指字符串分解成三个字符串,分别存入 s1、s2、s3 所指内存中。分解的方法是,s1、s2、s3 从 s0 中依次顺序每隔 3 个字符取 1 个。 例如,s0 为 "abcdefghij" 时,分解完成后,s1、s2、s3 分别为:"adgj"、"beh"、"cfi"。 注意:部分源程序在文件 PROG1.C 中。 请勿改动主函数 main 和其它函数中的任何内容, 仅在函数 fun 的花括号中填入你编写的若干语句。
代码如下
在fun函数中编写
cpp
#include <stdio.h>
#include <string.h>
void fun(char * s0,char *s1,char *s2, char *s3)
{
}
void main()
{
char s0[100],s1[40],s2[40],s3[40];
printf("请输入一行字符串\n");
gets(s0);
fun(s0,s1,s2,s3);
puts(s1);
puts(s2);
puts(s3);
}
分析🧐
这道编程题还是挺简单的,直接可以用三个for循环来解决,三个循环对应着三个指针
依次实现对于的功能
每个循环都是从对应的位置开始循环
解答代码如下:
cpp
#include <stdio.h>
#include <string.h>
void fun(char * s0,char *s1,char *s2, char *s3)
{
//i控制着每个循环从哪个位置开始
//j控制着每个指针的下标,所以每次一个循环后就要将其赋值为0
int i, j = 0;
int len = strlen(s0);
for(i = 0; i < len; i+=3)
{
s1[j++] = s0[i];
}
//最后都要记得加上斜杠零
s1[j] = '\0';
j = 0;
for(i = 1; i < len; i+=3)
{
s2[j++] = s0[i];
}
s2[j] = '\0';
j = 0;
for(i = 2; i < len; i+=3)
{
s3[j++] = s0[i];
}
s3[j] = '\0';
}
void main()
{
char s0[100],s1[40],s2[40],s3[40];
printf("请输入一行字符串\n");
gets(s0);
fun(s0,s1,s2,s3);
puts(s1);
puts(s2);
puts(s3);
}
希望本篇文章能够帮助到你😊