HUNAN UNIVERSITY ACM/ICPC Judge Online —— Problem 10038 Lowest Bit

本文介绍了C++编程中遇到的数组赋值和输入控制问题。强调了不能直接对数组名赋值,应使用循环逐个元素赋值。同时,针对输入控制,提出了使用`while(1)`结合`cin>>Num`和`if(Num==0) break;`的解决方案。问题出在对题目要求的理解上,原本以为需要先输入后统一打印结果,实际应逐个处理输入并输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这次注意到了几个问题:

1.数组的赋值问题。

数组的赋值只能逐个对数组元素赋值,不能直接对数组名赋值。例如,定义了int i,a[5]后,要将100,200,300,400,500存入数组a中,可用如下程序段实现:for (i=0;i<5;i++)      a[i]=(i+1)*100;  a[0]=100,a[1]=200,a[2]=300,a[3]=400,a[4]=500;但不能将该程序段写成:a={100,200,300,400,500};a[5]={100,200,300,400,500};

所以我将一开始的Bin_Arr[32]={0,0,};(其实也是运行不通过的)改为了for(int z=0;z<30;z++)    Bin_Arr[z]=0;

2.关于控制输入。

题目中要求用0作为输入的结束,一开始我在龟哥的建议下使用while(cin>>Num){.........}控制。但是发现输入值为0的时候直接从主程序就跳出来了。所以用

while(1)
 {
  cin>>Num;
  if(Num==0) break;

................}

是比较好的。

3.关于问题描述。

之所以三次提交才AC不是因为我提交的时候程序有错误。而是问题描述出了问题。我以为要先输入,输入完毕以后集中打印结果。所以一开始还用了一个临时数组储存结果。结果老是RE,后来跑到“马牛不是人”那看了看,原来是这里出了问题。

问题描述:

Lowest Bit 
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB 
Total submit users: 415, Accepted users: 389 
Problem 10038 : No special judgement 
Problem description
Given an positive integer A (1 <= A <= 109), output the lowest bit of A. For example, given A = 26, we can write A in binary form as 11010, so the lowest bit of A is 10, so the output should be 2. Another example goes like this: given A = 88, we can write A in binary form as 1011000, so the lowest bit of A is 1000, so the output should be 8.

 
Input
Each line of input contains only an integer A (1 <= A <= 109). A line containing "0" indicates the end of input, and this line is not a part of the input data.

 
Output
For each A in the input, output a line containing only its lowest bit.

 
Sample Input
26
8
0
 
Sample Output
2
8
 
Problem Source
HNU 1'st Contest
 

郁闷,又是三次才AC! 

 

// 315.cpp : Defines the entry point for the console application.
//

#include 
"stdafx.h"
#include
<iostream>
#include
<math.h>
using namespace std;

//#define MAX_SIZE 30;
int Bin_Arr[30]={0,};    //全局二进制数组
int N=0;    //N记录二进制位数

void DecToBin(int Dec)
{
    
if(Dec==1)
        Bin_Arr[N]
=1;
    
else
    
{
        Bin_Arr[N]
=Dec%2;
        Dec
=Dec/2;
        N
++;
        DecToBin(Dec);
    }

}


int LowBit()
{
    
for(int x=0;x<N+1;x++)
    
{
        
if(Bin_Arr[x]==0)
            
1;
        
else 
            
return pow(2,x);
    }

    
return 0;
}


int main()
{
    
int Num=0,temp=0;
    
int i=0;
    
while(1)
    
{
        cin
>>Num;
        
if(Num==0)    break;
        N
=0;
        
for(int z=0;z<30;z++)
                Bin_Arr[z]
=0;
        DecToBin(Num);
//        for(int p=0;p<30;p++)
//                cout<<Bin_Arr[p];
        temp=LowBit();
        cout
<<temp<<endl;
        i
++;
    }

    
return 0;
}
 

 

同样附上“马牛不是人”的解法(每次他的代码都比我精简很多,诶~):

#include <stdio.h>
#include 
<math.h>

int getbits(int);
main()
{
    
int n;
    scanf(
"%d",&n);
    
while(n!=0){
        printf(
"%.0f ",pow(2,getbits(n)));
        scanf(
"%d",&n);
    }

    
return 0;
}

int getbits(int n)
{
    
if(n==1)return 0;
    
int tmp=n,i=0,s[100];
    
for (i = 0;i<100;i++)s[i]=-1;
    i 
= 0;
    
while(1){
        
if( (s[i++]=tmp%2== 1)return i-1;        
        tmp 
= tmp/2;
    }
}

 

更强的一个解!使用按位运算的解法。明天再贴上关于按位运算的学习心得。(现在这段代码完全看不懂……)

#include <iostream>

using namespace std;

int main()
{
    
int N;
    
int res = 1;
    
while(1)
    
{
        cin 
>> N;
        
if(N == 0)
              
break;
        res 
= 1;
        
while((N & 1== 0)
        
{
              res 
*= 2;
              N 
>>= 1;
        }

        cout 
<< res << endl;
    }


    
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值