#include<stdio.h>
#include<math.h>
long long int GetFigures(long long int n)//求位数
{
long long int num=0;
while(n!=0)
{
n/=10;
num++;
}
return num;
}
void PrintOrder(long long int n)
{
long long int len=GetFigures(n);
int i=0;
long long int power=pow(10.0,len-1);
while(n!=0)
{
printf("%lld_",n/power);//输出最高位
n%=power;//丢弃最高位
power/=10;//power减10
}
printf("\n");
}
void PrintReverse(long long int n)
{
while(n!=0)
{
printf("%lld_",n%10);
n/=10;
}
}
int main()
{
long long int n;
scanf("%lld",&n);
void PrintOrder(long long int n);
PrintOrder(n);
void PrintReverse(long long int n);
PrintReverse(n);
}
供参考:
#include <stdio.h>
int main()
{
char s[501]={0};
int i=0,k=0;
scanf("%s", s);
while(s[i])
{
if(i==0 && s[i] == '0')
while(s[i] == '0' && s[i]) i++;
else{
printf("%c_",s[i]);
s[k++] = s[i++];
}
}
s[k] = '\0';
printf("\n");
while(k--) printf("%c_",s[k]);
printf("\n");
return 0;
}
这么改下:
#include<stdio.h>
#include<string.h>
int main()
{
char a[501];
int i = 0,k,flg;
gets(a);
int n=strlen(a);
for (i = 0,k = 0,flg = 1;i < n; i++)
{
if(a[i] == '0' && flg)
continue;
flg = 0;
printf("%c_",a[i]);
a[k++] = a[i];
}
printf("\n");
for (i = k - 1; i >= 0; i--)
{
printf("%c_", a[i]);
}
return 0;
}
样例中的测试用例明显超了long long int了所以你要用字符串来做 所以测试用例位数很长的时候你的结果就是错的
题目说的很明确了,500位以内的正整数
你算算long long最多能存多少位
很明显这题必须用数组做
输入明显是个字符串,不是数值。用字符串数组接收数据。去掉前导0后输出。
int main()
{
char str[500] = {0}, ch;
int i = 0, n = 0;
while (1)
{
ch = getchar();
if (n == 0 && ch == '0') //忽略前导0
continue;
if (ch == '\n')
break;
str[n++] = ch;
}
for (i = 0; i < n; i++)
{
printf("%c_", str[i]);
}
printf("\n");
for (i = n - 1; i >= 0; i--)
{
printf("%c_", str[i]);
}
}