阅读以下说明和C 程序代码,将应填入 (n) 处的字句写在答题纸的对应栏内。
【说明】
下面C 程序代码的功能是:对于输入的一个正整数n(100≤n<1000),先判断其是否是回文数(正读反读都一样的数)。若不是,则将 n 与其反序数相加,再判断得到的和数是否为回文数,若还不是,再将该和数与其反序数相加并进行判断,依此类推,直到得到一个回文数为止。例如,278 不是回文数,其反序数为 872,相加后得到的 1150还不是回文数,再将1150与其反序数511相加,得到的1661是回文数。
函数int isPalm(long m)的功能是:将正整数m的各位数字取出存入数组中,然后判断其是否为回文数。若m是回文数则返回1,否则返回0。
【C 程序代码】
#include <stdio.h>
#include <stdliB.h>
int isPalm(long m)
{ /*判断m是否为回文数*/
int i = 0, k = 0;
char str[32];
while (m > 0) { /*从个位数开始逐个取出m的各位数字并存入字符数组str*/
str[k++] = (1) + ’0’;
m = m / 10;
}
for(i = 0; i < k/2; i++) /*判断str中的k个数字字符序列是否是回文*/
if ( str[i] != str[ (2) ] ) return 0;
return 1;
}
int main( )
{
long n, a, t;
printf("input a positive integer:"); scanf("%ld",&n);
if (n < 100 || n > =1000) return -1 ;
while( (3) ) { /*n不是回文数时执行循环*/
printf("%ld -> ", n);
for(a = 0, t = n; t > 0; ) { /*计算n的反序数并存入a*/
a = (4) *10 + t % 10; t = t / 10;
} /*end of for*/
n = (5) ; /*与反序数求和*/
} /*end of while*/
printf("%ld\n",n);
system("pause"); return 0;
}
正确答案及解析
正确答案
解析
(1) m % 10,或其等价表示 (2) k-1-i
(3) !isPalm(n),或isPalm(n)!=1,或isPalm(n)=0 (4)a (5) n+a
本题考查C程序设计的基本能力。
函数isPalm{long m)的功能是判断m是否为回文数,其方法是先将m的各位数字依次取出转换为对应的数字字符保存在数组str中,然后再判断str中的字符序列是否对称。
代码如下:
While(m>0){ /*从个位数开始逐个取出m的各位数字并存入字符数组str*/
str[k++]=m%10+’0’;
m=m/10;
}
因此,空(1)处应填入“m%10”,将数m的个位数字取出。以上while循环结束时,k的值即为m取初始值时的位数。
若需判断str[0]、str[1]…、str[k-1]中的k个数字字符序列是否对称,则应依次比较str[0]与str[k-1], str[1]与str[k-2]、...str[k/2-1]与str[k/2+l]是否相等,若都相等,则是回文数:若其中有一处不等,则不是回文数。代码如下:
For (i=0; i If (str[i] !=str[ (2) ]) return 0;
因此,空(2)处应填入“k-1-i”。
根据题目描述,从最初输入的数开始,直到得到一个回文数时结束,因此对于数n,调用函数is Palm(n),根据返回值确定n是否为一个回文数,空(3)处应填入“lispalm(n)”。
为了求一个数t的反序数,可从其个位数字开始,依次取出其各位数字并进行组合。下面以t=345举例说明通过整除取余“%”、整除“/”取出各位数字并组合出543的过程。
初始时:a=0 t=345
下一步:345%10=>5 a*10+5=>a=5 t/10=345/10=>t=34
下一步:34%10=>4 a*10+4=>a=54 t/10=34/10=>t=3
下一步:3%10=>3 a*10+3=>a=543 t/10=3/10=>t=0
因此,可知空(4)处应填入“a”。
最后数n与其反序数a相加得到新的数,继续产生回文数的过程。空(5)处应填入“n+a”。
你可能感兴趣的试题
The Internet of( 1)(IoT) describes physical objects that are embedded with Sensor,processing abilities,softwares,and other technologies that connect with other devices and systems over the ( 2) or other communication and exchange data networks .Over the past few years, IoT has become one of the most important technologies of the ( 3 ) centuryWe can connect objects to the Internet via embedded devices. By means of( 4) computing, the cloud, big data, and mobile technologies, physical things can share and collect data with minimal human intervention. Traditional fields of embedded systems,wireless ( 5) networks (WSNs),control systems,automation, independently and collectively enable IoT.回答5处
-
- A.sensor
- B.searching
- C.service
- D.source
- 查看答案
The Internet of( 1)(IoT) describes physical objects that are embedded with Sensor,processing abilities,softwares,and other technologies that connect with other devices and systems over the ( 2) or other communication and exchange data networks .Over the past few years, IoT has become one of the most important technologies of the ( 3 ) centuryWe can connect objects to the Internet via embedded devices. By means of( 4) computing, the cloud, big data, and mobile technologies, physical things can share and collect data with minimal human intervention. Traditional fields of embedded systems,wireless ( 5) networks (WSNs),control systems,automation, independently and collectively enable IoT.回答4处
-
- A.low-level
- B.low-cost
- C.high-cost
- D.high-performance
- 查看答案
The Internet of( 1)(IoT) describes physical objects that are embedded with Sensor,processing abilities,softwares,and other technologies that connect with other devices and systems over the ( 2) or other communication and exchange data networks .Over the past few years, IoT has become one of the most important technologies of the ( 3 ) centuryWe can connect objects to the Internet via embedded devices. By means of( 4) computing, the cloud, big data, and mobile technologies, physical things can share and collect data with minimal human intervention. Traditional fields of embedded systems,wireless ( 5) networks (WSNs),control systems,automation, independently and collectively enable IoT.回答3处
-
- A.19th
- B.20th
- C.21th
- D.22th
- 查看答案
The Internet of( 1)(IoT) describes physical objects that are embedded with Sensor,processing abilities,softwares,and other technologies that connect with other devices and systems over the ( 2) or other communication and exchange data networks .Over the past few years, IoT has become one of the most important technologies of the ( 3 ) centuryWe can connect objects to the Internet via embedded devices. By means of( 4) computing, the cloud, big data, and mobile technologies, physical things can share and collect data with minimal human intervention. Traditional fields of embedded systems,wireless ( 5) networks (WSNs),control systems,automation, independently and collectively enable IoT.回答2处
-
- A.path
- B.Internet
- C.route
- D.switch
- 查看答案
The Internet of( 1)(IoT) describes physical objects that are embedded with Sensor,processing abilities,softwares,and other technologies that connect with other devices and systems over the ( 2) or other communication and exchange data networks .Over the past few years, IoT has become one of the most important technologies of the ( 3 ) centuryWe can connect objects to the Internet via embedded devices. By means of( 4) computing, the cloud, big data, and mobile technologies, physical things can share and collect data with minimal human intervention. Traditional fields of embedded systems,wireless ( 5) networks (WSNs),control systems,automation, independently and collectively enable IoT.回答1处
-
- A.this
- B.thing
- C.think
- D.things
- 查看答案