neuq oj 1043: 谭浩强C语言(第三版)习题5.7 C语言

数字与字符串的转换

1043: 谭浩强C语言(第三版)习题5.7

时间限制:  1 Sec   内存限制:  128 MB
提交:  451   解决:  226
[ 提交 ][ 状态 ][ 讨论版 ]

题目描述

给出一个不多于5位的整数,要求 1、求出它是几位数 2、分别输出每一位数字 3、按逆序输出各位数字,例如原数为321,应输出123

输入

一个不大于5位的数字

输出

三行 第一行 位数 第二行 用空格分开的每个数字,注意最后一个数字后没有空格 第三行 按逆序输出这个数

样例输入

12345

样例输出

5
1 2 3 4 5
54321

提示

哈姆雷特:数字还是字符?这是一个问题!

代码

#include   
main()  
{ long int num;  
  int indiv,ten,hundred,thousand,ten_thousand,place;  
                
  scanf("%ld",&num);  
  if (num>9999)  place=5;  
  else  if(num>999)  place=4;  
  else  if(num>99)  place=3;  
  else  if(num>9)  place=2;  
  else  place=1;  
  printf("%d\n", place);  
  ten_thousand=num/10000;  
  thousand=num/1000%10;  
  hundred=num/100%10;  
  ten=num%100/10;  
  indiv=num%10;  
  switch(place)  
  { case 5: printf("%d %d %d %d %d",ten_thousand,thousand,hundred,ten,indiv);  
          printf("\n%d%d%d%d%d\n",indiv,ten,hundred,thousand,ten_thousand);  
          break;  
    case 4: printf("%d %d %d %d",thousand,hundred,ten,indiv);  
          printf("\n%d%d%d%d\n",indiv,ten,hundred,thousand);  
          break;  
    case 3: printf("%d %d %d",hundred,ten,indiv);  
          printf("\n%d%d%d\n",indiv,ten,hundred);  
          break;  
    case 2: printf("%d %d",ten,indiv);  
          printf("\n%d%d\n",indiv,ten);  
          break;  
    case 1: printf("%d",indiv);  
          printf("\n%d\n",indiv);  
          break;  
  }  
}  


下面做法并未经过oj系统测试,但我认为正确。

#include
#include
#define MAXSIZE 5

int main()
{
 int num,j=0,i=0;
 while(scanf("%d",&num)!=EOF&&num>=0)
{
 char temp[MAXSIZE],str[MAXSIZE];
 //printf("number=%d\n",num);
 //itoa(num,str,10);
 while(num)
 {
  temp[i]=num%10+'0';
  i++;
  num=num/10;
 }
 temp[i]='\0';   //i=5时要赋值
 i=i-1;
 while(i>=0)     //刚才转化的字符串是逆序的,必须把它反转过来
 {
  str[j]=temp[i];
  j++;
  i--;
 }
 str[j]='\0';
 
 
 int len=strlen(str);
 if(len<=5&&len>0)
 {
 printf("%d\n",strlen(str));
 for(i=0;i


体会:

整数转化为字符串的方法:

好的博客:C语言字符串与数值之间的转换

两个方法:
一种方式是利用非标准C语言扩展函数

● itoa():将整型值转换为字符串。

       ● ltoa():将长整型值转换为字符串。

       ● ultoa():将无符号长整型值转换为字符串。

       ● gcvt():将浮点型数转换为字符串,取四舍五入。

       ● ecvt():将双精度浮点型值转换为字符串,转换结果中不包含十进制小数点。

       ● fcvt():指定位数为转换精度,其余同ecvt()。

 

       ● atof():将字符串转换为双精度浮点型值。

       ● atoi():将字符串转换为整型值。

       ● atol():将字符串转换为长整型值。

       ● strtod():将字符串转换为双精度浮点型值,并报告不能被转换的所有剩余数字。

       ● strtol():将字符串转换为长整值,并报告不能被转换的所有剩余数字。

       ● strtoul():将字符串转换为无符号长整型值,并报告不能被转换的所有剩余数字。

另一种方式是数字%10+‘0’,逆序反转。

while(num)
 {
  temp[i]=num%10+'0';
  i++;
  num=num/10;
 }
 temp[i]='\0';   //i=5时要赋值
 i=i-1;
 while(i>=0)     //刚才转化的字符串是逆序的,必须把它反转过来
 {
  str[j]=temp[i];
  j++;
  i--;
 }
 str[j]='\0';


求整数的位数:

两个方法,一种方式是用循环求,不断除10。
另一种方式是将其转换为字符串,然后求字符串的位数。

#include
#include
int f1(int n){
    int i=0;
    if(n==0)return 1;
    while(n>0){
        n=n/10;
        i++;
    }
    return i;
}
int f2(int n){
    char s[32]="";
    sprintf(s,"%d",n);
    return strlen(s);
}
main()
{
    int n;
    scanf("%d",&n);
    printf("%d has %d bits\n",n,f1(n));
    printf("%d has %d bits\n",n,f2(n));
}

你可能感兴趣的:(C++,neuq,oj,C语言,1043,谭浩强C语言第三版习题5.7,数值与字符串转换)