HDOJ(HDU) 2137 circumgyrate the string(此题用Java-AC不过!坑)

此题如果有用JavaACDSee,请评论,谢谢了。

Problem Description
Give you a string, just circumgyrate. The number N means you just circumgyrate the string N times, and each time you circumgyrate the string for 45 degree anticlockwise.

Input
In each case there is string and a integer N. And the length of the string is always odd, so the center of the string will not be changed, and the string is always horizontal at the beginning. The length of the string will not exceed 80, so we can see the complete result on the screen.

Output
For each case, print the circumgrated string.

Sample Input
asdfass 7

Sample Output
a
 s
  d
   f
    a
     s
      s

题目意思很简单:
输入一个字符串和一个整数n,n表示把字符串逆时针旋转n个45°,输出旋转后的图形。

注意,n可以是负数,如果是负数,就是按照顺时针旋转就可以了。

和HDU2135题类似。那个是矩阵旋转,这个是字符串旋转。
两题做法都一样,找出循环节分别输出。
此题的字符串旋转8次,可以回到原来的位置,所以对8取余,对8种情况分别输出就可以了。

此题有一个问题,我不知道其他人遇到没有,这个题目用JavaAC不了。。。
下面给出WA的Java代码,和AC的C语言代码。—大家可以对比一下。
Java这个代码完全没问题的,至少我还没找到错哪了,如果有找到的,求告知,谢谢。

WA的Java代码:

package cn.hncu.acm;

import java.util.Scanner;

public class P2137 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String str = sc.next();
            int n = sc.nextInt();
            n %= 8;
            if (n < 0)
                n += 8;
            int t = str.length();
            if (n == 0)
                System.out.println(str);
            else if (n == 4) {
                for (int i = t - 1; i >= 0; i--)
                    System.out.print(str.charAt(i));
                System.out.println();
            } else if (n == 1) {
                for (int i = t - 1; i >= 0; i--) {
                    for (int j = i; j > 0; j--)
                        System.out.print(" ");
                    System.out.println(str.charAt(i));
                }
            } else if (n == 2) {
                for (int i = t - 1; i >= 0; i--) {
                    for (int j = 0; j < (t / 2); j++)
                        System.out.print(" ");
                    System.out.println(str.charAt(i));
                }
            } else if (n == 3) {
                for (int i = t - 1; i >= 0; i--) {
                    for (int j = t - 1; j > i; j--)
                        System.out.print(" ");
                    System.out.println(str.charAt(i));
                }
            } else if (n == 5) {
                for (int i = 0; i < t; i++) {
                    for (int j = i + 1; j < t; j++)
                        System.out.print(" ");
                    System.out.println(str.charAt(i));
                }
            } else if (n == 6) {
                for (int i = 0; i < t; i++) {
                    for (int j = 0; j < t / 2; j++)
                        System.out.print(" ");
                    System.out.println(str.charAt(i));
                }
            } else if (n == 7) {
                for (int i = 0; i < t; i++) {
                    for (int j = 0; j < i; j++)
                        System.out.print(" ");
                    System.out.println(str.charAt(i));
                }
            }
        }
    }
}

AC的C语言代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>  

char name[100];
int n;  

int main()
{
    while( scanf("%s",name) !=EOF ){
           int t = strlen(name);
           scanf("%d",&n);
           n %= 8;
           if( n < 0 )
               n += 8;
           if( n == 0 )
               printf("%s\n",name);
           else if( n==4 ){
                for( int i=t-1 ; i>=0 ; i-- )
                     printf("%c",name[i]);
                printf("\n");
           }
           else if( n == 1 ){
                for( int i=t-1; i>=0 ; i-- ){
                     for( int j=i ; j>0 ; j-- )
                          printf(" ");
                     printf("%c\n",name[i]);
                }
           }
           else if( n == 2 ){
                for( int i=t-1 ; i>=0 ; i-- ){
                     for( int j=0 ; j<(t/2) ; j++ )
                          printf(" ");
                printf("%c\n",name[i]);
                }
           }
           else if( n == 3 ){
                for( int i=t-1 ; i>=0 ; i-- ){
                     for( int j=t-1 ; j>i ; j-- )
                          printf(" ");
                     printf("%c",name[i]);
                     printf("\n");
                }
           }
           else if( n == 5 ){
                for( int i=0 ; i<t ; i++ ){
                     for( int j=i+1 ; j<t ; j++ )
                          printf(" ");
                     printf("%c\n",name[i]);
                }
           }
           else if( n == 6 ){
                for( int i=0 ; i<t ; i++ ){
                     for( int j=0 ; j<t/2 ; j++ )
                          printf(" ");
                printf("%c\n",name[i]);
                }
           }
           else if( n == 7 ){
                for( int i=0 ; i<t ; i++ ){
                     for( int j=0 ; j<i ; j++ )
                          printf(" ");
                     printf("%c\n",name[i]);
                }
           }
    }
    return 0;
}  
时间: 2024-08-24 00:09:50

