回 帖 发 新 帖 刷新版面

主题:第79次编程比赛题目

[b]1、破解某论坛管理员的密码[/b]
题目描述:
  将数字1-9填入以下九宫格,要求每行每列及每个小方格(共九个)均为1-9,不能重复出现,
管理员密码的后九位就在第五行。要求编程解出其值。
输入: 无
输出: 标准输出,输出这九个整数,每个数字占一行
[center][img]http://www.hxcxit.com/Lory/1.png[/img][/center]

[b]2、Lagrange’s Four-Square Theorem[/b]
Input: lagrange.txt
The fact that any positive integer has a representation as the sum of at most four positive squares (i.e. squares of positive integers) is known as Lagrange’s Four-Square Theorem. The first published proof of the theorem was given by Joseph-Louis Lagrange in 1770. Your mission however is not to explain the original proof nor to discover a new proof but to show that the theorem holds for some specific numbers by counting how many such possible representations there are.
For a given positive integer n, you should report the number of all representations of n as the sum of at most four positive squares. The order of addition does not matter, e.g. you should consider 4^2 + 3^2 and 3^2 + 4^2 are the same representation.
For example, let’s check the case of 25. This integer has just three representations 1^2+2^2+2^2+4^2, 3^2 + 4^2, and 5^2. Thus you should report 3 in this case. Be careful not to count 4^2 + 3^2 and 3^2 + 4^2 separately.
[b]Input[/b]
The input is composed of at most 255 lines, each containing a single positive integer less than 2^15, followed by a line containing a single zero. The last line is not a part of the input data.
[b]Output[/b]
The output should be composed of lines, each containing a single integer. No other characters
should appear in the output.
The output integer corresponding to the input integer n is the number of all representations
of n as the sum of at most four positive squares.
[b]Sample Input[/b]
1
25
2003
211
20007
0
[b]Output for the Sample Input[/b]
1
3
48
7
738
[color=800080]开始时间: 2008-12-18 20:30:00
结束时间: 2008-12-26 20:30:00

第二题为选做,是我和同学以前做的 Acm 练习题,希望大家都能参与到其中来。[/color]

回复列表 (共16个回复)

沙发

A response test.

板凳

我必须过了20号才能做题目,20号要考英语!期待好的答案

3 楼

#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;

int count=0;

void LFST(int temp,int lim,int n)
{
    for(int i=(int)sqrt(temp/n);i<=(int)sqrt(temp)&&i<=lim;i++)
    {
        if(i*i==temp)
            count++;
        else if(i*i<temp)
        {
            if(n-1!=0)
                LFST(temp-i*i,i,n-1);
        }
    }
}

void main()
{
    ifstream fin("input.txt");
    ofstream fout("output.txt");
    int temp;
    fin>>temp;
    while(temp!=0)
    {    
        LFST(temp,sqrt(temp)+1,4);
        fout<<count<<endl;
        count=0;
        fin>>temp;
    }    
}




4 楼

第一题的图看不到,只能做第二题...

5 楼

0.0

6 楼

jp b dkcjo

7 楼


[em10]

8 楼

参与,,呵呵,大家一起进步!!
第一题:
// 2008.12.23 v1.0  write by qingfengliandie
#include <stdio.h>
int main()
{
    int a[9][9] = {{0,0,3,8,1,0,0,0,9},
                   {5,0,0,4,0,0,0,8,0},
                   {0,6,0,9,0,0,1,0,0},
                   {0,0,8,0,3,0,0,0,6},
                   {0,0,0,0,0,0,0,0,0},
                   {9,0,0,6,0,0,5,0,0},
                   {0,0,6,0,0,9,0,1,0},
                   {0,1,0,0,0,5,0,0,4},
                   {2,0,0,0,4,8,7,0,0}
                   };
    int m=0;
    int n=0;
    if(sub1(a,m,n)==0){
    printf("OK!\n");
    }
    return 0;
}
int sub1(int b[9][9], int m, int n) {
    int re,k;
    if(b[m][n] > 0){
       if(n==8){
        re=sub1(b,m+1,0);
       }
       else{
           re=sub1(b,m,n+1);
       }
       if(re==1){
           return 1;
       }
       else{
           return 0;
       }
    }
    else{
      for(k=1;k<10;k++){
          b[m][n]=k;
          if (sub2(b,m,n)==0 ){
              if(m==8 && n == 8){
              sub3(b);
              return 0;
            }
            if(n==8){
               re=sub1(b,m+1,0);
            }
            else{
                  re=sub1(b,m,n+1);
            }
            if(re==0){
                  return 0;
            }
            else{
                  if(k==9){
                        b[m][n]=0;
                  return 1;}
            }
        }
        else{
            if(k==9){
            b[m][n]=0;
            return 1;}
        }
      }
    }
 }
