求m的n次方的最后一位,这题挺好
Code
#include <iostream>
using namespace std;
int main()
{
int m,n,i,sum,flag=1;
char a[2000],b[2000];
while(cin>>a>>b){
m=a[strlen(a)-1]-'0';
if(strlen(b)>1)
n=(b[strlen(b)-2]-'0')*10+b[strlen(b)-1]-'0';
else
{n=b[strlen(b)-1]-'0';flag=0;}
if(m==0&&flag==0&&n==0)
break;
n%=4;
if(flag==0&&n==0)
sum=1;
else
{
sum=m;
for(i=0;i<n+3;i++)
{
sum*=m;
sum%=10;
}
}
cout <<sum<<endl;
flag=1;
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int m,n,i,sum,flag=1;
char a[2000],b[2000];
while(cin>>a>>b){
m=a[strlen(a)-1]-'0';
if(strlen(b)>1)
n=(b[strlen(b)-2]-'0')*10+b[strlen(b)-1]-'0';
else
{n=b[strlen(b)-1]-'0';flag=0;}
if(m==0&&flag==0&&n==0)
break;
n%=4;
if(flag==0&&n==0)
sum=1;
else
{
sum=m;
for(i=0;i<n+3;i++)
{
sum*=m;
sum%=10;
}
}
cout <<sum<<endl;
flag=1;
}
return 0;
}
Code
#include <iostream>
using namespace std;
int main()
{
int test,n;
cin>>test;
while(test--)
{
cin>>n;
cout<<n*n-(n-1)<<' '<<n*n+(n-1)<<endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int test,n;
cin>>test;
while(test--)
{
cin>>n;
cout<<n*n-(n-1)<<' '<<n*n+(n-1)<<endl;
}
return 0;
}
TOJ 1380. A multiplication game
找规律
Code
# include <iostream>
# include <cmath>
using namespace std;
typedef unsigned int ULI;
bool stanWin(ULI n)
{
if (2 <= n && n <= 9) {
return true;
} else if (n < 2) {
return false;
} else {
return stanWin(ceil(n / 18.0));
}
}
int main()
{
ULI n;
while (cin >> n) {
if (stanWin(n)) {
cout << "Stan wins." << endl;
} else {
cout << "Ollie wins." << endl;
}
}
return 0;
}
# include <iostream>
# include <cmath>
using namespace std;
typedef unsigned int ULI;
bool stanWin(ULI n)
{
if (2 <= n && n <= 9) {
return true;
} else if (n < 2) {
return false;
} else {
return stanWin(ceil(n / 18.0));
}
}
int main()
{
ULI n;
while (cin >> n) {
if (stanWin(n)) {
cout << "Stan wins." << endl;
} else {
cout << "Ollie wins." << endl;
}
}
return 0;
}
这规律让人无语
Code
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int test,n;
cin>>test;
while(test--)
{
cin>>n;
cout<<(int)sqrt(n)<<endl;
}
return 0;
}
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int test,n;
cin>>test;
while(test--)
{
cin>>n;
cout<<(int)sqrt(n)<<endl;
}
return 0;
}
Code
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
double b,n,d1,d2;
int i,j;
while(cin>>b>>n)
{
if(b==0&&n==0)
break;
i=floor(pow(b,1.0/n));
j=ceil(pow(b,1.0/n));
d1=pow(i,n)-b;
d2=pow(j,n)-b;
if(abs(int(d1))<abs(int(d2)))
cout<<i<<endl;
else
cout<<j<<endl;
}
return 0;
}
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
double b,n,d1,d2;
int i,j;
while(cin>>b>>n)
{
if(b==0&&n==0)
break;
i=floor(pow(b,1.0/n));
j=ceil(pow(b,1.0/n));
d1=pow(i,n)-b;
d2=pow(j,n)-b;
if(abs(int(d1))<abs(int(d2)))
cout<<i<<endl;
else
cout<<j<<endl;
}
return 0;
}
简单题
Code
#include<iostream>
using namespace std;
int main()
{
int test,k,sum,n,t;
cin>>test;
while(test--)
{
sum=0;
cin>>k;
n=k;
while(k--)
{
cin>>t;
sum+=t;
}
sum=sum-n+1;
cout<<sum<<endl;
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int test,k,sum,n,t;
cin>>test;
while(test--)
{
sum=0;
cin>>k;
n=k;
while(k--)
{
cin>>t;
sum+=t;
}
sum=sum-n+1;
cout<<sum<<endl;
}
return 0;
}
这题规律有点难
以斜列来看,列从下往上,分子分母依次递减和递增
但奇数列是从下往上数,而偶数列则相反
Code
#include <iostream>
using namespace std;
int main()
{
int n;
while (scanf("%d",&n)!=EOF)
{
int k=0,raw=n;
while (n>k)
{
n-=k;
k++;
}
if (k%2==0)
printf("TERM %d IS %d/%d\n",raw,n,k-n+1);
else
printf("TERM %d IS %d/%d\n",raw,k-n+1,n);
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int n;
while (scanf("%d",&n)!=EOF)
{
int k=0,raw=n;
while (n>k)
{
n-=k;
k++;
}
if (k%2==0)
printf("TERM %d IS %d/%d\n",raw,n,k-n+1);
else
printf("TERM %d IS %d/%d\n",raw,k-n+1,n);
}
return 0;
}
到达的时间实际就是最先到达的人的到达时间
Code
#include<iostream>
#include <math.h>
int s;
int t;
using namespace std;
int main()
{
int n;
while(cin>>n&&n!=0)
{
double time=1e18;
double sum=0;
for(int i=0;i<n;i++)
{
scanf("%d%d",&s,&t);
if(t>=0)
{
double d=ceil(4500*3.6/(double)s+(double)t);
if(d<time)
time=d;
}
}
printf("%.0lf\n",time);
}
return 0;
}
#include<iostream>
#include <math.h>
int s;
int t;
using namespace std;
int main()
{
int n;
while(cin>>n&&n!=0)
{
double time=1e18;
double sum=0;
for(int i=0;i<n;i++)
{
scanf("%d%d",&s,&t);
if(t>=0)
{
double d=ceil(4500*3.6/(double)s+(double)t);
if(d<time)
time=d;
}
}
printf("%.0lf\n",time);
}
return 0;
}
分三种情况:
绳长>=周长;绳长<=内切圆周长;绳长介于周长和内切圆周长之间
Code
#include<iostream>
#include<math.h>
#include<iomanip>
using namespace std;
const double PI=(2.0*asin(1.0));
int main()
{
double x,y,z,role,half,max;
double R,r,triple,area;
int hujk=1;
cin>>x>>y>>z>>role;
while(x+y+z+role)
{
triple=x+y+z;
half=triple*0.5;
area=sqrt(half*(half-x)*(half-y)*(half-z));
R=area*2.0/triple;
if(x+y+z<=role)
max=area;
else if(2.0*PI*R>=role)
max=role*role/(4.0*PI);
else
{
r=(triple-role)/(triple/R-2.0*PI);
max=area+PI*r*r-(r*r*area/(R*R));
}
cout<<"Case "<<hujk++<<": ";
cout<<setiosflags(ios::fixed)<<setprecision(2)<<max<<endl;
cin>>x>>y>>z>>role;
}
return 0;
}
#include<iostream>
#include<math.h>
#include<iomanip>
using namespace std;
const double PI=(2.0*asin(1.0));
int main()
{
double x,y,z,role,half,max;
double R,r,triple,area;
int hujk=1;
cin>>x>>y>>z>>role;
while(x+y+z+role)
{
triple=x+y+z;
half=triple*0.5;
area=sqrt(half*(half-x)*(half-y)*(half-z));
R=area*2.0/triple;
if(x+y+z<=role)
max=area;
else if(2.0*PI*R>=role)
max=role*role/(4.0*PI);
else
{
r=(triple-role)/(triple/R-2.0*PI);
max=area+PI*r*r-(r*r*area/(R*R));
}
cout<<"Case "<<hujk++<<": ";
cout<<setiosflags(ios::fixed)<<setprecision(2)<<max<<endl;
cin>>x>>y>>z>>role;
}
return 0;
}