亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

c++素數問題

c++素數問題

C++
the__sky123 2017-12-29 11:44:50
#include <iostream>#include <cmath>using namespace std;int main(){ int i,j; cin>>i; bool ch;? for(j=2;j<=sqrt(i);j++) { if(i%j==0) ch=true; ? //真 不是素數? if(i%j!=0) ch=false; } if(ch) ? /*******/ cout<<"不是素數"<<endl; else cout<<"是素數"<<endl; }在/***/處,若改為if(ch=true)不能正常輸出,但是if(ch==false)可以正常輸出,為什么????
查看完整描述

2 回答

已采納
?
慕用4063026

TA貢獻195條經驗 獲得超115個贊

你自己用了bool聲明,if是判斷語句,ch=true是個賦值語句,當然不行了,要==

#include?<iostream>
#include?<cmath>
using?namespace?std;
int?main()
{
int?i,j;
cin>>i;
bool?ch;?
for(j=2;j<=sqrt(i);j++)
{
if(i%j==0)
ch=true;???//真?不是素數?
if(i%j!=0)
ch=false;
}
if(ch==true)???/*這里要判斷*/
cout<<"不是素數"<<endl;
else
cout<<"是素數"<<endl;
}


查看完整回答
1 反對 回復 2017-12-30
?
onemoo

TA貢獻883條經驗 獲得超454個贊

你還沒發現你寫的一個是“=”,一個是“==”嗎?

現在明白了嗎?

查看完整回答
1 反對 回復 2017-12-29
  • 2 回答
  • 0 關注
  • 1310 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號