#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

int num;
int a[10]={0};
int f()
{
    for(int i=1;i<=num;i++)
    {
        int t=i;
        while(t)
        {
            a[t%10]++;
            t/=10;
        }
    }
    cout<<"页码数目为:"<<num<<endl;
    for(int i=0;i<10;i++)
        cout<<i<<"\t"<<a[i]<<endl;
}
int main()
{
    FILE *fp1=fopen("input.txt","r");
    FILE *fp2=fopen("output.txt","w");

    char text[10];
    while(fgets(text,10,fp1)!=NULL);
    num=atoi(text);
    cout<<num<<endl;
    f();
    for(int i=0;i<10;i++)
    {
        char t[10];
        sprintf(t,"%d\t%d\n",i,a[i]);
        fputs(t,fp2);
    }
    fclose(fp1);
    fclose(fp2);
}
0
Posted in ACM

#include<iostream>
#include<iomanip>
#include<stdlib.h>
using namespace std;


int tile = 0;
int *(*board) = NULL;

void ChessBoard(int tr, int tc, int dr, int dc, int size)
{
    if (size == 1) return;
    int t = tile++,
    s = size / 2;
    if (dr < tr + s && dc < tc + s)
    ChessBoard(tr, tc, dr, dc, s);
    else {
    board[tr + s - 1][tc + s - 1] = t;
    ChessBoard(tr, tc, tr + s - 1, tc + s - 1, s);
    }
    if (dr < tr + s && dc >= tc + s)
    ChessBoard(tr, tc + s, dr, dc, s);
    else {
    board[tr + s - 1][tc + s] = t;
    ChessBoard(tr, tc + s, tr + s - 1, tc + s, s);
    }
    if (dr >= tr + s && dc < tc + s)
    ChessBoard(tr + s, tc, dr, dc, s);
    else
    {
        board[tr + s][tc + s - 1] = t;
        ChessBoard(tr + s, tc, tr + s, tc + s - 1, s);
    }
    if (dr >= tr + s && dc >= tc + s)
    ChessBoard(tr + s, tc + s, dr, dc, s);
    else {
    board[tr + s][tc + s] = t;
    ChessBoard(tr + s, tc + s, tr + s, tc + s, s);
    }
}


int main()
{
    p1:int tx = 0, ty = 0, sp, dx, dy, zsize;
    cout << "方格行号:";  cin >> dx;
    cout << "方格列号 :"; cin >> dy;
    cout << "要填充的数字: "; cin >> sp;
    cout << "请输入棋盘的大小: ";
    cin >> zsize; cout << endl;
    board = new int *[zsize];
    for (int i = 0; i < zsize; i++)
    {
        board[i] = new int[zsize];
    }
    board[dx-1][dy-1] = sp;
    ChessBoard(tx, ty, dx-1, dy-1, zsize);
    for (int j = 0; j < zsize; j++)
    {
        for (int m = 0; m < zsize; m++)
        {
            cout << setw(8) << board[j][m];
        }
        cout << endl;
    }
    goto p1;
    return 0;
}
0
Posted in ACM

#include<iostream>
#include<fstream>
using  namespace std;

#define MAX 100
#define MAXCOST 0x7fffffff

int graph[MAX][MAX];

int prim(int graph[][MAX], int n)
{
    int lowcost[MAX];
    int mst[MAX];
    int i, j, min, minid, sum = 0;
    for (i = 2; i <= n; i++)
    {
        lowcost[i] = graph[1][i];
        mst[i] = 1;
    }
    mst[1] = 0;
    for (i = 2; i <= n; i++)
    {
        min = MAXCOST;
        minid = 0;
        for (j = 2; j <= n; j++)
        {
            if (lowcost[j] < min && lowcost[j] != 0)
            {
                min = lowcost[j];
                minid = j;
            }
        }
        cout << "V" << mst[minid] << "-V" << minid << "=" << min << endl;
        sum += min;
        lowcost[minid] = 0;
        for (j = 2; j <= n; j++)
        {
            if (graph[minid][j] < lowcost[j])
            {
                lowcost[j] = graph[minid][j];
                mst[j] = minid;
            }
        }
    }
    return sum;
}

