写一个java程序 将一首古诗竖着排序从右往左读

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了写一个java程序 将一首古诗竖着排序从右往左读相关的知识,希望对你有一定的参考价值。

import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.StringReader;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class PoetryTest
public static void main(String[] args) throws Exception
final JFrame f = new JFrame();
final ArrayList<Poetry> ps = PContents.load();
final PoetryLabel plabel = new PoetryLabel();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(plabel);
f.setSize(320,268);
f.setLocationRelativeTo(null);
f.setVisible(true);
plabel.setPoetry(ps.get(0));
f.setTitle(plabel.getTitle()+"-"+plabel.getAuthor());
plabel.addMouseListener(new MouseAdapter()
int index;
public void mouseClicked(MouseEvent e)
++index;
index=index%ps.size();
Poetry p = ps.get(index);
plabel.setPoetry(p);
f.setTitle(plabel.getTitle()+"-"+plabel.getAuthor());

);


//呈现诗歌的标签类
class PoetryLabel extends JLabel
private static final Color tColor = Color.white;//标题颜色
private static final Color aColor = Color.yellow;//作者颜色
private static final Color cColor = Color.LIGHT_GRAY;//内容颜色
private static final int fontSize=16;//字体大小
private static final int tType = Font.BOLD;//标题字体样式为粗体
private static final int aType = Font.ITALIC;//作者字体样式为斜体
private static final int cType = Font.PLAIN;//内容字体样式为普通样式
private static final float scaleFactor = 1.2f;//标题字体大小比其它字体大
private static final String fontName="SimSun";//字体名称
private static final int offset=5,space=5;

private Font font;
private AffineTransform at;
private Poetry poem;

public PoetryLabel()
this.poem=new Poetry();
this.font=new Font(fontName,cType,fontSize);
this.setHorizontalAlignment(JLabel.CENTER);
this.setVerticalAlignment(JLabel.CENTER);
at = new AffineTransform();
at.setToRotation(Math.toRadians(-90));
font = font.deriveFont(at);//将字体逆时针旋转90°

public Poetry getPoetry()
return poem;

//设置诗歌,来初始化标签内容
public void setPoetry(Poetry s)
this.poem=s;
this.repaint();

public String getTitle()return poem.title;
public String getAuthor()return poem.author;
//创建图像,先将逆时针旋转了90°的文件画上,然后将整幅图像顺时针旋转90°
private Image createImage()
int x;
int y=offset;
int w = getTWidth(poem);
int h = getTHeight(poem);
BufferedImage img = new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB);
Graphics2D g = img.createGraphics();
//draw title
g.setColor(tColor);
Font tfont = font.deriveFont(font.getSize()*scaleFactor).deriveFont(tType);
g.setFont(tfont);
y+=tfont.getSize();
char[] cs = poem.title.toCharArray();
for(int i=0; i<cs.length; i++)
x=(i+1)*(tfont.getSize()+space)+offset;
g.drawChars(cs, i, 1, x, y);

//draw author
y+=space*2+tfont.getSize();
cs = poem.author.toCharArray();
tfont = font.deriveFont(aType);
g.setColor(aColor);
g.setFont(tfont);
for(int i=0; i<cs.length; i++)
x=(i+1)*(tfont.getSize()+space)+offset;
g.drawChars(cs, i, 1, x, y);

//draw contents
y+=space*3;
g.setColor(cColor);
g.setFont(font);

for(int i=0; i<poem.contents.size(); i++)
y+=fontSize+space;
cs = poem.contents.get(i).toCharArray();
for(int j=0; j<cs.length; j++)
x=(j+1)*(tfont.getSize()+space)+offset;
g.drawChars(cs, j, 1, x, y);


g.dispose();
return this.getRotateImage(img);

//将图像顺时针旋转90°
private Image getRotateImage(final BufferedImage img)
int w = img.getWidth();
int h = img.getHeight();
BufferedImage newImg = new BufferedImage(h,w,2);
AffineTransformOp op = new AffineTransformOp(AffineTransform.
getRotateInstance(Math.toRadians(90),h/2,h/2),null);
return op.filter(img,newImg);

