首先这道题不用字符串,其次给字符串赋一个整数那么你要做的应该是把整数分解,再逐个给字符串分解
7的倍数可以用 i%7 == 0这里我也看到了
另外整数里是否含7可以获取这个数的所有位判断,第一位向10求余获得,第n位除10^(n-1)获得
if( i % 7 == 0)
counter++;
else if( i%10 == 7 )
counter++;
else
for(d=10;num/d;d*=10)
if( i/d == 7 ){
counter++;
break;
}
变量复制到char数组中: sprintf(char,"格式",变量)
从char数组读出变量: sscanf(char,"格式",变量)
例如:
int main()
{
char a[100];
int x=123,y;
sprintf(a,"%d",x);
printf("%s\n",a);
printf("%c %c %c\n",a[0],a[1],a[2]);
sscanf(a,"%d",&y);
printf("y=%d\n",y);
return 0;
}
#include
void main()
{
unsigned int uiInputNum;
unsigned int i = 0;
unsigned int j;
unsigned int uiCount = 0;
int iRet = 0;
iRet = scanf("%d",&uiInputNum);
if(1 != iRet)
{
printf("Input error!\n");
}
for(i=1; i
if(0 == (i%7))
{
uiCount++;
continue;
}
j = i;
while(j >= 10)
{
if(7 == (j%10))
{
uiCount++;
continue;
}
j %= 10;
}
}
printf("There are %d number(s) hit!\n", uiCount);}