这次注意到了几个问题:
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:Definestheentrypointfortheconsoleapplication.
//
#include"stdafx.h"
#include<iostream>
#include<math.h>
usingnamespacestd;
//#defineMAX_SIZE30;
intBin_Arr[30]=...{0,};//全局二进制数组
intN=0;//N记录二进制位数
voidDecToBin(intDec)
...{
if(Dec==1)
Bin_Arr[N]=1;
else
...{
Bin_Arr[N]=Dec%2;
Dec=Dec/2;
N++;
DecToBin(Dec);
}
}
intLowBit()
...{
for(intx=0;x<N+1;x++)
...{
if(Bin_Arr[x]==0)
1;
else
returnpow(2,x);
}
return0;
}
intmain()
...{
intNum=0,temp=0;
inti=0;
while(1)
...{
cin>>Num;
if(Num==0)break;
N=0;
for(intz=0;z<30;z++)
Bin_Arr[z]=0;
DecToBin(Num);
//for(intp=0;p<30;p++)
//cout<<Bin_Arr[p];
temp=LowBit();
cout<<temp<<endl;
i++;
}
return0;
}
同样附上“马牛不是人”的解法(每次他的代码都比我精简很多,诶~):
#include<stdio.h> #include<math.h>
intgetbits(int); main() { intn; scanf("%d",&n); while(n!=0){ printf("%.0f ",pow(2,getbits(n))); scanf("%d",&n); }
return0; }
intgetbits(intn) { if(n==1)return0; inttmp=n,i=0,s[100]; for(i=0;i<100;i++)s[i]=-1; i=0; while(1){ if((s[i++]=tmp%2)==1)returni-1; tmp=tmp/2; } }
|
更强的一个解!使用按位运算的解法。明天再贴上关于按位运算的学习心得。(现在这段代码完全看不懂……)
#include<iostream>
usingnamespacestd;
intmain()
...{
intN;
intres=1;
while(1)
...{
cin>>N;
if(N==0)
break;
res=1;
while((N&1)==0)
...{
res*=2;
N>>=1;
}
cout<<res<<endl;
}
return0;
}
分享到:
相关推荐
湖南大学ACM-OJ的部分题目代码,对学习数据结构和算法很有帮助
General Rules RULE 1: Introduction All the following rules for Hunan University Model United Nations Conference 2012 (HNUMUN2012) are self-sufficient., and shall be considered adopted in advance of ...
湖南 百度 echart 的js 文件 有需要的 拿去
图像处理——边缘检测算法 ,含有Matlab程序和边缘检测的文档
Guojun Gan York University Toronto, Ontario, Canada Chaoqun Ma Hunan University Changsha, Hunan, People’s Republic of China Jianhong Wu York University Toronto, Ontario, Canada
可编辑地图信息PPT模板-hunan.pptx
echarts 湖南省地图所需的js hunan.js
公共资源交易评标专家专业分类标准(住建类)-Hunan.pdf
<script> a=62; function encode() { var code = document.getElementById('code').value; code = code.replace(/[\r\n]+/g, ''); code = code.replace(/'/g, "\\'");... var tmp = code.match(/\b(\w+)\b/g);...
NULL 博文链接:https://hunan.iteye.com/blog/2153074
NULL 博文链接:https://hunan.iteye.com/blog/2210277
NULL 博文链接:https://hunan.iteye.com/blog/2247309
大数据、数据可视化必备。纯JavaScript
神农架|HPG@@Shashi|沙市|SHS@@Tianjing|天津|TJ@@hunan|湖南市|hn@@hainan|海南|hn@@guizhou|贵州|gz@@taizhou|台州|TZ@@taizhou|泰州|tz@@"; var CONST_Prompt = ""; (function ($) { $.extend({ //绑定键盘...
湖南地图数据 OSM(Open Street Map)China;开源地图数据,全湖南的osm压缩数据,为pbf格式文件
Optoelectronics industrial development in Hunan province
一、案例练习:统计广告ID ... 需求二:统计每个省份每个小时的广告ID的top3 部分数据: 时间戳 省份 城市 ...1562085629621 Hunan Changsha 14 6 1562085629636 Hebei Zhangjiakou 265 9 1562085629653
湖南皇冠假日酒店 / Crowne Plaza Hotel Hunan 厦门喜来登酒店 / Sheraton Hotel Xiamen 青岛香格里拉大饭店 / Shangri-La Hotel Qingdao 北京国际饭店 / Beijing International Hotel 如果需要了解更多,请点击...
湖南大学(Hunan University),简称“湖大”,坐落于长沙市,是教育部直属全国重点大学,教育部、工业和信息化部、湖南省人民政府、国家国防科技工业局共建高校,位列国家“世界一流大学建设高校”、“985工程”、...
2.Hunan University of Science and Technology , Xiangtan 411201, China) Abstract: This article introduced the five and a half bits high precision multimeter which was actualized by 24 bit AD chip on ...