//绘图
public void paint(Graphics gg)
BufferedImage img = (BufferedImage)this.createImage();
int iw = img.getWidth();
int ih = img.getHeight();
Graphics2D g = (Graphics2D)gg;
int w = this.getWidth();
int h = this.getHeight();
g.setPaint(new GradientPaint(0,0,Color.DARK_GRAY,0,h,Color.black));
g.fillRect(0,0,w,h);
int x = (w-iw)/2;
int y = (h-ih)/2;
g.drawImage(img, x, y, this);
g.dispose();

//计算图片的高度
private int getTHeight(Poetry s)
int h = offset*2+space*5;
h+=(int)(fontSize*(scaleFactor+1));
h+=(fontSize+space*2)*s.contents.size();
return h;

//计算图片的宽度
private int getTWidth(Poetry s)
int tl = (int)(s.title.length()*(fontSize*scaleFactor+space)+offset*2);
int al = s.author.length()*(fontSize+space)+offset*2;
int t;
int max = tl>al?tl:al;
for(int i=0; i<s.contents.size(); i++)
t = s.contents.get(i).length()*(fontSize+space)+offset*2;
max=max>t?max:t;

return max;


//诗歌类
class Poetry
public String title;//标题
public String author;//作者
public ArrayList<String> contents=new ArrayList<String>();//内容
public void addLine(String s)this.contents.add(s);//加入一行内容

//
class PContents
public static String s=
"苏武庙\r\n"+
"温庭筠\r\n"+
"苏武魂销汉使前,\r\n"+
"古祠高树两茫然。\r\n"+
"云边雁断胡天月,\r\n"+
"陇上羊归塞草烟。\r\n"+
"回日楼台非甲帐,\r\n"+
"去时冠剑是丁年。\r\n"+
"茂陵不见封侯印,\r\n"+
"空向秋波哭逝川。\r\n"+
"\r\n"+
"贫女\r\n"+
"秦韬玉\r\n"+
"蓬门未识绮罗香,\r\n"+
"拟托良媒益自伤。\r\n"+
"谁爱风流高格调?\r\n"+
"共怜时世俭梳妆。\r\n"+
"敢将十指夸针巧,\r\n"+
"不把双眉斗画长。\r\n"+
"苦恨年年压金线,\r\n"+
"为他人作嫁衣裳。\r\n"+
"\r\n"+
"宫词\r\n"+
"薛逢\r\n"+
"十二楼中尽晓妆,\r\n"+
"望仙楼上望君王。\r\n"+
"锁衔金兽连环冷,\r\n"+
"水滴铜龙昼漏长。\r\n"+
"云髻罢梳还对镜,\r\n"+
"罗衣欲换更添香。\r\n"+
"遥窥正殿帘开处,\r\n"+
"袍裤宫人扫御床。\r\n"+
"\r\n"+
"利洲南渡\r\n"+
"温庭筠\r\n"+
"澹然空水对斜晖,\r\n"+
"曲岛苍茫接翠微。\r\n"+
"波上马嘶看棹去,\r\n"+
"柳边人歇待船归。\r\n"+
"数丛沙草群鸥散,\r\n"+
"万顷江田一鹭飞。\r\n"+
"谁解乘舟寻范蠡,\r\n"+
"五湖烟水独忘机?\r\n"+
"\r\n"+
"无题二首之一\r\n"+
"李商隐\r\n"+
"凤尾香罗薄几重,\r\n"+
"碧文圆顶夜深缝。\r\n"+
"扇裁月魄羞难掩,\r\n"+
"车走雷声语未通。\r\n"+
"曾是寂寥金烬暗,\r\n"+
"断无消息石榴红。\r\n"+
"斑骓只系垂杨岸,\r\n"+
"何处西南任好风?\r\n"+
"\r\n"+
"无题二首之二\r\n"+
"李商隐\r\n"+
"重帷深下莫愁堂,\r\n"+
"卧后清宵细细长。\r\n"+
"神女生涯原是梦,\r\n"+
"小姑居处本无郎。\r\n"+
"风波不信菱枝弱,\r\n"+
"月露谁教桂叶香?\r\n"+
"直道相思了无益,\r\n"+
"未妨惆怅是清狂。\r\n"+
"\r\n"+
"金陵图\r\n"+
"韦庄\r\n"+
"江雨霏霏江草齐,\r\n"+
"六朝如梦鸟空啼。\r\n"+
"无情最是台城柳,\r\n"+
"依旧烟笼十里堤。\r\n" +
"\r\n"
;
public static ArrayList<Poetry> load() throws Exception
ArrayList<Poetry> list = new ArrayList<Poetry>();
BufferedReader br = new BufferedReader(new StringReader(s));
Poetry p = null;
String t = null;
while((t = br.readLine()) != null)
if(p==null)
p=new Poetry();
if(t.isEmpty())
list.add(p);
p=null;

