您的位置:首页 > 编程语言 > C语言/C++

HDU 水题十道,慢慢品味

2016-12-04 00:29 344 查看
转载自:提高鸟语能力~~

1 hdu 1032

[cpp] view
plain copy

print?

#include <iostream>

using namespace std;

int len(int n)

{

int s = 1;

while (n!=1)

{

if (n&1) n = 3*n +1;

else n /= 2;

s++;

}

return s;

}

int main()

{

int i, j, n, l, m;

while (~scanf("%d %d", &i, &j))

{

printf("%d %d", i, j);

if (i>j) swap(i, j);

m = 0;

for (;i<=j;++i)

{

l = len(i);

if (m<l) m = l;

}

printf(" %d\n", m);

}

return 0;

}

2 hdu 1029 (鸽巢原理)

[cpp] view
plain copy

print?

#include <iostream>

using namespace std;

int main()

{

int ans, n, c, num;

while (~scanf("%d", &n))

{

c = 0;

for (int i=1;i<=n;++i)

{

scanf("%d", &ans);

if (c==0)

{

num = ans;

c ++;

}

else if (ans == num) c ++;

else c --;

}

printf("%d\n", num);

}

return 0;

}

3 hdu 1033 题目看到蛋碎

[cpp] view
plain copy

print?

#include <iostream>

using namespace std;

int f[4][2] = {0,10,10,0,0,-10,-10,0};

void move(int &x, int &y, char c, int &s)

{

int u;

switch (s)

{

case 0: if (c=='V') u = 0, s = 1; else u = 2, s = 3; break;

case 1: if (c=='V') u = 3, s = 2; else u = 1, s = 0; break;

case 2: if (c=='V') u = 2, s = 3; else u = 0, s = 1; break;

case 3: if (c=='V') u = 1, s = 0; else u = 3, s = 2; break;

}

x += f[u][0];

y += f[u][1];

}

int main()

{

char c[300];

int i, k;

while (~scanf("%s", c))

{

k = strlen(c);

printf("300 420 moveto\n310 420 lineto\n");

int x = 310, y = 420, s = 0;

for (i=0;i<k;++i)

{

move(x, y, c[i], s);

printf("%d %d lineto\n", x, y);

}

printf("stroke\nshowpage\n");

}

return 0;

}

4 hdu 1036 sscanf的用法

[cpp] view
plain copy

print?

#include <iostream>

using namespace std;

int main()

{

int f, n, i, a, sum, h, m, s;

double d;

char c[20], t[100];

scanf("%d%lf", &n, &d);

while (~scanf("%d", &a))

{

sum = f = 0;

for (i=1;i<=n;++i)

{

scanf("%s", c);

if (c[0] == '-') f = 1;

sscanf(c, "%d:%d:%d", &h, &m, &s);

sum += h*3600 + m*60 + s;

}

if (f) printf("%3d: -\n", a);

else

{

sum = int(sum/d + 0.5);

printf("%3d: %d:%2.2d min/km\n", a, sum/60, sum%60);

}

}

return 0;

}

5 hdu 1037 不解释

[cpp] view
plain copy

print?

#include <iostream>

using namespace std;

int main()

{

int h1, h2, h3;

while (~scanf("%d %d %d", &h1, &h2, &h3))

{

if (h1>=168 && h2>=168 && h3>=168)

printf("NO CRASH\n");

else

{

printf("CRASH ");

if (h1<168)

{

printf("%d\n",h1);

break;

}

if (h2<168)

{

printf("%d\n",h2);

break;

}

if (h3<168)

{

printf("%d\n",h3);

break;

}

}

}

return 0;

}

6 hdu 1038

[cpp] view
plain copy

print?

#include <stdio.h>

#define P 3.1415926

int main()

{

double diameter, revolutions, time;

double sum, zong;

int n = 0;

while(~scanf("%lf%lf%lf",&diameter,&revolutions,&time))

{

if (revolutions==0) break;

n++;

sum = 0;

zong = 0;

sum = diameter / 12 / 5280 * P * revolutions * time;

zong = sum/time;

sum = zong / time * 60 * 60;

printf("Trip #%d: %.2lf %.2lf\n",n,zong,sum);

}

return 0;

}

7 hdu 1039

[cpp] view
plain copy

print?

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <assert.h>

#define MAX_LEN 1111

char psd[MAX_LEN];

const char end[] = "end";

const char s1[] = "aeiou";

int c1(void)

{

int i,j;

for(i=0;psd[i]!='\0';i++)

for(j=0;s1[j]!='\0';j++)

if(psd[i]==s1[j])

return 1;

return 0;

}

int c2(void)

{

int i,j,cnt1,cnt2,tag;

cnt1=cnt2=0;

for(i=0;psd[i]!='\0';i++)

{

for(j=0,tag=0;s1[j]!='\0';j++)

if(psd[i]==s1[j])

{

cnt1++;

cnt2=0;

tag=1;

break;

}

if(tag==0)

{

cnt1=0;

cnt2++;

}

if(cnt1>=3 || cnt2>=3)

return 0;

}

return 1;

}

int c3(void)

{

int i;

for(i=1;psd[i]!='\0';i++)

if(psd[i]==psd[i-1])

if(psd[i]!='e' && psd[i]!='o')

return 0;

return 1;

}

