第一部分 C++语言
第四章 循环结构的程序设计
第二节 while语句
1085 球弹跳高度的计算
#include <cstdio>
using namespace std;
int main(){
double h;
scanf("%lf", &h);
double ttl = h;
int n = 1;
while(n < 10){
n++;
h /= 2;
ttl += h*2;
}
printf("%g\n%g\n", ttl, h/2);
return 0;
}
1086 角谷猜想
#include <cstdio>
using namespace std;
int main() {
int n;
scanf("%d", &n);
while (n != 1) {
if ( n%2 == 0) {
printf("%d/2=%d\n", n, n/2);
n /= 2;
}
else{
printf("%d*3+1=%d\n", n, n*3+1);
n = n * 3 + 1;
}
}
printf("End\n");
return 0;
}
1087 级数求和
#include <cstdio>
using namespace std;
int main() {
int k, n = 0;
double sum = 0;
scanf("%d", &k);
while (sum <= k) {
n++;
sum += 1.0 / n;
}
printf("%d\n", n);
return 0;
}
1088 分离整数的各个数
#include <cstdio>
using namespace std;
int main() {
int n;
scanf("%d", &n);
while (n) {
printf("%d ", n%10);
n /= 10;
}
return 0;
}