1.
观察数字:12321,123321 都有一个共同的特征,无论从左到右读还是从右向左读,都是相同的。
这样的数字叫做:回文数字。
本题要求你找到一些5位或6位的十进制数字。满足如下要求:
该数字的各个数位之和等于输入的整数。一个正整数 n (10< n< 100), 表示要求满足的数位和。
输入:
若干行,每行包含一个满足要求的5位或6位整数。
数字按从小到大的顺序排列。
如果没有满足条件的,输出:-1
比如:44
输出:
99899
499994
589985
598895
679976
688886
697796
769967
778877
787787
796697
859958
868868
877778
886688
895598
949949
958859
967769
976679
985589
994499
import java.util.Scanner;
public class Main1 {
static int huishu = 0;
public static void main(String []args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
F(sc.nextInt());
}
sc.close();
}
private static void F(int n){
int count = 0;
//5位或6位的截取一般都是3位数: 循环次数为 (29)*2 = 58
//寻找5位的
for(int a = 1; a <= 9; a++){
for(int b = 0; b <= 9; b++){
for(int c = 0; c <= 9; c++){
huishu++;
if(2*a + 2*b + c == n){
huishu += 2;
System.out.println("" + a + b + c + b + a);
count++;
}
}
}
}
//寻找6位的
for(int a = 1; a <= 9; a++){
for(int b = 0; b <= 9; b++){
for(int c = 0; c <= 9; c++){
huishu++;
if(2*(a+b+c) == n){
huishu += 2;
System.out.println("" + a + b + c + c + b + a);
count++;
}
}
}
}
if(count == 0)
System.out.println(-1);
//一共要进行的程序的执行次数
System.out.println(huishu);
}
}
执行效率还是比较快的.
2
用栈的思想实现加乘表达式之和
输入:
234+121+432
输出50
import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void main(String [] args) {
Scanner in = new Scanner(System.in);
String str = in.next();
Stack<String> fuhao = new Stack<String>();
Stack<Integer> shuzi = new Stack<Integer>();
for(int i = 0;i<str.length();i++) {
String c = String.valueOf(str.charAt(i));
if(c.equals("+") ) {fuhao.push(c);}
if(c.equals("*")) {
String next = String.valueOf(str.charAt(i+1));
Integer nextn = Integer.valueOf(next);
Integer chengji = nextn*shuzi.pop();
shuzi.push(chengji);
i++;
}
if(!(c.equals("+")||c.equals("*"))) {
shuzi.push(Integer.valueOf(c));
}
}
while(!fuhao.empty()) {
fuhao.pop();
Integer a = shuzi.pop();
Integer sum =a +shuzi.pop();
shuzi.push(sum);
}
System.out.println(shuzi.lastElement());
}
}
3.
栋栋正在和同学们玩一个数字游戏。
游戏的规则是这样的:栋栋和同学们一共n个人围坐在一圈。栋栋首先说出数字1。接下来,坐在栋栋左手边的同学要说下一个数字2。再下面的一个同学要从上一个同学说的数字往下数两个数说出来,也就是说4。下一个同学要往下数三个数,说7。依次类推。
为了使数字不至于太大,栋栋和同学们约定,当在心中数到 k-1 时,下一个数字从0开始数。例如,当k=13时,栋栋和同学们报出的前几个数依次为:
1, 2, 4, 7, 11, 3, 9, 3, 11, 7。
游戏进行了一会儿,栋栋想知道,到目前为止,他所有说出的数字的总和是多少。
样例说明
栋栋说出的数依次为1, 7, 9,和为17。
数据规模和约定
1 < n,k,T < 1,000,000;
public class Main {
public static void main(String []args) {
Scanner in = new Scanner(System.in);
long n = in.nextLong();
long k = in.nextLong();
long T = in.nextLong();
long j = T;
long add = 0;
for(long i = 1;j>0;i += n) {
long shuzi =((i*i-i+2)/2)%k ;
add += shuzi;
j--;
}
System.out.println(add);
}
}
4.
长100厘米的细长直杆子上有n只蚂蚁。它们的头有的朝左,有的朝右。
每只蚂蚁都只能沿着杆子向前爬,速度是1厘米/秒。
当两只蚂蚁碰面时,它们会同时掉头往相反的方向爬行。
这些蚂蚁中,有1只蚂蚁感冒了。并且在和其它蚂蚁碰面时,会把感冒传染给碰到的蚂蚁。
请你计算,当所有蚂蚁都爬离杆子时,有多少只蚂蚁患上了感冒
第一行输入一个整数n (1 < n < 50), 表示蚂蚁的总数。
接着的一行是n个用空格分开的整数 Xi (-100 < Xi < 100), Xi的绝对值,
表示蚂蚁离开杆子左边端点的距离。正值表示头朝右,负值表示头朝左,数据中不会出现0值,
也不会出现两只蚂蚁占用同一位置。其中,第一个数据代表的蚂蚁感冒了。
要求输出1个整数,表示最后感冒蚂蚁的数目。
输入:
5
-10 8 -20 12 25
输出:
3
解题思路:
(1)当首只蚂蚁向右走,则它右边所有向左蚂蚁都感冒;若它右边有向左蚂蚁感冒,则它左边所有向右蚂蚁都感冒。
(2)当首只蚂蚁向左走,则它左边所有向右蚂蚁都感冒;若它左边有向右蚂蚁感冒,则它右边所有向左蚂蚁都感冒。
package com.wyc.cxsj;
import java.util.Scanner;
public class Main {
public static int abs(int a) {
if(a < 0 ) { return -a;}
return a;
}
public static void main(String []args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int a = in.nextInt(); // 感冒的蚂蚁
int sum = 1;
boolean isganmao = false;
int c = 0;
for(int i = 1;i<n;i++) {
int b = in.nextInt();
//第一种情况 , 感冒蚂蚁向左走,那左边所有向右走的都感冒了,如果有左边向右走的蚂蚁感冒了
//右边所有向左走的都感冒了
if(a<0) {
if(abs(b)<-a && b>0) {
sum++;
isganmao = true;
}
if(abs(b)>-a && b<0) {
c++;
}
}
//第二种情况
if(a>0) {
if(abs(b)>a && b<0) {
sum++;
isganmao = true;
}
if(abs(b)<a && b>0) {
c++;
}
}
}
int add = (isganmao ?sum+c:sum);
System.out.println(add);
}
}