1、UVa 10161 Ant on a Chessboard(棋盘上的蚂蚁)
#include <iostream>
#include <cmath>
using namespace std;
int IsSquares(int n);
int main()
{
int N;
while (cin>>N && N!=0) {
int i = N;
while (!IsSquares(i)) {
++i;
}
int x, y;
int Sqrti = sqrt(i);
if (Sqrti%2 == 0) {
if (N-(Sqrti-1)*(Sqrti-1) <= Sqrti) {
x = N - (Sqrti-1)*(Sqrti-1);
y = Sqrti;
} else {
x = Sqrti;
y = i + 1 - N;
}
} else {
if (N-(Sqrti-1)*(Sqrti-1) <= Sqrti) {
x = Sqrti;
y = N-(Sqrti-1)*(Sqrti-1);
} else {
x = i + 1 - N;
y = Sqrti;
}
}
cout<<x<<" "<<y<<endl;
}
return 0;
}
int IsSquares(int n)
{
if (floor(sqrt(n)) == sqrt(n)) {
return 1;
} else {
return 0;
}
}
2、UVa 253 Cube painting(立方体着色)
#include <iostream>
using namespace std;
int main()
{
char s[20];
char color1[10], color2[10];
while (cin>>s) {
int i, j;
int a1, a2, a3, b1, b2, b3;
for (j=0,i=1; i<=6; ++i,++j) {
color1[i] = s[j];
// cout<<color1[i];
}
// cout<<endl;
a1 = color1[1] + color1[6];
a2 = color1[2] + color1[5];
a3 = color1[3] + color1[4];
// cout<<a1<<" "<<a2<<" "<<a3<<endl;
for (; i<=12; ++i,++j) {
color2[i-6] = s[j];
// cout<<color2[i-6];
}
// cout<<endl;
b1 = color2[1] + color2[6];
b2 = color2[2] + color2[5];
b3 = color2[3] + color2[4];
// cout<<b1<<" "<<b2<<" "<<b3<<endl;
int flag = 0;
if (a1==b1 && a2==b2 && a3==b3) {
flag = 1;
} else if (a1==b1 && a2==b3 && a3==b2) {
flag = 1;
} else if (a1==b2 && a2==b1 && a3==b3) {
flag = 1;
} else if (a1==b2 && a2==b3 && a3==b1) {
flag = 1;
} else if (a1==b3 && a2==b1 && a3==b2) {
flag = 1;
} else if (a1==b3 && a2==b2 && a3==b1) {
flag = 1;
}
if (flag) {
cout<<"TRUE"<<endl;
} else {
cout<<"FALSE"<<endl;
}
}
return 0;
}
3、UVa 10025 The ? 1 ? 2 ? … ? n = k problem
#include <iostream>
using namespace std;
int main()
{
int T;
while (cin>>T) {
int k;
while (T--) {
cin>>k;
if (k < 0) {
k = -k;
}
int i = 1;
int count = 0;
while (1) {
++count;
if (k-i == 0) {
break;
}
if (k-i < 0) {
if ((i-k)%2 == 0) {
break;
} else {
if (i%2 == 0) {
count += 1;
break;
} else {
count += 2;
break;
}
}
}
k -= i;
++i;
}
cout<<count<<endl;
if (T > 0) {
cout<<endl;
}
}
}
return 0;
}
4、UVa 573 The Snail(蜗牛)
题意:井的高度为H,蜗牛白天可以爬的高度为U,晚上滑落的高度为D,疲劳系数为F%,开始的时候蜗牛在井底,输出它在第几天会成功或失败
解题思路:
(1)、蜗牛第n个白天爬的高度为U-F%(n-1)(注意U-F%(n-1)<=0时,蜗牛白天会在原地不动),每天下滑高度为D
(2)、某一天上爬后的高度>H则成功,下滑后的高度<0则失败
#include <iostream>
using namespace std;
int main()
{
int H, U, D, F;
while (cin>>H>>U>>D>>F && H!=0) {
int day = 1;
double Init_H = 0, Dis_Climb = U, H_After_Climb = U, H_After_Slid = U-D;
// cout<<Init_H<<" "<<Dis_Climb<<" "<<H_After_Climb<<" "<<H_After_Slid<<endl;
while ((H_After_Climb<=H) && (H_After_Slid>=0)) {
Init_H = H_After_Slid;
if (Dis_Climb-U*F/100.0 > 0) {
Dis_Climb -= (U*F/100.0);
} else {
Dis_Climb = 0;
}
H_After_Climb = Init_H + Dis_Climb;
H_After_Slid = H_After_Climb - D;
++day;
// cout<<Init_H<<" "<<Dis_Climb<<" "<<H_After_Climb<<" "<<H_After_Slid<<endl;
}
if (H_After_Climb > H) {
cout<<"success on day "<<day<<endl;
} else if (H_After_Slid < 0) {
cout<<"failure on day "<<day<<endl;
}
}
return 0;
}
5、UVa 846 Steps(步数)
#include <iostream>
using namespace std;
int main()
{
int n;
while (cin>>n) {
unsigned long long x, y;
while (n--) {
cin>>x>>y;
int sum = y - x;
int i = 2;
int steps = 0;
while (sum) {
if (sum-i < 0) {
if (sum-(i/2) > 0) {
steps += 2;
break;
} else {
steps += 1;
break;
}
}
sum -= i;
steps += 2;
i += 2;
}
cout<<steps<<endl;
}
}
return 0;
}