设有n个人围坐一圈并按顺时针方向从1到n编号,从第1个人开始进行1到m的报数,报数到第个m人,此人出圈,再从他的下一个人重新开始1到m的报数,如此进行下去直到所剩下一人为止。
package 网赛;
import java.util.*;
public class 出圈 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
List<Integer>list=new ArrayList<Integer>();
int k=0;
for(int i=1;i<=n;i++) {
list.add(i);
}
while(list.size()>0) {
if(list.size()==1) {
System.out.println(list.get(list.size() - 1));
}
k = k + m;
k = k % (list.size()) - 1;
if (k < 0) {
list.remove(list.size() - 1);
k = 0;
} else {
list.remove(k);
}
}
}
}
一条单链表可以表示一个一元多项式,每个节点包含三个域:指数、系数和后继节点(指针或引用)。
表示多项式3X4-6X2+5X-10的单链表如图所示。给定两个多项式,实现两个多项式相加算法。
import java.util.Scanner;
public class 多项式相加 {
public static class Term{
//系数
private int coef;
//指数
private int expn;
public Term next;
public Term() {
this(0,0);
}
public Term(int coef, int expn) {
this.coef = coef;
this.expn = expn;
}
public int getCoef() {
return coef;
}
public void setCoef(int coef) {
this.coef = coef;
}
public int getExpn() {
return expn;
}
public void setExpn(int expn) {
this.expn = expn;
}
}
public static class PolyList{
Term head;
Term current;
public PolyList() {
head =new Term();
current =head;
head.next=null;
}
public boolean isEmpty() {
return head.next == null;
}
public void insert(Term term) {
current.next=term;
current =term;
}
public static PolyList addPoly(PolyList p, PolyList q) {
Term pnext = p.head.next;
Term qnext = q.head.next;
PolyList result = new PolyList();
while (pnext != null && qnext != null) {
int pexpn = pnext.getExpn();
int qexpn = qnext.getExpn();
int pcoef = pnext.getCoef();
int qcoef = qnext.getCoef();
if (pexpn == qexpn) {
if (pcoef+qcoef != 0) {
Term node = new Term(pcoef + qcoef, pexpn);
result.insert(node);
}
pnext = pnext.next;
qnext = qnext.next;
}else if(pexpn < qexpn){
Term node = new Term(pnext.getCoef(), pnext.getExpn());
result.insert(node);
pnext = pnext.next;
}else{
Term node = new Term(qnext.getCoef(), qnext.getExpn());
result.insert(node);
qnext = qnext.next;
}
}
while (pnext != null) {
Term node = new Term(pnext.getCoef(), pnext.getExpn());
result.insert(node);
pnext = pnext.next;
}
while (qnext != null) {
Term node = new Term(qnext.getCoef(), qnext.getExpn());
result.insert(node);
qnext = qnext.next;
}
return result;
}
public void output(){
Term node = head.next;
while (node != null) {
System.out.println(node.getCoef() + " "+node.getExpn());
node = node.next;
}
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
PolyList p1=new PolyList();
PolyList p2=new PolyList();
for(int i=0;i<n;i++) {
p1.insert(new Term(sc.nextInt(),sc.nextInt()));
}
for(int i=0;i<m;i++) {
p2.insert(new Term(sc.nextInt(),sc.nextInt()));
}
PolyList result= PolyList.addPoly(p1, p2);
result.output();
}
}
若一个数(首位不为零)从左向右读与从右向左读都一样,我们就将其称之为回文数。
例如:给定一个10进制数56,将56加65(即把56从右向左读),得到121是一个回文数。
又如:对于10进制数87:
STEP1:87+78 = 165 STEP2:165+561 = 726
STEP3:726+627 = 1353 STEP4:1353+3531 = 4884
在这里的一步是指进行了一次N进制的加法,上例最少用了4步得到回文数4884。
写一个程序,给定一个N(2<=N<=16)进制数M,求最少经过几步可以得到回文数。如果在30步以内(包含30步)不可能得到回文数,则输出“Impossible!”
package 网赛;
import java.util.*;
public class 回文式 {
static String ma = "0123456789ABCDEF";
//判断是否为回文数
public static boolean judge(String str) {
for(int i=0;i<str.length()/2;i++) {
if(str.charAt(i)!=str.charAt(str.length()-1-i)) {
return false;
}
}
return true;
}
//反转
public static String Reversal(String str) {
StringBuffer sb=new StringBuffer();
for(int i=str.length()-1;i>=0;i--) {
sb.append(String.valueOf(str.charAt(i)));
}
return sb.toString();
}
public static String addstr(String a,String b,int n) {
int len=a.length();
char []a1=a.toCharArray();
char []b1=b.toCharArray();
int tmp=0;
StringBuffer sb=new StringBuffer();
for(int i=0; i<len; i++) {
int m = ma.indexOf(String.valueOf(a1[i]))+ma.indexOf(String.valueOf(b1[i]))+tmp;
if(m>=n) {
tmp = m/n;
m = m%n;
}else {
tmp = 0;
}
sb.append(String.valueOf(ma.charAt(m)));
}
if(tmp!=0) {
sb.append(String.valueOf(ma.charAt(tmp)));
}
return Reversal(sb.toString());
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int m = sc.nextInt();
String s = sc.next();
sc.close();
int count = 0;
while(!judge(s) && count<=30) {
s = addstr(s, Reversal(s),m);
count++;
}
if(judge(s)) {
System.out.println("STEP="+count);
}else {
System.out.println("Impossible!");
}
}
}