私はUSACOトレーニングを解決しようとしています。「YourRideIsHere」の問題はこのアルゴリズムで解決できます。
#include <iostream>
#include <conio.h>
using namespace std;
int Calculated(char * calc_me);
int main() {
char * comet_name = (char*)calloc(sizeof(char), 7);
if (comet_name == NULL) {return 0;}
char * group_name = (char*)calloc(sizeof(char), 7);
if (group_name == NULL) {free(comet_name); return 0;}
cout << "Enter the name of the comet: ";
cin >> comet_name;
cout << "Enter the name of the group: ";
cin >> group_name;
if ((Calculated(comet_name) % 47) == (Calculated(group_name) % 47)) {
cout << "GO";
}
else {
cout << "STAY";
}
free (group_name);
free (comet_name);
return 0;
}
int Calculated (char * calc_me) {
int i;
int total = 1;
for (i = 0; i < 7; i++) {
if (calc_me[i] == '0') {break;}
total *= calc_me[i] - 64;
}
getch();
return total;
}
forループをdo-whileループで変更しようとしています。これが私のコードなので、do-whileに置き換えましたが、機能しません。誰かが私が間違っている部分を教えてくれますか?
#include <iostream>
#include <conio.h>
using namespace std;
int Calculated(char * calc_me);
int main() {
char * comet_name = (char*)calloc(sizeof(char), 7);
if (comet_name == NULL) {return 0;}
char * group_name = (char*)calloc(sizeof(char), 7);
if (group_name == NULL) {free(comet_name); return 0;}
cout << "Enter the name of the comet: ";
cin >> comet_name;
cout << "Enter the name of the group: ";
cin >> group_name;
if ((Calculated(comet_name) % 47) == (Calculated(group_name) % 47)) {
cout << "GO";
}
else {
cout << "STAY";
}
free (group_name);
free (comet_name);
return 0;
}
int Calculated (char * calc_me) {
int i;
int total = 0;
do
{
total *= calc_me[i] - 64;
i += 1;
}while(i < 7);
getch();
return total;
}
これはサンプル入力です:COMETQ HVNGAT
GO
ABSTAR USACO
STAY