int main(void)

{

#ifndef ONLINE_JUDGE

assert(freopen("1039.in","r",stdin));

#endif

while(scanf("%s",psd),strcmp(psd,end))

{

if(c1() && c2() && c3())

printf("<%s> is acceptable.\n",psd);

else

printf("<%s> is not acceptable.\n",psd);

}

return 0;

}

8 hdu 1047

大正整数加。

脑抽用string。。。越写越丑。。。还好一遍a了,避免了重敲的宿命。。

[cpp] view
plain copy

print?

#include <iostream>

#include <string>

#include <vector>

#include <algorithm>

#define clr(x, k) memset((x), (k), sizeof(x))

const int N = 1000;

using namespace std;

//char a
, x
;

string x, a;

int l;

void re(string &x)

{

int i = 0;

int j = x.length() -1;

while (i<j)

swap(x[i++], x[j--]);

}

void add(string &a, string &x)

{

int i, j, ma, mi;

ma = a.length();

mi = x.length();

if (ma<mi)

{

swap(ma, mi);

swap(a, x);

}

for (i=0;i<mi;++i) a[i] += x[i]-'0';

for (i=0;i<ma;++i)

if (a[i]>'9')

{

if (i+1<ma) a[i+1] += (a[i]-'0')/10;

else a.insert(i+1,1,(a[i]-'0')/10+'0');

a[i] -= 10;

}

}

int main()

{

int n;

scanf("%d", &n);

while (n--)

{

l = 1;

a = "0";

while (cin>>x)

{

re(x);

if (x == "0")

{

re(a);

cout<<a<<endl;

if (n!=0) cout<<endl;

break;

}

add(a, x);

}

}

return 0;

}

9 hdu 1856 ans初值为1,wa了几次才找到。。。

[cpp] view
plain copy

print?

#include <iostream>

using namespace std;

const int N = 10000002;

int n, m;

int f
, r
;

int find(int x)

{

if (f[x]!=x) f[x] = find(f[x]);

return f[x];

}

int main()

{

int x, y, i, ans;

while (scanf("%d", &n)!=EOF)

{

for (i=0;i<=N;++i)

{

f[i] = i;

r[i] = 1;

}

ans = 1;

for (i=0;i<n;++i)

{

scanf("%d %d", &x, &y);

x = find(x);

y = find(y);

if (x>y) swap(x, y);

if (x!=y)

{

f[y] = x;

r[x] += r[y];

if (r[x]>ans) ans = r[x];

}

}

printf("%d\n", ans);

}

return 0;

}

10 hdu 1060

题目大意是输入N,求N^N的最高位数字。1<=N<=1,000,000,000

N^N = a*10^x;

我们要求的最右边的数字就是(int)a,即a的整数部分

两边同时取以10为底的对数 lg(N^N) = lg(a*10^x) ;

N*lg(N) = lg(a) + x;

N*lg(N) - x = lg(a)

a = 10^(N*lg(N) - x);

现在就只有x是未知的了,如果能用n来表示x的话,这题就解出来了。

又因为,x是N^N的位数-1。比如 N^N = 1200 ==> x = 3;

实际上就是 x 就是lg(N^N) 向下取整数,表示为[lg(N^N)]

a = 10^(N*lg(N) – [lg(N^N)]);

然后(int)a 就是答案了。

[cpp] view
plain copy

print?

#include<iostream>

#include<cstring>

#include<cmath>

using namespace std;

int main(){

int t;

__int64 ans,n;

double m;

scanf("%d",&t);

while(t--){

scanf("%I64d",&n);

m=n*log10(n+0.0);

m-=(__int64)m;

ans=pow((double)10,m);

printf("%I64d\n",ans);

}

return 0;

}

11 hdu 1061 N^N%10

[cpp] view
plain copy

print?

#include <cstdio>

#include <cstdlib>

#define I64 __int64

int Fuction(I64 n)

{

int res = 1;

I64 b = n;

if(!n) // n==0, 输出1

return 1;

if(!(n % 10))

return 0;

while(b)

{

if(b & 1)

{

res *= n;

res %= 10; // 要取模,否则溢出

}

n *= n;

n %= 10; // 要取模,否则溢出

b >>= 1;

}

return res % 10;

}

int main()

{

int t;

I64 n;

scanf("%d", &t);

while(t--)

{

scanf("%I64d", &n);

printf("%d\n", Fuction(n));

}

return 0;

}

//题目分析:求n^n的个位数,只要根据每一个数的幂的周期性规律,就行了

#include <stdio.h>

int main()

{

int m,n,a;

int s[10][4]={{0},{1},{6,2,4,8},{1,3,9,7},{6,4},{5},{6},

{1,7,9,3},{6,8,4,2},{1,9}};

scanf("%d",&m);

while(m--)

{

scanf("%d",&n);

a=n % 10;

if(a==0||a==1||a==5||a==6)

printf("%d\n",a);

else if(a==4||a==9)

printf("%d\n",s[a][n%2]);

else if(a==2||a==3||a==7||a==8)

printf("%d\n",s[a][n%4]);

}

return 0;

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  函数 算法 c语言 cpp