B1009 说反话
给定一句英语,要求编写程序,将句中所有单词按颠倒顺序输出。
//个人
void reverse(){
char tmp,str[10][80];
int i=0,j=0;
while((tmp=getchar())!='\n'){
if(tmp==' '){
str[i][j] = '\0';
i++;
j=0;
}else{
str[i][j] = tmp;
j++;
}
}
str[i][j] = '\0';
for(;i>=0;i--){
printf("%s",str[i]);
if(i>0)
printf(" ");
}
printf("\n");
}
//参考
#include <cstdio>
#include <cstring>
int main(){
char str[90];
gets(str);
int len = strlen(str),r = 0,h = 0;
char ans[90][90];
for(int i = 0;i<len;i++){
if(str[i] != ' '){
ans[r][h++] = str[i];
}else{
ans[r][h++] = '\0';
r++;
h = 0;
}
}
for(int i = r;i >= 0;i--){
printf("%s", ans[i]);
if(x>0) printf(" ");
}
return 0;
}
B1016 部分A+B
正整数A的“Da(为1位整数)部分”定义为由A中所有Da组成的新整数Pa。例如给定A = 3862767,Da = 6,则A的“6部分”Pa是66,因为A中有2个6。
现给定A、Da、B、Db,请编写程序计算Pa+Pb。
//个人
void partAandB(){
char a[255],b[255];
char da,db;
long long pa = 0,pb = 0;
scanf("%s %c %s %c",a,&da,b,&db);
int ta,tb;
sscanf(&da,"%1d",&ta);
sscanf(&db,"%1d",&tb);
for(int i = 0;i<strlen(a);i++){
if(a[i] == da){
pa = pa*10 + ta;
}
}
for(int i = 0;i<strlen(b);i++){
if(b[i] == db){
pb = pb*10 + tb;
}
}
printf("%lld\n",pa+pb);
}
//参考
#include <cstdio>
int main(){
long long a,b,da,db;
scanf("%lld%lld%lld%lld",&a,&da,&b,&db);
long long pa = 0,pb = 0;
while(a != 0){
if(a%10 == da) pa = pa*10+da;
a = a/10;
}
while(b != 0){
if(b%10 == db) pb = pb*10 +db;
b = b/10;
}
printf("%lld\n",pa + pb);
return 0 ;
}
A1001 A+B Format
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()){
String numbers = scanner.nextLine();
String a = numbers.split(" ")[0];
String b = numbers.split(" ")[1];
int sum = Integer.parseInt(a) + Integer.parseInt(b);
String result = Integer.toString(Math.abs(sum));
int comma = (result.length()/3);
if (result.length() % 3 == 0)
comma--;
char[] res = new char[result.length()+comma];
int j = 0;
for (int i = result.length()-1; i>=0; i--){
++j;
res[i+comma] = result.charAt(i);
if (j%3 == 0 && i != 0){
--comma;
res[i+comma] = ',';
}
}
if (sum < 0)
System.out.print('-');
for (char re : res) {
System.out.print(re);
}
}
}
}
A1002 A+B for Polynomials
import java.util.Scanner;
import java.util.Map;
import java.util.HashMap;
import java.util.LinkedHashMap;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
Map<Integer, Double> map = new HashMap<>();
for (int i=0; i<2; i++){
int numA = sc.nextInt();
while (numA-- != 0){
int expo = sc.nextInt();
double coe = sc.nextDouble();
if (!map.containsKey(expo))
map.put(expo, coe);
else{
double t = map.get(expo);
map.put(expo, coe+t);
}
}
}
Map<Integer, Double> result = new LinkedHashMap<>();
map.entrySet().stream().sorted(Map.Entry.<Integer, Double>comparingByKey().reversed()).forEachOrdered(x -> result.put(x.getKey(), x.getValue()));
int i = 0;
for (double value: result.values()){
if (value != 0)
i++;
}
System.out.print(i);
for (Map.Entry<Integer, Double> entry: result.entrySet()){
if (entry.getValue() != 0.0){
System.out.printf(" %d %.1f",entry.getKey(), entry.getValue());
}
}
}
}
A1003 Emergency
#include <iostream>
#include <cstdio>
#include <climits>
#include <algorithm>
using namespace std;
int main(){
int city, road, from, to;
cin>>city>>road>>from>>to;
int i, j, rescues[city], froms[road], tos[road], roads[road];
for (i = 0; i < city; i++)
cin>>rescues[i];
for (i = 0; i < road; i++)
cin>>froms[i]>>tos[i]>>roads[i];
int path[city]{}, vis[city]{}, pathnum[city]{}, maxrescue[city]{};
fill(path, path+city, INT_MAX);
path[from] = 0, pathnum[from] = 1, maxrescue[from] = rescues[from];
for (i = 0; i < city; i++){
int min = INT_MAX, u = -1;
for (j = 0; j < city; j++){
if (!vis[j] && path[j]<min){
min = path[j];
u = j;
}
}
if (u == -1) break;
vis[u] = 1;
for (j = 0; j < road; j++){
if (froms[j] == u && !vis[tos[j]]){
if (roads[j] + path[u] < path[tos[j]]){
path[tos[j]] = roads[j] + path[u];
pathnum[tos[j]] = pathnum[u];
maxrescue[tos[j]] = maxrescue[u] + rescues[tos[j]];
}else if (roads[j] + path[u] == path[tos[j]]){
pathnum[tos[j]] += pathnum[u];
if (maxrescue[tos[j]] < maxrescue[u] + rescues[tos[j]])
maxrescue[tos[j]] = maxrescue[u] + rescues[tos[j]];
}
}
if (tos[j] == u && !vis[froms[j]]){
if (roads[j] + path[u] < path[froms[j]]){
path[froms[j]] = roads[j] + path[u];
pathnum[froms[j]] = pathnum[u];
maxrescue[froms[j]] = maxrescue[u] + rescues[froms[j]];
}else if (roads[j] + path[u] == path[froms[j]]){
pathnum[froms[j]] += pathnum[u];
if (maxrescue[froms[j]] < maxrescue[u] + rescues[froms[j]])
maxrescue[froms[j]] = maxrescue[u] + rescues[froms[j]];
}
}
}
}
cout<<pathnum[to]<<" "<<maxrescue[to];
return 0;
}
A1004 Counting Leaves
// DFS method
import java.util.Scanner;
import java.util.Set;
import java.util.HashSet;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
public class Main {
static int[] leaves = new int[100];
static int maxlevel = 0;
static List<HashSet<Integer>> child = new ArrayList<>();
public static void main(String[] args) {
for (int i=0; i<100; i++){
HashSet<Integer> set = new HashSet<>();
child.add(set);
}
Scanner in = new Scanner(System.in);
int N, M;
N = in.nextInt();
M = in.nextInt();
for (int line=0; line<M; line++){
int node = in.nextInt();
HashSet<Integer> set = new HashSet<>();
int num = in.nextInt();
for (int i=0; i<num; i++)
set.add(in.nextInt());
child.remove(node-1);
child.add(node-1, set);
}
DFS(1, 1);
for (int i=1; i<=maxlevel; i++){
if (i == 1)
System.out.print(leaves[i]);
else
System.out.print(" "+leaves[i]);
}
}
public static void DFS(int id, int level){
if (child.get(id-1).size() == 0){
leaves[level]++;
if (maxlevel < level)
maxlevel = level;
}
for (int newId: child.get(id-1))
DFS(newId, level+1);
}
}
// traverse method
import java.util.Scanner;
import java.util.Set;
import java.util.HashSet;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
public class Main {
static int[] leaves = new int[100];
static List<HashSet<Integer>> child = new ArrayList<>();
public static void main(String[] args) {
for (int i=0; i<100; i++){
HashSet<Integer> set = new HashSet<>();
child.add(set);
}
Scanner in = new Scanner(System.in);
int N, M;
N = in.nextInt();
M = in.nextInt();
for (int line=0; line<M; line++){
int node = in.nextInt();
HashSet<Integer> set = new HashSet<>();
int num = in.nextInt();
for (int i=0; i<num; i++)
set.add(in.nextInt());
child.remove(node-1);
child.add(node-1, set);
}
int[] levels = new int[101], nodes = new int[101], tags = new int[101];
int k = 0;
nodes[1] = 1;
while (k != M){
for (int j=0; j<100; j++){
if (tags[j] == 1) continue;
if (nodes[j+1] == 0 || child.get(j).isEmpty()) continue;
for (int id: child.get(j)){
nodes[id] = nodes[j+1] + 1;
}
tags[j] = 1;
levels[nodes[j+1]]++;
k++;
}
}
boolean end = false;
for (int i=1; i<101; i++){
if (end) break;
if (levels[i] == 0) end = true;
int n = 0;
for (int j=1; j<101; j++){
if (nodes[j] == i)
n++;
}
if (i == 1)
System.out.print(n-levels[i]);
else
System.out.print(" "+(n-levels[i]));
}
}
}
A1005 Spell It Right
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String digit = in.next();
char[] digits = digit.toCharArray();
int sum = 0;
for (int i=0; i<digits.length; i++){
sum += digits[i]-48;
}
char[] sums = String.valueOf(sum).toCharArray();
for (int i=0; i<sums.length; i++){
switch (sums[i]){
case '0': System.out.print("zero");break;
case '1': System.out.print("one");break;
case '2': System.out.print("two");break;
case '3': System.out.print("three");break;
case '4': System.out.print("four");break;
case '5': System.out.print("five");break;
case '6': System.out.print("six");break;
case '7': System.out.print("seven");break;
case '8': System.out.print("eight");break;
case '9': System.out.print("nine");
}
if (i != sums.length-1)
System.out.print(" ");
}
}
}
A1006 Sign In and Sign Out
// Method one
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int M = in.nextInt(), min = Integer.MAX_VALUE, max = 0, first = 0, last = 0;
String[] ids = new String[M], ins, outs;
for (int i=0; i<M; i++){
ids[i] = in.next();
ins = in.next().split(":");
outs = in.next().split(":");
int intime = Integer.parseInt(ins[0])*60*60+Integer.parseInt(ins[1])*60+Integer.parseInt(ins[2]);
int outtime = Integer.parseInt(outs[0])*60*60+Integer.parseInt(outs[1])*60+Integer.parseInt(outs[2]);
if (intime < min){
min = intime;
first = i;
}
if (outtime > max){
max = outtime;
last = i;
}
}
System.out.println(ids[first] + " " + ids[last]);
}
}
// Method two
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int M = in.nextInt(), first = 0, last = 0;
String[] ids = new String[M], ins = new String[M], outs = new String[M];
for (int i=0; i<M; i++){
ids[i] = in.next();
ins[i] = in.next();
outs[i] = in.next();
if (ins[i].compareTo(ins[first]) < 0)
first = i;
if (outs[i].compareTo(outs[last]) > 0)
last = i;
}
System.out.println(ids[first] + " " + ids[last]);
}
}
A1007 Maximum Subsequence Sum
// Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int K = in.nextInt();
int[] numbers = new int[K];
for (int i=0; i<K; i++)
numbers[i] = in.nextInt();
int start = 0, end = 0, max = Integer.MIN_VALUE;
boolean allneg = true;
for (int i=0; i<K; i++){
if (numbers[i] >= 0) allneg = false;
int sum = 0;
for (int j=i; j<K; j++){
sum += numbers[j];
if (sum > max){
max = sum;
start = i;
end = j;
}
}
}
if (allneg)
System.out.print(0 + " " + numbers[0] + " " + numbers[K-1]);
else
System.out.print(max + " " + numbers[start] + " " + numbers[end]);
}
}
#include <cstdio>
#include <iostream>
#include <climits>
using namespace std;
int main() {
int K;
cin>>K;
int numbers[K]{};
for (int i=0; i<K; i++)
cin>>numbers[i];
int start = 0, end = 0, max = INT_MIN;
bool allneg = true;
for (int i=0; i<K; i++){
if (numbers[i] >= 0) allneg = false;
int sum = 0;
for (int j=i; j<K; j++){
sum += numbers[j];
if (sum > max){
max = sum;
start = i;
end = j;
}
}
}
if (allneg)
cout<<0<<" "<<numbers[0]<<" "<<numbers[K-1];
else
cout<<max<<" "<<numbers[start]<<" "<<numbers[end];
}
A1008 Elevator
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
int[] floors = new int[N];
for (int i=0; i<N; i++)
floors[i] = in.nextInt();
int sumtime = 0;
sumtime += floors[0]*6;
for (int i=0; i<N-1; i++){
if (floors[i]>floors[i+1])
sumtime += (floors[i]-floors[i+1])*4;
else
sumtime += (floors[i+1]-floors[i])*6;
}
sumtime += N*5;
System.out.print(sumtime);
}
}
A1009 Product of Polynomials
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int K1 = in.nextInt();
double[] pA = new double[1001], pB = new double[1001];
double[] res = new double[2001];
for (int i=0; i<K1; i++){
int ex = in.nextInt();
pA[ex] = in.nextDouble();
}
int K2 = in.nextInt();
for (int i=0; i<K2; i++){
int ex = in.nextInt();
pB[ex] = in.nextDouble();
}
for (int i=0; i<1001; i++){
if (pA[i] != 0){
for (int j=0; j<1001; j++){
if (pB[j] != 0)
res[i+j] += pA[i]*pB[j];
}
}
}
int num = 0;
for (int i=0; i<2001; i++)
if (res[i] != 0)
num++;
System.out.print(num);
for (int i=2000; i>=0; i--)
if (res[i] != 0)
System.out.printf(" %d %.1f", i, res[i]);
}
}
A1010 Radix
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String da = in.next(), db = in.next();
int tag = in.nextInt();
long radix = in.nextLong();
long r = tag==1 ? getRadix(getNumber(da, radix), db, getMax(db), getNumber(da, radix)+1)
: getRadix(getNumber(db, radix), da, getMax(da), getNumber(db, radix)+1) ; // 1 1 1 10
if (r != 0)
System.out.print(r);
else
System.out.print("Impossible");
}
public static long getNumber(String digit, long radix){
long num = 0;
for (int i=0; i<digit.length(); i++){
char c = digit.charAt(i);
if (c <= '9')
num = num*radix + (c-'0');
else
num = num*radix + (c-'a'+10);
}
return num;
}
public static long getMax(String digit){
char max = '0';
for (int i=0; i<digit.length(); i++)
if (digit.charAt(i) > max)
max = digit.charAt(i);
if (max <= '9') return max-'0'+1;
return max-'a'+11;
}
public static long getRadix(long a, String db, long start, long end){
if (start == end){
if (getNumber(db, start) == a)
return start;
}else if (start < end){
long radix = (start + end)/2;
if (getNumber(db, radix) == a)
return radix;
else if (getNumber(db, radix) > a || getNumber(db, radix) < 0)
return getRadix(a, db, start, radix-1);
else
return getRadix(a, db, radix+1, end);
}
return 0;
}
}