hdu新手100题精选

1720

#include<stdio.h>
int main()
{
    int a,b;
    while(~scanf("%x%x",&a,&b))//%x十六进制
        printf("%d\n",a+b);//%d十进制
}

1062
题意:翻转字符串。
题解:遇到空格反向输出前面的字符串。

#include<bits/stdc++.h>
#include<string.h>
using namespace std;
char a[1010];
int main()
{
    int t,i,j;cin>>t;
    getchar();//吸收t后面的换行符(回车键)
    while(t--)
    {
        gets(a);//gets可以输入带空格的字符串,当用到gets时,需要吸收换行符,gets本身能吸收
        int l=strlen(a),last=0;
        for(i=0;i<l;i++)
        {
            if(a[i]==' ')
            {
                for(j=i-1;j>=last;j--)
                    cout<<a[j];
                cout<<" ";
                last=i+1;
            }
            else if(i==l-1)
            {
                for(j=i;j>=last;j--)
                    cout<<a[j];
            }
        }
        cout<<endl;
    }
}

2104
题意:从1出发,每次移动m步,问能否走过n个点。
题解:判断n,m是否互质,不互质有些点永远走不到,即最大公因数为1就是yes,否则不是。

#include<bits/stdc++.h>
using namespace std;
int gcd(int a,int b)
{
    return a%b==0?b:gcd(b,a%b);
}
int main()
{
    int n,m;
    while(cin>>n>>m&&n!=-1&&m!=-1)
        if(gcd(n,m)==1)cout<<"YES"<<endl;
    else cout<<"POOR Haha"<<endl;
}

1197
题意:输出1000-9999之间的二进制、十进制、十六进制各位之和相等的数。
题解:进制转换:1、n进制转十进制,权值法,权值为n。
2、十进制转n进制,短除法,除法为n。

#include<bits/stdc++.h>
using namespace std;
int t10(int x)
{
    int sum=0,xx=x;
    while(xx)
    {
        sum+=xx%10;
        xx/=10;
    }
    return sum;
}
int t12(int x)
{
    int xx=x,sum=0;
    while(xx/12)
        sum+=xx%12,xx/=12;
    sum+=xx;
    return sum;
}
int t16(int x)
{
    int xx=x,sum=0;
    while(xx/16)
        sum+=xx%16,xx/=16;
    sum+=xx;
    return sum;
}
int main()
{
    int i;
    for(i=1000;i<=9999;i++)
        if(t10(i)==t12(i)&&t10(i)==t16(i)&&t12(i)==t16(i))cout<<i<<endl;
}

2013
题解:后一天吃掉前一天的一半多一个桃子,设第n天桃子数为n个,那么这一天的前一天的桃子为2(n+1)个。

#include<bits/stdc++.h>
#include<math.h>
using namespace std;
int f[40];
int main()
{
    int n,i;
    f[1]=1;
    for(i=2;i<30;i++)
        f[i]=2*(f[i-1]+1);
    while(cin>>n)
    {
        cout<<f[n]<<endl;
    }
}

2018
题意:求第n年有几只母牛。
题解:很好的一道题目,列表找规律。母牛每4年长大生崽,4为一个周期,可将幼牛为分3种。
求得规律为f[n]=f[n-1]+f[n-3]。

#include<bits/stdc++.h>
using namespace std;
int f[60];
int main()
{
    int n,i;
    f[1]=1,f[2]=2,f[3]=3;
    for(i=4;i<55;i++)
        f[i]=f[i-1]+f[i-3];
    while(cin>>n&&n)
    {
        cout<<f[n]<<endl;
    }
}

2024
题意:判断是否为合法标识符。
题解:标识符首字符只能是字母或下划线,其余部分只能由字母、数字、下划线组成。

#include<bits/stdc++.h>
#include<string.h>
using namespace std;
char a[60];
int main()
{
    int n,i;
    while(cin>>n)
    {
        getchar();
        while(n--)
        {
            gets(a);
            int t=1,l=strlen(a);
            for(i=0;i<l;i++)
            {
                if(!i)
                {
                    if(a[i]=='_')continue;
                    if(a[i]>='A'&&a[i]<='Z')continue;
                    if(a[i]>='a'&&a[i]<='z')continue;
                    t=0;
                }
                else
                {
                    if(a[i]=='_')continue;
                    if(a[i]>='A'&&a[i]<='Z')continue;
                    if(a[i]>='a'&&a[i]<='z')continue;
                    if(a[i]>='0'&&a[i]<='9')continue;
                    t=0;
                }
            }
            t?cout<<"yes"<<endl:cout<<"no"<<endl;
        }
    }
}

