#8163: 一直記憶體錯誤 來請教一下..


tinbtinb (Dawn)


以下是我的程式碼
#include<cstdio>
int main(void)
{
int s;
while(scanf("%d",&s)==1)
{
int x=0,st[100000]={0};
if(s==0)
{
continue;
}
st[x]=s;
while(st[x]!=0)
{
x++;
scanf("%d",&st[x]);
}
for(int i=0;i<x;i++)
{
int a=0,count=0,tw[100]={0};
while(st[i]!=0)
{
tw[a]=st[i]%2;
st[i]/=2;
a++;
}
tw[0]+=1;
for(int i=0;i<a;i++)
{
if(tw[i]>1)
{
tw[i+1]+=tw[i]/2;
tw[i]%=2;
count++;
}
}
printf("%d\n",count);
}
}
}
別的地方跑都過,但送出解答卻顯示 記憶體區段錯誤!        
請問一下是哪裡有錯呢?
#8166: Re:一直記憶體錯誤 來請教一下..


a450 (要学会宽容)


 
s == 0 break;
这样呢 

#8167: Re:一直記憶體錯誤 來請教一下..


tinbtinb (Dawn)


 
s == 0 break;
这样呢  

	if(s==0)
{
break;
}
這樣還是記憶體錯誤.. 
#8169: Re:一直記憶體錯誤 來請教一下..


a450 (要学会宽容)


st[x]=s;
while(st[x]!=0)
{
x++;
scanf("%d",&st[x]);
}
为什么不直接转换二进制 不知道你写的这个什么意思 

#8176: Re:一直記憶體錯誤 來請教一下..


tinbtinb (Dawn)


st[x]=s;
while(st[x]!=0)
{
x++;
scanf("%d",&st[x]);
}
为什么不直接转换二进制 不知道你写的这个什么意思 


原本寫成 輸入就輸出 都顯示多餘輸出 才想說改成先輸入進陣列再一次輸出

這次再改成直接輸出竟然過了....  雖然還是不懂上面錯在哪

 

謝謝你回覆