題目https://www.patest.cn/contests/pat-b-practise/1057
遇到的問(wèn)題:
1.求字符串長(zhǎng)度匾乓,因?yàn)槲易铋_(kāi)始用的是string,所以選擇string.size(),但是由于cin函數(shù)的問(wèn)題调衰,我改為了gets(),所以字符串也用字符數(shù)組替換,用了strlen()函數(shù)吩蔑,頭文件為string.h;
輸入函數(shù)http://www.cnblogs.com/flatfoosie/archive/2010/12/22/1914055.html
2.記錄二進(jìn)制的0,1問(wèn)題
while(sum != 0)
{
int res = sum % 2;
if(res == 0)
countZero ++;
else
countOne ++;
sum = sum / 2;
}
我還看到了一個(gè)方法
#include<iostream>
using namespace std;
void printbinary(const unsigned int val)
{
for(int i = 16; i >= 0; i--)
{
if(val & (1 << i))
cout << "1";
else
cout << "0";
}
}
int main()
{
printbinary(1024);
return 0;
}
3.其他進(jìn)制轉(zhuǎn)換
http://blog.csdn.net/xiaofei2010/article/details/7434737
題目代碼
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
char str[100002];
using namespace std;
int main()
{
gets(str);
int sum=0;
for(int i=0;i<=strlen(str);i++)
{
if(str[i]>='a'&&str[i]<='z')
{
sum+=str[i]-'a'+1;
}
if(str[i]>='A'&&str[i]<='Z')
{
sum+=str[i]-'A'+1;
}
}
int countZero=0,countOne=0;
while(sum != 0)
{
int res = sum % 2;
if(res == 0)
countZero ++;
else
countOne ++;
sum = sum / 2;
}
cout<<countZero<<" "<<countOne;
return 0;
}