int main()
{
char *str="1212321234321234543212345654321..........";
int i=0,n,s=0,num=0;
printf("INPUT THE NUMBER\n");
scanf("%d",&n);
while(s<n)
{
i++;
s=s+2*i;
}
printf("the i is %d \n",i);
printf("the origin s is %d\n",s);
s=s-2*i;
printf("the s is %d\n",s);
getch();
num=(n-s-i-1)>0?(n-s-i-1):(-(n-s-i-1));
printf("the orgin num is %d\n",num);
getch();
num=i+1-num;
printf("the number is %d \n",num);
getch();
}
我写的,有点乱
> 123453
>+ 345386
> = 0468839
> 999
>+ 111
> = 1110
> 112
>+ 11
> = 0123
测试了几个数 都对的,没多调试
写错无怪(我也是只菜鸟)
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MX 500
int inspect(char *num)
{
for(; isdigit(*num); num++ )
{
}
return *num;
}
int getstr(char *num, char *num2)
{
printf(" > ");
gets(num);
printf(" >+ ");
gets(num2);
if( inspect(num) || inspect(num2) ) //检查输入是否为数字
{
printf(" =>> error !\n");
}
return 1;
}
char *plus(char *num, char *num2)
{
int a, b, c, k;
char *sum = NULL;
for(a=0; *num; num++, a++)
{
}
for(b=0; *num2; num2++, b++)
{
}
c = (a > b) ? a : b;
sum = (char *)malloc( sizeof(char)*c + 2);
sum[c+1] = '\0';
num--, num2--;
for(k=0; c != -1; c--) //计算合并到sum中
{
sum[c] = *num + *num2 - 48 + k;
if(sum[c] >= 58)
{
k = 1;
sum[c] = sum[c] - 10;
}
else
{
k = 0;
}
if(--a <= 0)
{
*num = '0';
}
else
{
num--;
}
if(--b <= 0)
{
*num2 = '0';
}
else
{
num2--;
}
}
return sum;
}
int main()
{
char num[MX]={0}, num2[MX]={0} ;
char *sum = NULL;
for(; getstr(num,num2); )
{
sum = plus(num,num2);
printf(" > = %s\n\n",sum);
free(sum);
}
return 0;
}