Rolling table 2135

Problem Description

After the 32nd ACM/ICPC regional contest, Wiskey is beginning to prepare for CET-6. He has an 

English words table and read it every morning.
One day, Wiskey's chum wants to play a joke on him. He rolling the table, and tell Wiskey how 

many time he rotated. Rotate 90 degrees clockwise or count-clockwise each time.
The table has n*n grids. Your task is tell Wiskey the final status of the table.

Input

Each line will contain two number.
The first is postive integer n (0 < n <= 10).
The seconed is signed 32-bit integer m.
if m is postive, it represent rotate clockwise m times, else it represent rotate count-clockwise -m 

times.
Following n lines. Every line contain n characters.

Output

Output the n*n grids of the final status.

Sample Input

3 2

123

456

789

3 -1

123

456

789

Sample Output

987

654

321

369

258

147

#include <cstdio>
#include <string>
#include <iostream>
const int MAX = 11;
char grids[MAX][MAX];

int main(int argc, const char* argv[])
{
    int n, m;
    while (std::cin >> n >> m)
    {
        std::cin.get();
        for (int i=0; i<n; ++i)
        {
            for (int j=0; j<n; ++j)
            {
                std::cin >> grids[i][j];
            }
            std::cin.get();
        }
        
        m %= 4;
        if (m<0)
        {
            m += 4;
        }    

        switch (m)
        {
        case 0:
            {
                for (int i=0; i<n; ++i)
                {
                    for (int j=0; j<n; ++j)
                    {
                        printf("%c", grids[i][j]);
                    }
                    printf("\n");
                }
                break;
            }
        case 1:
            {
                for (int j=0; j<n; ++j)
                {
                    for (int i=n-1; i>=0; --i)
                    {
                        printf("%c", grids[i][j]);
                    }
                    printf("\n");
                }
                break;
            }
        case 2:
            {
                for (int i=n-1; i>=0; --i)
                {
                    for (int j=n-1; j>=0; --j)
                    {
                        printf("%c", grids[i][j]);
                    }
                    printf("\n");
                }
                break;
            }
        case 3:
            {
                for (int j=n-1; j>=0; --j)
                {
                    for (int i=0; i<n; ++i)
                    {
                        printf("%c", grids[i][j]);
                    }
                    printf("\n");
                }
                break;
            }
        default:
            break;
        }
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, here's an implementation of a hash table with chaining in Python: ```python import random class HashTable: def __init__(self, capacity=10): self.capacity = capacity self.size = 0 self.table = [[] for _ in range(self.capacity)] def hash(self, key): hash_value = 0 for i in range(len(key)): hash_value = (hash_value * 31 + ord(key[i])) % self.capacity return hash_value def search(self, key): hash_value = self.hash(key) bucket = self.table[hash_value] for i in range(len(bucket)): if bucket[i][0] == key: return True return False def insert(self, key): if self.search(key): return hash_value = self.hash(key) bucket = self.table[hash_value] bucket.append((key, None)) self.size += 1 if self.size >= self.capacity * 0.7: self._resize() def delete(self, key): hash_value = self.hash(key) bucket = self.table[hash_value] for i in range(len(bucket)): if bucket[i][0] == key: bucket.pop(i) self.size -= 1 if self.size <= self.capacity * 0.3: self._resize() return def size(self): return self.capacity def _resize(self): self.capacity *= 2 new_table = [[] for _ in range(self.capacity)] for bucket in self.table: for key, value in bucket: hash_value = self.hash(key) new_table[hash_value].append((key, value)) self.table = new_table ``` The `__init__` method initializes the hash table with a specified capacity (default is 10), and creates a list of empty lists to represent the buckets. The `hash` method takes in a key and maps it to an integer value between 0 and the capacity of the table using a hash function. In this case, we're using a simple polynomial rolling hash function. The `search` method searches for a key in the table by computing its hash value and checking if it exists in the corresponding bucket. The `insert` method inserts a key into the table by computing its hash value and appending it to the corresponding bucket. If the load factor of the table exceeds 0.7, the table is resized to double its capacity. The `delete` method deletes a key from the table by computing its hash value and removing it from the corresponding bucket. If the load factor of the table falls below 0.3, the table is resized to half its capacity. The `size` method simply returns the current capacity of the table. The `_resize` method is a private helper method that is called by the `insert` and `delete` methods when the load factor threshold is exceeded. It creates a new table with double or half the capacity of the current table, and rehashes all the keys in the old table into the new table.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值