I
,输出二进位表示法与 parity
I
直到 0parity
其实我看不懂题目後面还有写
or 1
是什麽意思 QAQ
The parity of B is P (mod 2).
B
是 I
的二进位表示法P
就是 parity
while
回圈读入整数 I
直到 0
int I, P;
while(scanf("%d", &I) && I){
...
}
while
回圈每次都透过 % 2
检查奇偶 (代表最後一位 0 or 1),并存放到字元阵列里,并计算 parity
的次数
char B[50] = {0};
P = 0;
k = 0;
while(I > 0){
if(I % 2){
B[k] = '1';
P++;
}
else{
B[k] = '0';
}
k++;
I = I / 2;
}
用字元阵列就是因为原本想用
strrev()
来偷懒
结果好像因为 Judge 的环境问题
蛮高机会有可能不支援 QAQ
而且多余的 0 也会被输出
// printf("The parity of %s is %d (mod 2).\n", strrev(B), P);
printf("The parity of ");
for(i = strlen(B) - 1; i >= 0; i--){
printf("%c", B[i]);
}
printf(" is %d (mod 2).\n", P);
#include<stdio.h>
#include<string.h>
int main(){
int I, P;
int k;
int i;
while(scanf("%d", &I) && I){
char B[50] = {0};
P = 0;
k = 0;
while(I > 0){
if(I % 2){
B[k] = '1';
P++;
}
else{
B[k] = '0';
}
k++;
I = I / 2;
}
// printf("The parity of %s is %d (mod 2).\n", strrev(B), P);
printf("The parity of ");
for(i = strlen(B) - 1; i >= 0; i--){
printf("%c", B[i]);
}
printf(" is %d (mod 2).\n", P);
}
return 0;
}
& 1
一样会使结果只有 0 or 1,每次都右移一个,检查最右位元
int P = 0;
int bit[31] = {0};
for(i = 0; I > 0; i++){
if(I & 1){
P++;
}
bit[i] = I & 1;
I >>= 1;
}
#include<stdio.h>
int main(){
int I;
int i, j;
while(scanf("%d", &I) && I){
int P = 0;
int bit[31] = {0};
for(i = 0; I > 0; i++){
if(I & 1){
P++;
}
bit[i] = I & 1;
I >>= 1;
}
printf("The parity of ");
for(j = i - 1; j >= 0; j--){
printf("%d", bit[j]);
}
printf(" is %d (mod 2).\n", P);
}
return 0;
}
Heap Heap(堆积)是一个阵列,可以把它看作类似完全二元树(也就是按照顺序排放的树)。 p.s...
不知不觉commit了很多东西,今天要来面对的,就是这些纪录的修改。 这个篇章大概会分成两个部分: ...
本篇文章同步发表在 HKT 线上教室 部落格,线上影音教学课程已上架至 Udemy 和 Youtu...
事前准备 安装好 VS Code。 拥有 GitHub帐号,并开好一个练习用的 repository...
前言 承接昨天的内容,今天来开工啦 学习资源 这篇的资源参考与上一周同样教学团队的real-time...