跳到正文

个人记录

while循环

obbycode约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年10月4日
收录
2026年10月4日
更新
2026年10月4日
篇幅
约 2 分钟

版本

  1. v1当前

分离整数的各个数

C++
#include<iostream>
using namespace std;
int main()
{
    int n;
    cin>>n;
    while(n)// 非0即真 真→执行大括号里面的语句
    {
        cout<<n%10<<" ";
        n=n/10;
    }
}

含 k 个 3 的数

C++
#include<iostream>
using namespace std;
int main()
{
    long long m,k,s=0;//s统计m有多少个3
    cin>>m>>k;
    while(m)
    {
        if(m%10==3)
            s++;
        m=m/10;
    }
    if(s==k) cout<<"YES";
    else cout<<"NO";
}

求平均年龄

C++
#include<iostream>
using namespace std;
int main()
{
    int n,x,sum=0;//初始值为0,随机分配初始值
    cin>>n;
    int tem=n;//总学生人数
    while(n--)//非0即真
    {
        cin>>x;//每输入一格学学生的年龄
        sum=sum+x;//加到总年龄
    }
    printf("%.2f",1.0*sum/tem);//1.0*sum换成小数 
}

整数的个数

C++
#include<iostream>
using namespace std;
int main()
{
    int k,x,a=0,b=0,c=0;//a 1 b 5 c 10
    cin>>k;
    while(k--)//非0即真 真执行大括号语句
    {
        cin>>x;//每输入一个数字
        if(x==1) a++;//数字是否为1
        else if(x==5) b++;//数字是否为5
        else if(x==10) c++;//数字是否为10
    }
    cout<<a<<endl<<b<<endl<<c;
}
原文