#include<iostream>
using namespace std;
int main(){
return 0;
}
#include<cstdlib>
#include<cstdio>
using namespace std;
int main(){
return 0;
}
Hdu1001
#include<iostream>
usingnamespace std;
intmain(){
int n;
int res=0;
while(EOF!=scanf("%d",&n)){
res=0;
if(n%2==0)res=(n+1)*(n/2);
else res=(n+1LL)/2*n;
printf("%d\n\n",res);
}
return 0;
}
因为最后输出是32bite,所以在n*(n+1)的时候到边界值还没除二就开始溢出。所以利用本题特殊条件n与n+1之间必将有一个为二整除的数字。
或者——
#include<iostream>
usingnamespace std;
intmain(){
int n;
int res=0;
while(EOF!=scanf("%d",&n)){
res=0;
res=(n+1LL)*n/2;
printf("%d\n\n",res);
}
return 0;
}
在数字后加上后缀可以转型。如:2LL5int 不论大小写。
最小公倍数解法一:
#include<iostream>
using namespace std;
int cmp(int x,int y){
int t=x;
x=y;
y=t;
}
int main(){
int n,m;
int res;
while(EOF!=scanf("%d %d",&n,&m)){
if(n>m)cmp(n,m);
for(inti=1;i;i++){
res=m*i;
if(res%n==0){
printf("%d\n",res);
break;
}
}
}
return 0;
}
辗转相除法版本:
最小公约数用辗转相除求得原理:
Lcm=a*b/gcd
#include<iostream>
using namespace std;
int cmp(int &x,int &y){
int t=x;
x=y;
y=t;
}
int gcd( int x ,int y ){
while(y){
int t=x%y;
x=y;
y=t;
}
return x;
}
int lcm(int x , int y , int z){
return x*y/z;
}
int main(){
int n,m;
int res1,res2;
while(EOF!=scanf("%d %d",&n,&m )){
if(n<m)cmp(n,m);
res1=gcd(n,m);
res2=lcm(n,m,res1);
printf("%d\n",res2);
}
return 0;
}
Hdu 2035 人见人爱a^b 仅仅保留最后三位数
#include<iostream>
using namespace std;
int main(){
int a,b;
while(scanf("%d %d",&a,&b)&&a!=0,b!=0){
int res=1;
for(inti=0;i<b;i++){
res*=a;
res%=1000;
}
printf("%d\n",res);
}
return 0;
}
Hdu 1061 RightmostDigit
#include<iostream>
using namespace std;
int main(){
int num;
long long m;
scanf("%d",&num);
while(num!=0&&scanf("%I64d",&m)!=EOF){
int n=m%10;
int t=n;
int pan;
intflog=0;
int res=1;
if(n*n==n)res=n;
for(inti=1;i;i++){
t*=n;
t%=10;
if(t==n){
flog=i;
break;
}
}
if(m%flog==0)pan=flog;
elsepan=m%flog;
for(intj=0;j<pan;j++){
res*=n;
res%=10;
}
printf("%d\n",res);
num--;
}
return 0;
}
归并排序:
#include<cstdio>
#include<cstdlib>
#define N 100000
using namespace std;
int a[N];
int c[N];
int m,k;
void merge(int l,int r){
int flog=l;
int mid=(l+r)/2;
k=0;
intnum1=l,num2=mid+1;
while(num1<=mid&&num2<=r){
if(a[num1]>a[num2])c[k++]=a[num2++];
elsec[k++]=a[num1++];
if(num1==num2&&num2==r)c[k++]=a[num2++];
}
while(num2>=r&&num1<=mid)c[k++]=a[num1++];
while(num1>=mid&&num2<=r)c[k++]=a[num2++];
for(inti=0;i<k;i++)
a[flog++]=c[i];
}
void msor(int l,int r){
m=(l+r)/2;
if(l==r)return ;
msor(l,m);
msor(m+1,r);
merge(l,r);
}
int main(){
int n;
int numb;
scanf("%d",&numb);
for(inti=0;i<numb;i++){
scanf("%d",&n);
for(int i =0; i < n ;i++)scanf("%d",&a[i]);
msor(0,n-1);
for(int i =0; i < n; i++){
printf("%d",a[i]);
if(i<n-1)printf(" ");
}
printf("\n");
}
//while(1);
return 0;
} Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE /* Style Definitions */ table.MsoNormalTable{mso-style-name:普通表格;mso-tstyle-rowband-size:0;mso-tstyle-colband-size:0;mso-style-noshow:yes;mso-style-priority:99;mso-style-parent:"";mso-padding-alt:0cm 5.4pt 0cm 5.4pt;mso-para-margin:0cm;mso-para-margin-bottom:.0001pt;mso-pagination:widow-orphan;font-size:10.5pt;mso-bidi-font-size:11.0pt;font-family:"Calibri","sans-serif";mso-ascii-font-family:Calibri;mso-ascii-theme-font:minor-latin;mso-hansi-font-family:Calibri;mso-hansi-theme-font:minor-latin;mso-bidi-font-family:"Times New Roman";mso-bidi-theme-font:minor-bidi;mso-font-kerning:1.0pt;}I