while循环练习
2025-10-05 12:09:37
发布于:江苏
7阅读
0回复
0点赞
一、上节课作业部分
#include <iostream>
using namespace std;
int main() {
int x,y;
cin>>x>>y;
if(x>y){
for(int i=x;i>=y;i--){
if(i%2==0){
cout<<i<<" ";
}
}
}else{
for(int i=x;i<=y;i++){
if(i%2==0){
cout<<i<<" ";
}
}
}
return 0;
}
二、课堂程序
- 如意金箍棒1
#include <bits/stdc++.h>
using namespace std;
int main(){
int a, b, cnt = 0;
cin >> a >> b;
while (a<b){
a*=2;
cnt++;
}
cout << cnt;
return 0;
}
- 位数求和
#include <bits/stdc++.h>
using namespace std;
int main(){
int n, s = 0;
cin >> n;
// for (;n;){
// cout << n%10 << endl;
// n /= 10;
// }
while (n){
s += n%10;
n /= 10;
}
cout << s;
return 0;
}
- 如意金箍棒2
#include <bits/stdc++.h>
using namespace std;
int main(){
int a, b, cnt = 0;
cin >> a >> b;
while (a>=b){
a/=2;
cnt++;
}
cout << cnt;
return 0;
}
三、作业提示
这里空空如也
有帮助,赞一个