从C中的一个位置提取一个数字
在这个练习中,他让我创建一个函数从C中的一个位置提取一个数字,c,math,C,Math,在这个练习中,他让我创建一个函数 Number\u pos(N,pos,m)允许提取由m数字组成的数字 使用函数从位置pos 例如: N=12345,位置=2,m=3 数量_pos(N,pos,m)=234 我使用一个extract\u from\u position函数从给定位置提取一个数字,然后使用一个函数计算要提取的数字的位数,然后我有一个镜像函数,它反转数字,然后进行连续除法,直到位数等于我们要提取的数字的位数 问题是:禁止使用镜像功能,你能帮我吗 int Extract_from_po
Number\u pos(N,pos,m)
允许提取由m
数字组成的数字
使用函数从位置pos
例如:
N=12345,位置=2,m=3数量_pos(N,pos,m)=234 我使用一个
extract\u from\u position
函数从给定位置提取一个数字,然后使用一个函数计算要提取的数字的位数,然后我有一个镜像函数,它反转数字,然后进行连续除法,直到位数等于我们要提取的数字的位数
问题是:禁止使用镜像功能,你能帮我吗
int Extract_from_position(int n, int r)
{
int m = 0, s = 0;
while (n != 0)
{
m = n % 10;
s++;
if (s == r)
{
return n;
}
n = n / 10;
}
}
int Number_of_digits(int n)
{
int m = 0, s = 0;
while (n != 0)
{
m = n % 10;
s++;
n = n / 10;
}
return s;
}
int Mirror(int n)
{
int m = 0, s = 0;
while (n != 0)
{
m = n % 10;
s = s * 10 + m;
n = n / 10;
}
return s;
}
int Number_Pos(int N, int pos, int m)
{
int x = Extract_from_position(N, pos);
int y = 0;
int R = Mirror(x);
int T = Number_of_digits(R);
while (T >= m + 1)
{
y = R % 10;
R = R / 10;
T--;
}
return Mirror(R);
}
int main()
{
int n, pos, nbcx;
printf("Give n :");
scanf("%d", &n);
printf("Give the position :");
scanf("%d", &pos);
printf("give the number of digits of the number to extract :");
scanf("%d", &nbcx);
printf("\nThe result after the amber extract from position %d on the right and the number of digits %d is : %d \n", pos, nbcx, Number_Pos(n, pos, nbcx));
}
更新:从右侧计数
如果要从右侧开始计算数字,则
NumberPos
函数将仅为:
#include <stdio.h>
#include <math.h>
int NumberPos(int N, int pos, int m)
{
int trc = (int)(N / (int)pow(10, pos - 1));
trc = trc % (int)pow(10, m);
return trc;
}
int main()
{
int n, pos, nbcx;
printf("Give n :");
scanf("%d", &n);
printf("Give the position :");
scanf("%d", &pos);
printf("give the number of digits of the number to extract :");
scanf("%d", &nbcx);
printf("\nThe result after the amber extract from position %d on the right and the number of digits %d is : %d \n", pos, nbcx, NumberPos(n, pos, nbcx));
}
旧版:这可能是一个解决方案(基本上是4行):
更新:库限制
如果出于任何原因不允许您使用
math.h
或stdlib.h
,您可以:
- 重新执行
读取:pow
- 重新执行abs读取:
- 重新实现数字计数器:
- 类似的方法可能会奏效。我修剪右边你不想要的数字,然后修改以屏蔽左边你不想要的数字
根据您的样本,我假设
pos
是基于1的。如果没有,则需要删除代码上的注释
您可能希望添加错误检查,以确保pos
和num\u数字对给定的N
有效,但这是一个练习
#include <stdio.h>
int Number_of_digits(int n)
{
int count = 0;
while (n != 0)
{
n = n / 10;
++count;
}
return count;
}
int Number_Pos(int N, int pos, int num_digits)
{
int len = Number_of_digits(N);
pos -= 1; //pos is 1 based.
//trim right side
for (int i = 0; i < len - num_digits - pos; ++i)
{
N /= 10;
}
//calculate mod to keep num_digits.
int m = 10;
for (int i = 0; i < num_digits - 1; ++i)
{
m *= 10;
}
return N % m;
}
int main()
{
int n = 1234567;
int pos = 2;
int num_digits = 3;
int result = Number_Pos(n, pos, num_digits);
printf("Num: %d, Pos: %d, Digits: %d - Result: %d\n", n, pos, num_digits, result);
pos = 3;
num_digits = 4;
result = Number_Pos(n, pos, num_digits);
printf("Num: %d, Pos: %d, Digits: %d - Result: %d\n", n, pos, num_digits, result);
pos = 1;
num_digits = 4;
result = Number_Pos(n, pos, num_digits);
printf("Num: %d, Pos: %d, Digits: %d - Result: %d\n", n, pos, num_digits, result);
pos = 6;
num_digits = 2;
result = Number_Pos(n, pos, num_digits);
printf("Num: %d, Pos: %d, Digits: %d - Result: %d\n", n, pos, num_digits, result);
return 0;
}
似乎您可以拆分以删除右侧不想要的数字,然后修改以删除左侧不想要的数字。不使用镜像功能如何修改以删除左侧不想要的数字?注意:Extract\u from\u position(0,0)
从不指定返回的内容。代码工作不正常。。。给出n:12345给出位置:2给出要提取的数字的位数:2从右侧位置2琥珀色提取后的结果,位数2为:23@MEDLDN等等,对不起,怎么了?2位12345的2位数字为23。。没错。我误解了这个问题或某些参数?2位12345的2位数字是34,从右到左开始计数,从右到右提取2个数字left@MEDLDN绝对不是,并且此代码提供的结果正是您标记为答案的代码提供的结果。唯一可能造成误解的是,是开始计算索引为0还是索引为1的第一位数字。我从索引1开始计数。对不起,我真的看不出有什么问题,如果有问题的话,那我的工作就是偶然的。仅基于这个示例,我假设从左侧开始计算以1为基础的计数,以找到提取的开始,然后从中提取X位数字。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int NumberPos(int N, int pos, int m)
{
int digit = floor(log10(abs(N))) + 1;
int trc = N % (int)pow(10, digit - pos + 1);
digit = floor(log10(abs(trc))) + 1;
trc = (int)(trc / (int)pow(10, digit - m));
return trc;
}
int main()
{
int n, pos, nbcx;
printf("Give n :");
scanf("%d", &n);
printf("Give the position :");
scanf("%d", &pos);
printf("give the number of digits of the number to extract :");
scanf("%d", &nbcx);
printf("\nThe result after the amber extract from position %d on the right and the number of digits %d is : %d \n", pos, nbcx, NumberPos(n, pos, nbcx));
}
Give n :12345
Give the position :2
give the number of digits of the number to extract :2
The result after the amber extract from position 2 on the right and the number of digits 2 is : 23
#include <stdio.h>
int Number_of_digits(int n)
{
int count = 0;
while (n != 0)
{
n = n / 10;
++count;
}
return count;
}
int Number_Pos(int N, int pos, int num_digits)
{
int len = Number_of_digits(N);
pos -= 1; //pos is 1 based.
//trim right side
for (int i = 0; i < len - num_digits - pos; ++i)
{
N /= 10;
}
//calculate mod to keep num_digits.
int m = 10;
for (int i = 0; i < num_digits - 1; ++i)
{
m *= 10;
}
return N % m;
}
int main()
{
int n = 1234567;
int pos = 2;
int num_digits = 3;
int result = Number_Pos(n, pos, num_digits);
printf("Num: %d, Pos: %d, Digits: %d - Result: %d\n", n, pos, num_digits, result);
pos = 3;
num_digits = 4;
result = Number_Pos(n, pos, num_digits);
printf("Num: %d, Pos: %d, Digits: %d - Result: %d\n", n, pos, num_digits, result);
pos = 1;
num_digits = 4;
result = Number_Pos(n, pos, num_digits);
printf("Num: %d, Pos: %d, Digits: %d - Result: %d\n", n, pos, num_digits, result);
pos = 6;
num_digits = 2;
result = Number_Pos(n, pos, num_digits);
printf("Num: %d, Pos: %d, Digits: %d - Result: %d\n", n, pos, num_digits, result);
return 0;
}
Num: 1234567, Pos: 2, Digits: 3 - Result: 234
Num: 1234567, Pos: 3, Digits: 4 - Result: 3456
Num: 1234567, Pos: 1, Digits: 4 - Result: 1234
Num: 1234567, Pos: 6, Digits: 2 - Result: 67