第一题:
#include <iostream>
using namespace std;
void main()
{
int iTurtle = 0; // Turtle distance
int iRabbit = 0; // Rabbit distance
int iTime = 0; // Input time
// Input the time
cout << "Please input the time: ";
cin >> iTime;
if (iTime == 0)
{
cout << "Time cannot be equal to 0" << endl;
exit(0);
}
cout << endl;
int i = 0;
while (i < iTime)
{
iTurtle += 3;
iRabbit += 9;
if (iRabbit % 10 == 0)
{
iTurtle += 30 * 3;
i += 30;
}
i++;
}
// Output
if (iTurtle>iRabbit)
cout << "@_@" << iTurtle << endl;
else if (iRabbit > iTurtle)
cout << "^_^" << iRabbit << endl;
else
cout << "-__-" << iRabbit << endl;
cin.ignore();
}
第二题
#include <iostream>
using namespace std;
void main()
{
int iInteger = 0;
int i1, i2, i3, i4; // 个十百千
i1 = i2 = i3 = i4 = 0;
cout << "Please input the number: ";
cin >> iInteger;
if (iInteger > 9999 || iInteger < 1000)
{
cout << "ERROR! Invalid input" << endl;
}
cout << endl;
// Get the number of each position
i4 = iInteger / 1000;
i3 = (iInteger - i4 * 1000) / 100;
i2 = (iInteger - i3 * 100 - i4 * 1000) / 10;
i1 = (iInteger - i2 * 10 - i3 * 100 - i4 * 1000);
// Encrypt
i4 = (i4 + 9) % 10;
i3 = (i3 + 9) % 10;
i2 = (i2 + 9) % 10;
i1 = (i1 + 9) % 10;
int temp;
temp = i4;
i4 = i2;
i2 = temp;
temp = i3;
i3 = i1;
i1 = temp;
cout << "The encrypted number is " << i4 * 1000 + i3 * 100 + i2 * 10 + i1 << endl;
}
第三题:
int str_to_int(char *str, int &result)
{
int temp;
if (atoi(str) != 0)
{
// 8进制
if (str[0] == '0')
{
result = strtol(str, NULL, 8);
}
// 16进制
else if (str[0] == '0' && str[1] == 'x')
{
result = strtol(str, NULL, 16);
}
else
{
result = atoi(str);
}
return 1;
}
else
{
return 0;
}
}
请采纳,谢谢。