2028
题解:先算前两个数的最小公倍数,然后后面的数每取一个数就算一次最小公倍数。最小公倍数=两数乘积/最大公因数。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b)
{
    return a%b==0?b:gcd(b,a%b);
}
ll lcm(ll a,ll b)
{
    return a*b/gcd(a,b);
}
int main()
{
    ll n,x;
    while(cin>>n)
    {
        ll res=1;
        while(n--)
        {
            cin>>x;
            res=lcm(res,x);
        }
        cout<<res<<endl;
     }
}

2029
回文串

#include<bits/stdc++.h>
#include<string.h>
using namespace std;
char a[1010];
int main()
{
    int n,i;
    while(cin>>n)
    {
        while(n--)
        {
            cin>>a;
            int t=1,l=strlen(a);
            for(i=0;i<l/2;i++)
            {
                if(a[i]!=a[l-1-i])
                {
                    t=0;
                    break;
                }
            }
            t?cout<<"yes"<<endl:cout<<"no"<<endl;
        }
    }
}

2030
题解:汉字内码小于0且占两个字节,一个英文字符占一个字节。

#include<bits/stdc++.h>
#include<string.h>
using namespace std;
char a[1010];
int main()
{
    int n,i;cin>>n;
    getchar();
    while(n--)
    {
        gets(a);
        int l=strlen(a),cnt=0;
        for(i=0;i<l;i++)
            if(a[i]<0)cnt++;
        cout<<cnt/2<<endl;
    }
}

2032
杨辉三角

#include<bits/stdc++.h>
using namespace std;
int f[40][40];
void iint()
{
    int i,j;
    f[1][1]=f[2][1]=f[2][2]=1;
    for(i=3;i<=30;i++)
    for(j=1;j<=30;j++)
    {
        if(j==1||j==i)f[i][j]=1;
        else f[i][j]=f[i-1][j]+f[i-1][j-1];
    }
}
int main()
{
    int n,i,j;
    iint();
    while(cin>>n)
    {
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=i;j++)
                j==1?cout<<f[i][j]:cout<<" "<<f[i][j];
            cout<<endl;
        }
        cout<<endl;
    }
}

2042
题解:逆推可得前一站的羊是后一站的2*(n-1)

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,i,num;cin>>n;
    while(n--)
    {
        int res=3;
        cin>>num;
        for(i=1;i<=num;i++)
            res=2*(res-1);
        cout<<res<<endl;
    }
}

2072统计单词

#include<bits/stdc++.h>
#include<string.h>
#include<map>
using namespace std;
char a[1010];
int main()
{
    while(gets(a)&&a[0]!='#')
    {
        int cnt=0;
        map<string,int>mp;
        string str="";
        int i,l=strlen(a);
        mp[str]=1;
        for(i=0;i<l;i++)
        {
            if(a[i]==' ')
            {
                if(!mp[str])mp[str]=1,cnt++;
                str="";
            }
            else str+=a[i];
        }
        if(!mp[str])cnt++;
        cout<<cnt<<endl;
    }
}

2091

#include<bits/stdc++.h>
using namespace std;
char a;
int main()
{
    int n,i,j,t=0;
    while(cin>>a&&a!='@')
    {
        cin>>n;
        if(t)cout<<endl;
        t=1;
        for(i=1;i<=n;i++)
        {
            if(i==1)
            {
                for(j=1;j<=n-i;j++)
                    cout<<" ";
                cout<<a<<endl;
            }
            else if(i==n)
            {
                for(j=1;j<=2*i-1;j++)
                    cout<<a;
                cout<<endl;
            }
            else
            {
                for(j=1;j<=n-i;j++)
                    cout<<" ";
                cout<<a;
                for(j=1;j<=2*i-3;j++)
                    cout<<" ";
                cout<<a<<endl;
            }
        }
    }
}

1713
题解:分数的最小公倍数=分子的最小公倍数/分母的最大公约数

