教授は最初の完全な正方形を印刷し、最後の2桁が奇妙であるプログラムを作成するよう依頼しました。以下は私がこの問題を試してみたものですが、それを動作させるにはさまざまな方法を試しましたが、役に立たないものです。私は周りを検索し、同様の割り当てを持つ他の誰かを見つけることができないようです。これに関する助けがあれば大いに感謝します。最後の2桁が奇数である最初の完全な正方形が印刷されない理由を理解できません
// Project3.cpp : main project file.
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
int main() {
cout << "This program will attempt to find the first perfect square with the last two digits of the square being odd...\n";
int i = 10; //1. initialize "i" to 10 since square of 1 to 9 are already known to not have 2 odd last digits.
int a = i*i; //2. initialize "a" equal to the square of "i".
int b = a % 100; //3. initialize "b" to be the remainder of "a" divided by 100 (modulus operator).
int c = b/10; //4. initialize "c" to be b divided by 10, which gives us the "tens" place.
int d = a % 10; //5. initialize "d" to be the remainder of "a" divided by 10, giving us the "ones" place.
while (((b>10) && (b%2 != 0) && (c%2 != 0) && (d%2 != 0) && ((i*i)%i ==0)) != true){
if ((b>10) && (b%2 != 0) && (c%2 != 0) && (d%2 != 0) && ((i*i)%i ==0)) {
cout << "The first perfect square whose last two digits are odd is " << a << ".\n";
system("PAUSE");
return 0;
}
else {
++i;}
}
system("PAUSE");
return 0;
}
おかげで、あなたのコードは一度だけ実行されている デイブ
すべての行にコメントするための+1 – milancurcic