else if(p.title==null)
p.title=t;
else if(p.author==null)
p.author=t;
else
p.addLine(t);

return list;

参考技术A public static void main(String[] args)

String[] src=new String[]
"离离原上草,",
"一岁一枯荣。",
"野火烧不尽,",
"春风吹又生。"
;
ArrayList list=new ArrayList();
int maxlen=0;
for(int i=0;i<src.length;i++)
StringBuffer buffer=new StringBuffer();
if(src[i]!=null && src[i].length()>0)
buffer.append(src[i]);
if(maxlen<src[i].length())//记录最长的一行
maxlen=src[i].length();

else
buffer.append("");

list.add(buffer);


//转存结果
ArrayList result=new ArrayList();
for(int i=0;i<maxlen;i++)
StringBuffer buffer=new StringBuffer();
for(int j=0;j<list.size();j++)
StringBuffer tmp=(StringBuffer)list.get(j);
if(i>=tmp.length())
buffer.append(" ");
else
buffer.append(tmp.charAt(i)+" ");


result.add(buffer.toString());

//己得到result结果,横向打印
for(int i=0;i<result.size();i++)
System.out.println(result.get(i).toString());




说明: 需要熟练掌握 ArrayList和BufferString的用法
先把字符组存到ArrayList中,
1。每个元素的第一个字符拿出来组成一个新的字符串
2。每个元素的第二个字符拿出来组成第二个新字符串
3。类上
4。直到结束

优点,对所有四行字数相等的唐诗都行快速转换。
缺点,对所有每行字数不相等的宋词就不管用了,
但楼主只需要诗吧
参考技术B 我运行0079007的那个程序
第一行输出与后面有偏差~~~
参考技术C 需求不明确.

回文串(最长公共子序列)

 

一个字符串如果从左往右读和从右往左读都一样,那么这个字符串是一个回文串。例如:"abcba","abccba"。

蒜头君想通过添加字符把一个非回文字符串变成回文串。例如:"trit",可以添加一个i变成回文串"tirit"。

请你用程序计算出,对于一个给定的字符串,最少需要添加几个字符,才能变成回文串。

输入格式

输入一个长度为n(1≤n≤3000)的字符串。(字符串只包含字母)

输出格式

输出最少需要添加的字符个数,占一行。

样例输入

trit

样例输出

1 

 

把回文串的顺序倒转后,与原串是一样的。

那么我们只要把给定的字符串顺序倒转与原串求最长公共子序列,再用字符串总长度减去最长公共子序列的长度就是相差的字符个数,也就是答案。

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <vector>
 8 #include <stack>
 9 #include <queue>
10 #include <set>
11 #include <map>
12 #include <sstream>
13 #include <ctime>
14 const int INF=0x3f3f3f3f;
15 typedef long long LL;
16 const int mod=1e9+7;
17 const double PI = acos(-1);
18 const double eps =1e-8;
19 #define Bug cout<<"---------------------"<<endl
20 const int maxn=1e5+10;
21 using namespace std;
22 
23 string str1,str2; 
24 int dp[3010][3010];
25 
26 int main()
27 {
28     cin>>str1;
29     str2=str1;
30     reverse(str2.begin(),str2.end());
31     for(int i=1;i<=str1.size();i++)
32     {
33         for(int j=1;j<=str2.size();j++)
34         {
35             if(str1[i-1]==str2[j-1]) dp[i][j]=dp[i-1][j-1]+1;
36             else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
37         }
38     }
39     cout<<str1.size()-dp[str1.size()][str2.size()]<<endl;
40     return 0;
41 }

 

 

 

 

 

-

以上是关于写一个java程序 将一首古诗竖着排序从右往左读的主要内容,如果未能解决你的问题,请参考以下文章

C语言 将一个正整数从右往左按位输出

Java中正则表达式如何实现从右往左匹配?

JavaScript基础 lastIndexOf 从右往左查找 返回一个子字符串在原始字符串中的索引

leetcode 31下一个排列

经典排序算法---快速排序

做图片轮播功能(要一直从右往左播,不断的循环),js怎么写?看一下我的代码给我讲几句重要的代码?