int main()
{
    int i, j, k, m, n;
    int x, y, cost;
    cin>> m >> n;
    for (i = 1; i <= m; i++)
    {
        for (j = 1; j <= m; j++)
        {
            graph[i][j] = MAXCOST;
        }
    }

    for (k = 1; k <= n; k++)
    {
        cin >> i >> j >> cost;
        graph[i][j] = cost;
        graph[j][i] = cost;
    }

    cost = prim(graph, m);

    cout << "最小权值和=" << cost << endl;
    return 0;
}
0
Posted in ACM

#include<iostream>
using namespace std;
int x[100],bestx[100],m[100][100];

int f1=0,f2=0,cf=0,bestf=10000,n;

void Backtrack(int t)
{
    int tempf,j;
    if(t>n)
    {
        if(cf<bestf)
        {
            for(int i=1; i<=n; i++)
                bestx[i]=x[i];
            bestf=cf;
        }
    }
    else
    {
        for(j=t; j<=n; j++)
        {
            f1+=m[x[j]][1];
            tempf=f2;
            f2=(f1>f2?f1:f2)+m[x[j]][2];
            cf+=f2;
            if(cf<bestf)
            {
                swap(x[t],x[j]);
                Backtrack(t+1);
                swap(x[t],x[j]);
            }
            f1-=m[x[j]][1];
            cf-=f2;
            f2=tempf;
        }
    }
}

int main()
{
    int i,j;

    cout<<"请输入作业数:"<<endl;
    cin>>n;
    cout<<"请输入在各机器上的处理时间"<<endl;
    for(i=1; i<=2; i++)
        for(j=1; j<=n; j++)
            cin>>m[j][i];
    for(i=1; i<=n; i++)
        x[i]=i;
    Backtrack(1);
    cout<<"调度作业顺序:"<<endl;
    for(i=1; i<=n; i++)
        cout<<bestx[i]<<' ';
        cout<<endl;
    cout<<"处理时间:"<<endl;
    cout<<bestf;
    return 0;
}
/*
测试数据:
3
2 3 2
1 1 3

3
2 5 4
3 2 1
*/
0
Posted in ACM

#include<stdio.h>
#include<math.h>
#define N 50
void GameTable(int k,int array[][N]);
void print(int k,int array[][N]);
main()
{
    int k;
    int array[N][N];
    do
    {
         scanf("%d",&k);
        if(k!=0)
        {
            GameTable(k,array);
            print(k,array);
        }
        else
          printf("您输入的数据有误,请重新输入");
    }while(k!=0);

}
void GameTable(int k,int array[][N])
{
    int i,j,s,t;
    int n=1;
    for(i=1;i<=k;i++)
        n*=2;
    for(i=1;i<=n;i++)
        array[1][i]=i;
    int m=1;
    for(s=1;s<=k;s++)
    {
        n=n/2;
        for(t=1;t<=n;t++)
            for(i=m+1;i<=2*m;i++)
                for(j=m+1;j<=2*m;j++)
                {
                    array[i][j+(t-1)*m*2]=array[i-m][j+(t-1)*m*2-m];
                    array[i][j+(t-1)*m*2-m]=array[i-m][j+(t-1)*m*2];
                }
        m*=2;
    }

}
void print(int k,int array[][N])
{
    int i,j;
    int num=pow(2,k);
    printf("%d人的循环赛日程表如下\n",num);
    for(i=1;i<=num;i++)
    {
        for(j=1;j<=num;j++)
        {
            printf("%d\t",array[i][j]);
        }
         printf("\n");
    }
}
0
Posted in ACM