#include<bits/stdc++.h>
using namespace std;
int gcd(int a, int b)
{
    return a % b == 0 ? b : gcd(b, a % b);
}
int lcm(int a, int b)
{
    return a * b / gcd(a, b);
}
main()
{
    int t, a1, b1, a2, b2;cin >> t;
    while (t--){
        scanf("%d/%d", &a1, &b1);
        scanf("%d/%d", &a2, &b2);
        int c = gcd(a1, b1);
        a1 /= c, b1 /= c;
        c = gcd(a2, b2);
        a2 /= c, b2 /= c;
        int a3 = lcm(a1, a2);
        int b3 = gcd(b1, b2);
        if (a3 % b3 == 0)cout << a3 << endl;
        else cout << a3 << '/' << b3 << endl;
    }
}

1717
题解:小数分为两种:
1、有限小数,形如0.a。分数=a / 10^len(a)。
2、无限小数。小数点后的数-非循环数 / 循环数的长度个9+非循环数的长度个0。

#include<bits/stdc++.h>
#include<string.h>
using namespace std;
char str[20];
int gcd(int a, int b)
{
    return a % b == 0 ? b : gcd(b, a % b);
}
main()
{
    int n;cin >> n;
    while (n--){
        cin >> str;
        int i, l = strlen(str);
        int t1 = 0, t2 = 0, t3 = 0;
        int a = 0, b = 0, c = 0, d = 0;
        for (i = 0; i < l; i++){
            if (str[i] == '.') t1 = 1;
            if (str[i] == '(') t2 = 1;
            if (str[i] == ')') t3 = 1;
            if (t1 && str[i] >= '0' && str[i] <= '9')
                a = a * 10 + str[i] - '0';
            if (t1 && !t2&& str[i] >= '0' && str[i] <= '9')
                b = b * 10 + str[i] - '0', d++;
            if (t2 && !t3&& str[i] >= '0' && str[i] <= '9')
                c++;
        }
        if (!c){
            int aa = a, bb = 1, cc;
            while (d--)
                bb *= 10;
            cc = gcd(aa, bb);
            aa /= cc, bb /= cc;
            cout << aa << '/' << bb << endl;
        }
        else{
            int aa = a - b, bb = 0, cc;
            while (c--)
                bb = bb * 10 + 9;
            while (d--)
                bb *= 10;
            cc = gcd(aa, bb);
            aa /= cc, bb /= cc;
            cout << aa << '/' << bb << endl;
        }
    }
}

2031
题解:十进制转R进制用短除法,且负数要取正。余数>=10加55,否则加48。

#include<string.h>
using namespace std;
char arr[100];
void transform(int n, int r)
{
    memset(arr, 0, sizeof(arr));
    int res = 0, k = 0, abs_n = abs(n);
    while (abs_n){
        int remainder = abs_n % r;
        if (remainder >= 10) arr[k++] = remainder + 55;
        else arr[k++] = remainder + 48;
        abs_n /= r;
    }
    if (n < 0)cout << '-';
    for (int i = k - 1; i >= 0; i--)
        cout << arr[i];
    cout << endl;
}
main()
{
    int n, r;
    while (cin >> n >> r){
        transform(n, r);
    }
    return 0;
}

2057
题解:十六进制字母大写用%llX,小写用%llx。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
main()
{
    ll a, b;
    while(~scanf("%llx%llx", &a, &b)){
        if (a + b < 0)printf("-%llX\n", abs(a + b));
        else printf("%llX\n", a + b);
    }
}

2098
题解:素数筛。

nclude<bits/stdc++.h>
using namespace std;
int arr[510], vis[10010];
void select()
{
    vis[1] = 1;
    for (int i = 2; i <= 10000; i++){
        if (!vis[i]){
            for(int j = 2 * i; j <= 10000; j += i)
                vis[j] = 1;
        }
    }
    int k = 0;
    for (int i = 2; i <= 10000; i++)
        if(!vis[i]) arr[k++] = i;
}
main()
{
    select();
    int n, i, j;
    while (cin >> n && n){
        int cnt = 0;
        for (i = 0; ; i++){
            if (arr[i] > n) break;
            for (j = i + 1; ; j++){
                if (arr[j] > n) break;
                if( arr[i] + arr[j] == n) cnt++;
            }
        }
        cout << cnt << endl;
    }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值