int sub3(int b[9][9]){
  int i,j;                
    for (i = 0 ;i < 9;i++){
           if(i==6){
          printf("the password is:\n");
          for(j = 0;j < 9;j++){
          printf(" %d ",b[i][j]);
             }
          printf("\n");
       }
    }
  return     0;    
}     
int sub2(int b[9][9], int m, int n) {
 int i,j;
 int s[9];
 int k=0;
 int z = (int)m/3;
 int x = (int)n/3;    
  for ( i = 0;i<9;i++){
      for ( j = i+1;j<9;j++){    
      if(b[m][i]!=0 && b[m][i]==b[m][j]){return 1;}
      if(b[i][n]!=0 && b[i][n]==b[j][n]){return 1;}
      }
  }
  for ( i = 0;i<3;i++){
      for ( j = 0;j<3;j++){
     s[k++] = b[z*3+i][x*3+j];
      }
  }
 for ( i = 0;i<9;i++){
      for ( j = i+1;j<9;j++){
      if(s[i]!=0 && s[i]==s[j]){return 1;}
      }
  }
 return 0;
}
     

9 楼

第一题
[code=c]
#include <stdio.h>

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

void display()
{
    int i;
    int j;
    for (i = 0; i < 9; i++)
    {
        for (j = 0; j < 9; j++)
        {
            if(map[i][j])
            {
                printf("< %d > ", map[i][j]);
            }
            else
            {
                printf("<   > ");
            }
        }
        printf("\n");
    }
}

int check(int x, int y, int *mark)
{
    int i;
    int j;
    int gi;
    int gj;
    int count = 0;

    for (i = 1; i <= 9; i++)
    {
        mark[i] = 0;
    }

    for (i = 0; i < 9; i++)
    {
        mark[map[x][i]] = 1;
        mark[map[i][y]] = 1;
    }

    gi = x / 3 * 3;
    gj = y / 3 * 3;

    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 3; j++)
        {
            mark[map[gi + i][gj + j]] = 1;
        }
    }

    for (i = 1; i <= 9; i++)
    {
        if(0 == mark[i])
        {
            count++;
        }
    }

    return count;
}

void crack()
{
    int i;
    int j;
    int mark[10];
    int min = 10;
    int ci = -1;
    int cj;

    for (i = 0; i < 9; i++)
    {
        for (j = 0; j < 9; j++)
        {
            if(map[i][j])
            {
                continue;
            }
            int c = check(i, j, mark);

            if (0 == c)
            {
                return;
            }

            if (c < min)
            {
                ci = i;
                cj = j;
                min = c;
            }
        }
    }

    if (-1 == ci)
    {
        printf("The answer is:\n");
        display();
        return;
    }

    check(ci, cj, mark);
    for (i = 1; i <= 9; i++)
    {
        if (mark[i] == 0)
        {
            map[ci][cj] = i;
            crack();
        }
        map[ci][cj] = 0;
    }
}
int main()
{
    printf("The game is:\n");
    display();

    crack();

    return 0;
}
[/code]

10 楼

1,居然看不见那数独图片.....应该大概思路是预处理+回溯+剪枝吧...有个问题是不知道图给的数独解是否唯一
2.简单枚举了....
[code]
#include<iostream>
#include<math.h>
using namespace std;

int main()
{
    int i,j,k;
    int sqr,sqr2,sqr3,sqr4;
    int n;
    int ans;
    while(cin>>n,n)
    {
        ans=0;
        sqr=(int)sqrt(n);if(sqr*sqr==n) ans++;//1
        for(i=1,sqr=(int)sqrt(n/2);i<=sqr+1;i++)//2
        {
            sqr2=(int)sqrt(n-i*i);
            if(sqr2<i) break;
            if(sqr2*sqr2+i*i==n) ans++;
        }
        for(i=1,sqr=(int)sqrt(n/3);i<=sqr+1;i++)//3
        {
            for(j=i,sqr2=(int)sqrt((n-i*i)/2);j<=sqr2+1;j++)
            {
                sqr3=(int) sqrt(n-i*i-j*j);
                if(sqr3<j) break;
                if(sqr3*sqr3+i*i+j*j==n) ans++;
            }
        }
        for(i=1,sqr=(int)sqrt(n/4);i<=sqr+1;i++)//4
        {
            for(j=i,sqr2=(int)sqrt((n-i*i)/3);j<=sqr2+1;j++)
            {
                for(k=j,sqr3=(int)sqrt((n-i*i-j*j)/2);k<=sqr3+1;k++)
                {
                    sqr4=(int) sqrt(n-i*i-j*j-k*k);
                    if(sqr4<k) break;
                    if(sqr4*sqr4+i*i+j*j+k*k==n) ans++;
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

[/code]

我来回复

您尚未登录,请登录后再回复。点此登录或注册