c语言大一某测试题 求大佬指点
编写一个函数Take(),该函数返回正整数n的第k位数。例如:如果n为543210,则调用函数Take(n,0)返回数字0,调用函数Take(n,3)则返回数字3。
注意:(1)数字的位次顺序从右到左,且从0开始。
(2)题目要求的是找一个正整数的第几位数,并把这个位的数数出来 #include <stdio.h>
size_t Take(size_t num, size_t index)
{
while(index--)
num /= 10;
return num % 10;
}
int main(void)
{
printf("%lu\n", Take(123456, 0));
printf("%lu\n", Take(123456, 2));
printf("%lu\n", Take(123456, 3));
printf("%lu\n", Take(123456, 5));
return 0;
}
code ver 0.0.1
本帖最后由 bin554385863 于 2019-10-29 10:38 编辑#include <stdio.h>
#include <malloc.h>
typedef struct
{
int date;
size_t index;
} Date_index;
void fun(int num, size_t _index, Date_index *ptr)
{
int tmp = 0;
ptr->index = _index;
while (_index--)
{
tmp = num % 10;
num /= 10;
}
ptr->date = tmp;
}
int main(int argc, char const *argv[])
{
int num = 123696;
Date_index *a = (Date_index*)malloc(sizeof(Date_index));
for (size_t i = 6; i > 0; i--)
{
fun(num, i, a);
printf("数字{%d}的第[%d]为是%d\n", num, a->index, a->date);
}
free(a);
return 0;
}
------------------------------------------------------------------------------------------------------
E:\Users\86184\Documents\Code>cmd /C "c:\Users\86184\.vscode\extensions\ms-vscode.cpptools-0.26.0\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-ko3k1myj.ina --stdout=Microsoft-MIEngine-Out-3gip4dkr.isa --stderr=Microsoft-MIEngine-Error-fijnybi3.ege --pid=Microsoft-MIEngine-Pid-oyykru0o.w1o "--dbgExe=E:\My Program\MinGW\bin\gdb.exe" --interpreter=mi "
数字{123696}的第为是1
数字{123696}的第为是2
数字{123696}的第为是3
数字{123696}的第为是6
数字{123696}的第为是9
数字{123696}的第为是6
E:\Users\86184\Documents\Code>
{:10_285:} MMP字打错了 bin554385863 发表于 2019-10-28 18:05
-------------------------------------------------------------------------------------------------- ...
注意:(1)数字的位次顺序从右到左,且从0开始。
code ver 0.0.2
本帖最后由 bin554385863 于 2019-10-29 10:38 编辑人造人 发表于 2019-10-28 21:38
注意:(1)数字的位次顺序从右到左,且从0开始。
#include <stdio.h>
#include <malloc.h>
typedef struct
{
int date;
size_t index;
} Date_index;
void fun(int num, size_t _index, Date_index *ptr)
{
int tmp = 0;
ptr->index = _index;
while (_index--)
{
tmp = 0;
tmp = num % 10;
num /= 10;
}
ptr->date = tmp;
}
int main(int argc, char const *argv[])
{
int num = 123696;
Date_index *a = (Date_index*)malloc(sizeof(Date_index));
for (size_t i = 0; a->date != 1; i++)
{
fun(num, i, a);
printf("数字{%d}的第[%d]位是%d\n", num, a->index, a->date);
}
free(a);
return 0;
}
---------------------------------------------------------------------------------------------
E:\Users\86184\Documents\Code>cmd /C "c:\Users\86184\.vscode\extensions\ms-vscode.cpptools-0.26.0\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-2g23kszp.xwg --stdout=Microsoft-MIEngine-Out-zp5nnwtx.0lm --stderr=Microsoft-MIEngine-Error-igzwk35o.v01 --pid=Microsoft-MIEngine-Pid-bz3tceqy.x3l "--dbgExe=E:\My Program\MinGW\bin\gdb.exe" --interpreter=mi "
数字{123696}的第位是0
数字{123696}的第位是6
数字{123696}的第位是9
数字{123696}的第位是6
数字{123696}的第位是3
数字{123696}的第位是2
数字{123696}的第位是1
E:\Users\86184\Documents\Code> 数字{123696}的第位是0
code ver 1.0.0
本帖最后由 bin554385863 于 2019-10-29 10:39 编辑人造人 发表于 2019-10-28 22:10
数字{123696}的第位是0
{:10_285:}
#include <stdio.h>
#include <malloc.h>
typedef struct
{
int date;
size_t index;
} Date_index;
void fun(int num, size_t _index, Date_index *ptr)
{
int tmp = 0;
ptr->index = _index;
while (1 + _index--)
{
tmp = num % 10;
num /= 10;
}
ptr->date = tmp;
}
int main(int argc, char const *argv[])
{
int num = 123696;
Date_index *a = (Date_index *)malloc(sizeof(Date_index));
for (size_t i = 0; a->date != 1; i++)
{
fun(num, i, a);
printf("数字{%d}的第[%d]位是%d\n", num, a->index, a->date);
}
free(a);
return 0;
}
--------------------------------------------------
E:\Users\86184\Documents\Code>cmd /C "c:\Users\86184\.vscode\extensions\ms-vscode.cpptools-0.26.0\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-kcq1a20e.afc --stdout=Microsoft-MIEngine-Out-lykau0fh.z2i --stderr=Microsoft-MIEngine-Error-y5siqckk.dt4 --pid=Microsoft-MIEngine-Pid-zibmxyei.jbt "--dbgExe=E:\My Program\MinGW\bin\gdb.exe" --interpreter=mi "
数字{123696}的第位是6
数字{123696}的第位是9
数字{123696}的第位是6
数字{123696}的第位是3
数字{123696}的第位是2
数字{123696}的第位是1
E:\Users\86184\Documents\Code>
页:
[1]