紫书第二章

///7744问题----技巧
#include "iostream"
#include "cstdio"
using namespace std;
int main()
{
    for(int x=1;;x++)
    {
        int n=x*x;
        if(n<1000)continue;
        if(n>9999)break;
        int hi=n/100;
        int lo=n%100;
        if(hi/10==hi%10&&lo/10==lo%10)
            printf("%d\n",n);
    }
    return 0;
}
///例题2-4阶乘之和---规律
#include "iostream"
#include "cstdio"
#include "time.h"
using namespace std;
int main()
{
     const int MOD=10e6;
     int n;
     while(~scanf("%d",&n))
     {
         if(n>40)
            cout<<140940313<<endl;
         else
         {
            int sum=0,factorial=1;
             for(int i=1;i<=n;i++)
             {
                 factorial=i*factorial%MOD;
                 sum+=factorial;
             }
             printf("%d\n",sum); 
         }
     }
    return 0;
}
///从40开始 答案不变

///习题2-1水仙花数
#include "iostream"
#include "cstdio"
using namespace std;
int main()
{
    int a,b,c;
    for(int i=100;i<1000;i++)
    {
        a=i%10;b=i%100/10;c=i/100;
        if(a*a*a+b*b*b+c*c*c==i)
            printf("%d\n",i);
    }
    return 0;
}
///习题2-2韩信点兵
#include "iostream"
#include "cstdio"
using namespace std;
int main()
{
    int a,b,c,n,t=0;
    while(~scanf("%d%d%d",&a,&b,&c))
    {
        t++;
        int i;
        for(i=10;i<=100;i++)
        {
            if(i%3==a&&i%5==b&&i%7==c)
            {
                cout<<"Case "<<t<<":"<<i<<endl;break;
            }
        }
        if(i>100)
            cout<<"Case "<<t<<":"<<"No answer"<<endl;
    }
    return 0;
}
///习题2-3倒三角形
#include "iostream"
#include "cstdio"
using namespace std;
int main()
{
    int n;
    while(~scanf("%d",&n)&&n)
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n-i;j++)
                printf(" ");
            for(int k=1;k<=2*i-1;k++)
                printf("*");
            printf("\n");
        }

        for(int i=n-1;i>=1;i--)
        {
            for(int j=1;j<=n-i;j++)
                printf(" ");
            for(int k=1;k<=2*i-1;k++)
                printf("*");
            printf("\n");
        }
    }
    return 0;
}
///习题2-4子序列的和
#include "iostream"
#include "cstdio"
using namespace std;
int main()
{
    int n,m,t=0;
    while(~scanf("%d%d",&n,&m)&&n&&m)
    {
        t++;
        double d=0,temp;
        for(int i=n;i<=m;i++)
        {
                d+=1.0/i/i;
        }
        printf("Case %d: %.5f\n",t,d);
    }
    return 0;
}
///题目中注意本题有陷阱:当i较大时i*i溢出,换为,1/i/i

///习题2-5分数化小数
#include "iostream"
#include "cstdio"
using namespace std;
int main()
{
    int a,b,c,t=0;
    while(~scanf("%d%d%d",&a,&b,&c)&&a&&b)
    {
        cout<<(double)a/b<<endl;
        t++;
        printf("Case %d: %d.",t,a/b);
        int remain=a%b;
        for(int i=0;i<c;i++)///小数部分单独提出
        {
            remain*=10;
            int temp=remain/b;
            if(i==c-1&&temp>=5)temp++;
            printf("%d",temp);
            remain%=b;
        }
        printf("\n");
    }
    return 0;
}
///double 默认最长6位,不足6位不补

///习题2-6排列
#include <stdio.h>
#include <stdlib.h>

int compare(const void *a, const void *b);

int main(int argc, char **argv){
    int arr[9];
    int abc, def, ghi;
    for(abc = 100; abc <334; abc++){
    def = 2 * abc;
    ghi = 3 * abc;
    arr[0] = abc/100;
    arr[1] = (abc/10)%10;
    arr[2] = abc%10;
    arr[3] = def/100;
    arr[4] = (def/10)%10;
    arr[5] = def%10;
    arr[6] = ghi/100;
    arr[7] = (ghi/10)%10;
    arr[8] = ghi%10;
    qsort(arr, 9, sizeof(int), compare);
    int m;
    for( m = 0; m<9; m++){
        if(arr[m] != m+1){
        break;
        }
    }
    if(m == 9){
        printf("%d %d %d\n",abc, def, ghi);
    }
    }
    return 0;
}

int compare(const void *a, const void *b){
    return *(int*)a - *(int*)b;
}

 

posted @ 2017-03-04 15:11  kimsimple  阅读(192)  评论(0编辑  收藏  举报