我正在尝试解决USACO 培训,“你的旅程在这里”问题可以用这个算法来解决:
#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;
}
我试图用do-while循环更改for循环,这是我的代码,所以我用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