1010. 一元多项式求导
假设系数为a,指数为b;需要输出a*b+" "+(b-1),如果输入的就是零多项式(即 a 0),那么就直接输出“0 0”。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int cnt = 0;
while(sc.hasNext()){
int n = sc.nextInt();
int m = sc.nextInt();
if(m != 0){
if(cnt == 0)
System.out.print(n*m+" "+(m-1));
else if(cnt != 0)
System.out.print(" "+n*m+" "+(m-1));
cnt++;
}
}
if(cnt == 0){
System.out.print("0 0");
}
}
}
1011.A+B和C
利用数组记录数据,注意要使用long类型,然后遍历运算比较按情况输出结果
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
long[][] a = new long[N][3];
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
a[i][j] = sc.nextInt();
}
}
for (int i = 0; i < a.length; i++) {
if(a[i][0]+a[i][1]>a[i][2]){
if(i==a.length-1)
System.out.print("Case #"+(i+1)+": true");
else
System.out.println("Case #"+(i+1)+": true");
}else
if(i==a.length-1)
System.out.print("Case #"+(i+1)+": false");
else
System.out.println("Case #"+(i+1)+": false");
}
}
}
1012.数字分类
判断出各个数字都属于哪种情况,分别输出相应的结果
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int A1 = 0, A2 = 0, A3 = 0, A4 = 0, A5 = 0, count = 0;
boolean isMinus = true, isNotExist = true;
int N = s.nextInt();
for (int i = 0; i < N; i++) {
int num = s.nextInt();
if (num % 5 == 0 && num % 2 == 0) A1 += num;
if (num % 5 == 1) {
if (isMinus) A2 += num;
else A2 -= num;
isMinus = !isMinus;
isNotExist = false;
}
if (num % 5 == 2) A3++;
if (num % 5 == 3) {
A4 += num;
count++;
}
if (num % 5 == 4 && num > A5) A5 = num;
}
String print = (A1 == 0) ? "N " : A1+" ";
print += (isNotExist) ? "N " : A2+" ";
print += (A3 == 0) ? "N " : A3+" ";
print += (A4 == 0) ? "N " : new DecimalFormat("0.0").format(A4*1.0/count) + " ";
print += (A5 == 0) ? "N" : A5 + "";
System.out.println(print);
}
}
1006.Sign in and Sign Out
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int min=99999999;
int max=0;
String unlock=null;
String lock=null;
int n=in.nextInt();
for(int i=0;i<n;i++){
String IdNum=in.next();
String SiTime=in.next();
String SoTime=in.next();
int hour1=Integer.parseInt(SiTime.substring(0,2));
int minute1=Integer.parseInt(SiTime.substring(3,5));
int second1=Integer.parseInt(SiTime.substring(6,8));
int hour2=Integer.parseInt(SoTime.substring(0,2));
int minute2=Integer.parseInt(SoTime.substring(3,5));
int second2=Integer.parseInt(SoTime.substring(6,8));
int intime=hour1*3600+minute1*60+second1;
int outtime=hour2*3600+minute2*60+second2;
if(intime<min){
min=intime;
unlock=IdNum;
}
if(outtime>max){
max=outtime;
lock=IdNum;
}
}
System.out.println(unlock+" "+lock);
}
}
1007.Maximum Subsequence Sum
设置一个临时的和temp,初始化为0,它的下标为tempindex,当temp < 0,那么后面不管来什么值,都应该舍弃temp < 0前面的内容,因为负数对于总和只可能拉低总和,不可能增加总和,还不如舍弃,舍弃后,直接令temp = 0,并且同时更新left的临时值tempindex。如果temp>sum,则更新sum,left,right值。如果序列所有值都为负数,因为sum初始化为-1,判断sum<0,更新sum=0。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String n=in.readLine();
int k=Integer.parseInt(n);
int temp=0;
int tempindex=0;
int left=0;
int right=k-1;
int sum=-1;
String []s=in.readLine().split(" ");
int a[]=new int [k];
for(int i=0;i<k;i++){
a[i]=Integer.parseInt(s[i]);
temp=temp+a[i];
if(temp>sum){
sum=temp;
left=tempindex;
right=i;
}else if(temp<0){
temp=0;
tempindex=i+1;
}
}
if(sum<0){
sum=0;
}
System.out.println(sum+" "+a[left]+" "+a[right]);
}
}
1002.Business
#include<bits/stdc++.h>
using namespace std;
struct task
{
int p,l,d;
bool operator< (const task et) const
{return d<et.d;}
};
int main()
{
// freopen("data.txt","r",stdin);
int n,m=0;
scanf("%d",&n);
vector<task> v(n);
for(int i=0;i<n;i++)
{
scanf("%d %d %d",&v[i].p,&v[i].l,&v[i].d);
m=max(v[i].d,m);
}
sort(v.begin(),v.end());
vector<int> vdp((n+1)*(m+1),0);
for(int i=1;i<n+1;i++)
{
for(int j=1;j<m+1;j++)
{
int t=min(j,v[i-1].d)-v[i-1].l;
if(t>=0) vdp[i*(m+1)+j]=max(vdp[(i-1)*(m+1)+j],vdp[(i-1)*(m+1)+t]+v[i-1].p);
else vdp[i*(m+1)+j]=vdp[(i-1)*(m+1)+j];
}
}
printf("%d",vdp[n*m+n+m]);
return 0;
}