標題:
誰的因數多
[打印本頁]
作者:
may
時間:
2023-8-4 12:13
標題:
誰的因數多
請輸入兩個正整數,比較誰的因數多?
[attach]16175[/attach]
作者:
may
時間:
2023-8-4 12:14
回復
1#
may
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
re:
int x1,x2, sum1=0,sum2=0;
cout<<"請輸入2個正整數: ";
cin>>x1>>x2;
cout<<x1<<"的因數有: ";
for(int i=1; i<=x1; i++)
{
if(x1%i==0)
{
cout<<i<<" ";
sum1++;
}
}
cout<<endl<<x2<<"的因數有: ";
for(int i=1; i<=x2; i++)
{
if(x2%i==0)
{
cout<<i<<" ";
sum2++;
}
}
cout<<endl<<x1<<"總共有"<<sum1<<"個因數!";
cout<<endl<<x2<<"總共有"<<sum2<<"個因數!"<<endl;
if(sum1>sum2)
cout<<x1<<"的因數比"<<x2<<"多"<<sum1-sum2<<"個"<<endl<<endl;
else if(sum2>sum1)
cout<<x2<<"的因數比"<<x1<<"多"<<sum2-sum1<<"個"<<endl<<endl;
else
cout<<x1<<"的因數和"<<x2<<"的因數,一樣多。"<<endl<<endl;
goto re;
return 0;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2