HNUCM-OJ算法練習13(JAVA,C)

問題 A: 牛妹的蛋糕

題目描述

衆所周知,牛妹非常喜歡吃蛋糕。

第一天牛妹吃掉蛋糕總數三分之一多一個,第二天又將剩下的蛋糕吃掉三分之一多一個,以後每天吃掉前一天剩下的三分之一多一個,到第n天準備吃的時候只剩下一個蛋糕。

牛妹想知道第一天開始吃的時候蛋糕一共有多少呢?

輸入

輸入n,0<n< 30。

輸出

輸出第一天蛋糕的數量。

樣例輸入 Copy

2
4

樣例輸出 Copy

3
10

import java.util.Scanner;
  
public class Main {
   
  
    public static void main(String[] args) {
  
        Scanner cin = new Scanner(System.in);
        while(cin.hasNext()) {
      int  n=cin.nextInt();
      int cnt=1;
      for(int i=1;i<n;i++) {
          cnt=((cnt+1)*3)/2;
      }
      System.out.println(cnt);
      }
    }
}
 

問題 B: 尼科徹斯定理

題目描述

驗證尼科徹斯定理,即:任何一個整數m的立方都可以寫成m個連續奇數之和。

例如:

1^3=1

2^3=3+5

3^3=7+9+11

4^3=13+15+17+19

輸入

多組輸入,輸入一個整數。

輸出

輸出分解後的字符串。

樣例輸入 Copy

6

樣例輸出 Copy

31+33+35+37+39+41

import java.util.Scanner;
  
public class Main {
    static void print_(int m,int i,boolean flag){
        if(flag==true){
            for(int k=0;k<m;k++){
                System.out.print(i+2*k);
              
              if(k<m-1) System.out.print("+");
            }
        }
        else System.out.print("-1");
          
    }
  
    public static void solve(int m) {
        boolean flag=false;
        int sum0=m*m*m;
        int sum1=0,start;
        for(int i=1;i<=sum0;i+=2){
             
            sum1=0;
            start=i;
            for(int j=0;j<m;j++){
              sum1=sum1+start;
              start+=2;
            }
            if(sum1==sum0) {
                flag=true;
                print_(m,i,flag);
                return;
            }
        }
          
        flag=false;
        print_(m,0,false);
  
  
    }
    public static void main(String[] args) {
   
        Scanner cin = new Scanner(System.in);
        while(cin.hasNext()) {
      int  n=cin.nextInt();
        
     StringBuffer str=new StringBuffer();
     solve(n);
     System.out.println();
      
        }
    }
}

問題 C: 油田問題

題目描述

輸入一個m行n列的字符矩陣,統計字符“@”組成多少個八連塊。如果兩個字符“@”所在的格子相鄰(橫、豎或者對角線方向),即屬於同一個八連塊。

輸入

多組輸入
輸入行數m,以及列數n。
然後輸入*和@
1<=n,m<=100

輸出

聯通塊個數

樣例輸入 Copy

5 5
****@
@@@
@**@
@@@
@
@@**@

樣例輸出 Copy

2

#include<cstdio>
#include<cstring>
const int maxn = 1000;
 
char arr[maxn][maxn];
int m, n, vist[maxn][maxn];
 
void dfs(int x, int y, int id){
    if (x < 0 || x >= m || y < 0 || y >= n)
        return;  //出界的格子
    if (vist[x][y]>0 || arr[x][y] != '@')return;
    vist[x][y] = id;//連通分量編號
    for (int dr = -1; dr <= 1;dr++)
    for (int dc = -1; dc <= 1;dc++)
    if (dr != 0 || dc != 0) dfs(x + dr, y + dc, id);//八個方向進行遞歸
 
}
 
int main()
{
    while (scanf("%d%d", &m, &n) == 2 && m&& n)
    {
        for (int i = 0; i < m; i++)
            scanf("%s", arr[i]);
        memset(vist, 0, sizeof(vist)); //用於標記已經訪問過的格子
        int cnt = 0;
        for (int i = 0; i < m;i++)
        for (int j = 0; j < n;j++)
        if (vist[i][j] == 0 && arr[i][j] == '@')dfs(i, j, ++cnt);
        printf("%d\n", cnt);
    }
 
    return 0;
}

問題 D: ABC + DEF = GHI

題目描述

用1, 2, 3…9 這九個數字組成一個數學公式,滿足:ABC + DEF = GHI,每個數字只能出現一次,編寫程序輸出所有的組合。

輸入

輸出

輸出所有的 ABC + DEF = GHI,
每行一條數據,格式爲ABC+DEF=GHI
輸出結果按照ABC升序排列,如果ABC相同,則按照DEF升序排列

#include<cstdio>
#include<algorithm>
using namespace std;
int main(){
    int list[] = {1,2,3,4,5,6,7,8,9};
    do{
 int a = list[0]*100 + list[1]*10 + list[2];
    int b = list[3]*100 + list[4]*10 + list[5];
    int c = list[6]*100 + list[7]*10 + list[8];
    if(a + b == c)
        printf("%d+%d=%d\n", a, b ,c);
        }
 
    while(next_permutation(list,list+9));
    return 0;
}

問題 E: 馬的遍歷問題

題目描述

在5*4的棋盤中,馬只能走斜“日”字。馬從位置(x, y)處出發,把棋盤的每一格都走一次,且只走一次,請找出所有路徑。

輸入

x,y,表示馬的初始位置。

輸出

將每一格都走一次的路徑總數,如果不存在該路徑則輸出“No solution!”。

樣例輸入 Copy

1 1
2 2

樣例輸出 Copy

32
No solution!

#include<iostream>
#include<stdio.h>
using namespace std;
 
int fx[8]= {1,2,2,1,-1,-2,-2,-1};
int fy[8]= {2,1,-1,-2,-2,-1,1,2};
 
static int count;
const static int n=5,m=4;
int a[n+1][m+1];  //用int二維數組來表示走的路勁
 
void dfs(int x,int y,int dep);//尋找路徑的遞歸
int Check(int x,int y);//判斷座標是否出界,是否已經走過
 
int main()
{
    int x,y;
    while(scanf("%d%d",&x,&y)!=EOF){
    count=0;
    for(int i=1; i<=n; i++)
        for(int j=1; j<=m; j++)
            a[i][j]=0;
 
    a[x-1][y-1]=1;
 
    dfs(x-1,y-1,2);
 
    if(count==0)
        cout<<"No solution!"<<endl;
    else
        cout<<count<<endl;
 
 
}
}
 
void dfs(int x,int y,int dep)
{
    int xx,yy;
    for(int i=0; i<8; i++)
    {
        xx=x+fx[i];
        yy=y+fy[i];
        if(Check(xx,yy)==1)
        {
            a[xx][yy]=dep;
            if(dep==n*m)
                count++;    //如果深度爲n*m,那麼表示遍歷結束,就打印
            else
                dfs(xx,yy,dep+1);
            a[xx][yy]=0;     //回溯,恢復未走座標。
        }
    }
}
 
int Check(int x,int y)
{
    //判斷座標是否出界,是否已經走過
    if(x<0||y<0||x>=n||y>=m||a[x][y]!=0)
        return 0;
    return 1;
}
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章