HDOJ(HDU) 2137 circumgyrate the string(此题用Java-AC不过!坑)的相关文章

HDOJ/HDU 1328 IBM Minus One(水题一个,试试手)

Problem Description You may have heard of the book '2001 - A Space Odyssey' by Arthur C. Clarke, or the film of the same name by Stanley Kubrick. In it a spaceship is sent from Earth to Saturn. The crew is put into stasis for the long flight, only tw

HDOJ(HDU) 1555 How many days?(水题)

Problem Description 8600的手机每天消费1元,每消费K元就可以获赠1元,一开始8600有M元,问最多可以用多少天? Input 输入包括多个测试实例.每个测试实例包括2个整数M, k,(2 <= k <= M <= 1000).M = 0, k = 0代表输入结束. Output 对于每个测试实例输出一个整数,表示M元可以用的天数. Sample Input 2 2 4 3 0 0 Sample Output 3 5 水题.... import java.util.

HDOJ/HDU 2537 8球胜负(水题.简单的判断)

Problem Description 8球是一种台球竞赛的规则.台面上有7个红球.7个黄球以及一个黑球,当然还有一个白球.对于本题,我们使用如下的简化规则:红.黄两名选手轮流用白球击打各自颜色的球,如果将该颜色的7个球全部打进,则这名选手可以打黑球,如果打进则算他胜.如果在打进自己颜色的所有球之前就把黑球打进,则算输.如果选手不慎打进了对手的球,入球依然有效. 现在给出打进的球(白球除外)的顺序,以及黑球由哪方打进,你的任务是判定哪方是胜者. 假设不会有一杆同时打进一颗黑球和其他彩球. Inp

HDOJ(HDU) 2123 An easy problem(简单题...)

Problem Description In this problem you need to make a multiply table of N * N ,just like the sample out. The element in the ith row and jth column should be the product(乘积) of i and j. Input The first line of input is an integer C which indicate the

HDOJ(HDU) 1562 Guess the number(水题,枚举就行)

Problem Description Happy new year to everybody! Now, I want you to guess a minimum number x betwwn 1000 and 9999 to let (1) x % a = 0; (2) (x+1) % b = 0; (3) (x+2) % c = 0; and a, b, c are integers between 1 and 100. Given a,b,c, tell me what is the

HDOJ(HDU) 2133 What day is it(认识下Java的Calendar类---日期类)

Problem Description Today is Saturday, 17th Nov,2007. Now, if i tell you a date, can you tell me what day it is ? Input There are multiply cases. One line is one case. There are three integers, year(0< year<10000), month(0<=month<13), day(0<

HDOJ/HDU 1161 Eddy&amp;#39;s mistakes(大写字母转换成小写字母)

Problem Description Eddy usually writes articles ,but he likes mixing the English letter uses, for example "computer science" is written frequently "coMpUtEr scIeNce" by him, this mistakes lets Eddy's English teacher be extremely disco

tomcat-InputStream转String问 题(如同见鬼一样令人费解)

问题描述 InputStream转String问 题(如同见鬼一样令人费解) 如图所示,我想调用一个将inputstream转换成string的函数,如果注释掉这两行,能够正常写图片,旦调用以后,就损坏了,我就是光调用了一下,结果都没有用 解决方案 解决方案二: 你那两行代码调用时,就读取了inputstream,当你下面的代码再次读取时,inputstream读取的byte位置已经发生的改变,所以读出的byte数据就出错了. 应该先读取完inputstream,并保存起来,再对保存的byte数

HDOJ/HDU 2087 剪花布条(indexOf()应用~~)

Problem Description 一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案.对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢? Input 输入中含有一些数据,分别是成对出现的花布条和小饰条,其布条都是用可见ASCII字符表示的,可见的ASCII字符有多少个,布条的花纹也有多少种花样.花纹条和小饰条不会超过1000个字符长.如果遇见#字符,则不再进行工作. Output 输出能从花纹布中剪出的最多小饰条个数,如果一块都没有,那就老老实实输出