扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
代码如下,可以直接运行。
目前创新互联已为数千家的企业提供了网站建设、域名、雅安服务器托管、网站改版维护、企业网站设计、漳平网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
public static void main(String[] args) {
final int M = 6; // number of girls,可改动
final int N = 7; // number of boys,可改动
int x = 3;// some boy,可改动
int y = 5;// some girl,可改动
String result = "";// 记录结果,即第二个问题
// 初始化,假设队列存放男女生编号,从1开始
QueueInteger boys = new LinkedListInteger();
for (int i = 1; i = N; i++) {
boys.add(i);
}
QueueInteger girls = new LinkedListInteger();
for (int i = 1; i = M; i++) {
girls.add(i);
}
// 跳舞开始
int min = boys.size() girls.size() ? girls.size() : boys.size();
int k = 1;// songs
int count = 2; // 求出两个值,可改动
while (k 1000) {//为了不死循环,这里假设最多有999支舞蹈
System.out.println("***This is the " + k + "st dance:");
for (int i = 0; i min; i++) {
// 跳舞,第一个问题:输出每曲配对情况
System.out.println("Boy " + boys.peek() + " = Girl "
+ girls.peek());
// 跳过的排到对尾
int boy = boys.remove();
boys.add(boy);
int girl = girls.remove();
girls.add(girl);
// 判断 x和y跳舞了没有
if (boy == x girl == y) {
result += k + ",";
count--;
}
}
if (count == 0)
break;
// next dance
k++;
}
// 结果
if (count == 0)
System.out.println("\n***Boy " + x + " and Girl " + y
+ " dance together in : " + result);//第二个问题的解答,跳了哪几支舞
else
System.out.println("\n***Boy " + x + " and Girl " + y
+ " have no chance to dance!");//第二个问题的解答,两人没机会跳舞
}
程序如下:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
public class JFrameDemo extends JFrame implements ActionListener
{
private JPanel panel;
private JButton button;
private JTextArea textArea;
private JCheckBox musicBox;
private JCheckBox danceBox;
private JRadioButton hanButton;
private JRadioButton manButton;
private JRadioButton huiButton;
private ButtonGroup buttonGroup;
public JFrameDemo()
{
panel = new JPanel();
button = new JButton("确定");
textArea = new JTextArea(40,30);
musicBox = new JCheckBox("唱歌");
danceBox = new JCheckBox("跳舞");
huiButton = new JRadioButton("回族");
hanButton = new JRadioButton("汉族");
manButton = new JRadioButton("满族");
buttonGroup = new ButtonGroup();
buttonGroup.add(huiButton);
buttonGroup.add(hanButton);
buttonGroup.add(manButton);
panel.setLayout(new FlowLayout(3));
panel.add(huiButton);
panel.add(hanButton);
panel.add(manButton);
panel.add(musicBox);
panel.add(danceBox);
panel.add(button);
panel.add(textArea);
add(panel);
setTitle("选择兴趣爱好");
setBounds(100, 100, 400, 280);
setResizable(false);
setVisible(true);
this.button.addActionListener(this);
}
public static void main(String[] args)
{
new JFrameDemo();
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == this.button)
{
String info = "";
if(this.huiButton.isSelected())
{
info += this.huiButton.getText() + "\n";
}
if(this.hanButton.isSelected())
{
info += this.hanButton.getText() + "\n";
}
if(this.manButton.isSelected())
{
info += this.manButton.getText() + "\n";
}
if(this.danceBox.isSelected())
{
info += this.danceBox.getText() + "\n";
}
if(this.musicBox.isSelected())
{
info += this.musicBox.getText() + "\n";
}
this.textArea.setText(info);
}
}
}
有问题欢迎提问,满意请采纳,谢谢!
/**
假设在周末舞会上,男士们(m人)和女士们(n人)进入舞厅时,各自排成一队。跳舞开始时,依次从男队和女队的队头上各出一人配成舞伴。若两队初始人数不相同,则较长的那一队中未配对者等待下一轮舞曲。现要求写一算法模拟上述舞伴配对问题。并m和n存在什么条件时,第x个(1 =x =m)男生才有可能和他心仪的第y个(1 =x =n)女生跳舞,在第几首曲子时?
*/
import java.util.ArrayList;
import java.util.List;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Dance{
private List String boy=new ArrayList String();
private List String girl=new ArrayList String();
public Dance(int m,int n){
init(m,n);
}
public void init(int m,int n){
for(int i=0;i m;i++){
boy.add("man"+i);
}
for(int i=0;i n;i++){
girl.add("girl"+i);
}
}
public int getMin(){
return Math.min(boy.size(),girl.size());
}
public void go(){
String b="man1";
String g="girl2";
int count=0;
int i=0;
boolean bool=true;
while(bool){
if(i%getMin()==0)
System.out.println("第"+(++count)+"次:");
boolean bGirl=girl.get(i%girl.size()).equals(g);
boolean bBoy=boy.get(i%boy.size()).equals(b);
bool=!(bGirlbBoy);
System.out.println((boy.get((i)%boy.size())).toString());
System.out.println((girl.get((i)%girl.size())).toString());
i++;
}
System.out.println(b+"要在"+count+"首歌中才可以和"+g+"一起Dancing");
}
public static void main(String [] args)throws Exception{
int m;
int n;
System.out.println("请输入男士人数:");
m=input();
System.out.println("请输入女士人数:");
n=input();
Dance dance=new Dance(m,n);
dance.go();
}
public static int input()throws Exception{
int returnNum=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String line=br.readLine();
while(!line.equalsIgnoreCase("exit")){
try{
returnNum=new Integer(line);
if(returnNum 0) throw new Exception();
break;
}catch(Exception e){
System.out.println("输入数据有误!请重新输入,退出请按exit");
line=br.readLine();
}
}
return returnNum;
}
}
6 5 1 2 结果才是6;男排 女排 男序号 女序号
。。。。。。。。。。。。。。。。。。。。。。。。。
package net;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入男排人数:");
int boyArray = sc.nextInt();
System.out.println("请输入女排人数:");
int girlArray = sc.nextInt();
System.out.println("请输入男生序号:");
int boy = sc.nextInt();
System.out.println("请输入女生序号:");
int girl = sc.nextInt();
int count=0;
for(int i=1;i=boyArray*girlArray;i++){
if(iboyArray||igirlArray){
if(i==boyi==girl){
count=i;
System.out.println("该对男女第一次牵手跳舞的序号是a:"+i);
break;
}
}
else if((i%boyArray==boy)(i%girlArray==girl)){
count=i;
System.out.println("该对男女第一次牵手跳舞的序号是:"+i);
break;
}
}
if(count==0){
System.out.println("该对男女不可能一起跳舞!");
}
}
}
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流