设计函数fun,判断一对整数中第二个整数是否为第一个整数的倍数。在主函数中输出一系列整数的,

设计函数fun,判断一对整数中第二个整数是否为第一个整数的倍数。在主函数中输出一系列整数的,并将将倍数对的数对输出。

#include <iostream>

using namespace std;
int fun(int n,int m){//判断m是不是n的倍数
  return !(m%n); //1.m%n=0,说明m能被n整除,对零做非运算即返回[1],m是n的倍数。
                  //2.m%n≠0,不能被整除。%运算结果非零,做非运算后,返回值是[0],不是倍数
}
int main()
{
    int n,m;
    while(cin>>n>>m){//循环输入,ctrl+z结束循环
       if(fun(n,m))//是倍数
          cout<<n<<","<<m<<", yes"<<endl;
        else     //不是倍数
          cout<<n<<","<<m<<", no"<<endl;
    }
    return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2021-04-21
int fun(int n,int m){//判断m是不是n的倍数 return !(m%n); //1.m%n=0,说明m能被n整除,对零做非运算即返回[1],m是n的倍数。 //2.m%n≠0,不能被整除。%运算结果非零,做非运算后,返回值是[0],不是倍数}int main(){ int n,m; while(cin>>n>>m){//循环输入,ctrl+z结束循环 if(fun(n,m))//是倍数 cout<<n<<","<<m<<", yes"<<endl; else //不是倍数 cout<<n<<","<<m<<", no"<<endl; } return 0;}
相似回答