问题 A: 剩下的树

时间限制: 1 Sec  内存限制: 32 MB

题目描述

有一个长度为整数L(1<=L<=10000)的马路,可以想象成数轴上长度为L的一个线段,起点是坐标原点,在每个整数坐标点有一棵树,即在0,1,2,...,L共L+1个位置上有L+1棵树。
    现在要移走一些树,移走的树的区间用一对数字表示,如 100 200表示移走从100到200之间(包括端点)所有的树。
    可能有M(1<=M<=100)个区间,区间之间可能有重叠。现在要求移走所有区间的树之后剩下的树的个数。

输入

两个整数L(1<=L<=10000)和M(1<=M<=100)。
    接下来有M组整数,每组有一对数字。

输出

 可能有多组输入数据,对于每组输入数据,输出一个数,表示移走所有区间的树之后剩下的树的个数。

样例输入

4 2
1 2
0 2
11 2
1 5
4 7
0 0

样例输出

2
5

代码:

#include <iostream>
#include<algorithm>
using namespace std;

const int MAXN=10010;
int tree[MAXN];


int main()
{
    int L,M;
    int a,b;
    while(cin>>L>>M){
        if(L==0&&M==0){
            break;
        }
        fill(tree,tree+L+1,1);
        for(int i=0;i<M;i++){
            cin>>a>>b;
            for(int i=a;i<=b;i++){
                if(tree[i]){
                    tree[i]=0;
                }
            }
        }
        int sum=0;
        for(int i=0;i<=L;i++){
            sum+=tree[i];
        }
        cout<<sum<<endl;
    }
    return 0;
}

 

 

问题 B: A+B

时间限制: 1 Sec  内存限制: 32 MB

题目描述

给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开。
现在请计算A+B的结果,并以正常形式输出。

输入

输入包含多组数据数据,每组数据占一行,由两个整数A和B组成(-10^9 < A,B < 10^9)。

输出

请计算A+B的结果,并以正常形式输出,每组数据占一行。

样例输入

-234,567,890 123,456,789
1,234 2,345,678

样例输出

-111111101
2346912

代码:

#include <iostream>
#include<cstdio>
#include<string>
using namespace std;

typedef long long ll;
string s1,s2;

ll toLong(string s){
    ll ans;
    char a[20];
    int len=s.length();
    int k=0;
    for(int i=0;i<len;i++){
        if(s[i]!=','){
            a[k++]=s[i];
        }
    }
    a[k]='\0';
    sscanf(a,"%lld",&ans);//codeblock 波浪线报错 有时不用管他
    return ans;
}

int main()
{
    while(cin>>s1>>s2){
        cout<<toLong(s1)+toLong(s2)<<endl;
    }
    return 0;
}

 

 

问题 C: 特殊乘法

时间限制: 1 Sec  内存限制: 32 MB

题目描述

写个算法,对2个小于1000000000的输入,求结果。特殊乘法举例:123 * 45 = 1*4 +1*5 +2*4 +2*5 +3*4+3*5

输入

 两个小于1000000000的数

输出

 输入可能有多组数据,对于每一组数据,输出Input中的两个数按照题目要求的方法进行运算后得到的结果。

样例输入

24 65
42 66666
3 67

样例输出

66
180
39

代码:

#include <iostream>
#include<string>
using namespace std;
typedef long long ll;
string s1,s2;

ll Sum(string s){
    ll res=0;
    for(int i=0;i<s.length();i++){
        res+=(s[i]-'0');
    }
    return res;
}


int main()
{
    while(cin>>s1>>s2){
        cout<<Sum(s1)*Sum(s2)<<endl;
    }

    return 0;
}

 

 

问题 D: 比较奇偶数个数

时间限制: 1 Sec  内存限制: 32 MB

题目描述

第一行输入一个数,为n,第二行输入n个数,这n个数中,如果偶数比奇数多,输出NO,否则输出YES。

输入

输入有多组数据。
每组输入n,然后输入n个整数(1<=n<=1000)。

 

输出

如果偶数比奇数多,输出NO,否则输出YES。

 

样例输入

1
67
7
0 69 24 78 58 62 64

样例输出

YES
NO

代码:

#include <iostream>
#include<cstring>
using namespace std;
int main()
{
    int n;
    int a[1010];
    int even,odd;
    while(cin>>n){
        memset(a,0,sizeof(a));
        even=0;
        odd=0;
        for(int i=0;i<n;i++){
            cin>>a[i];
            if(a[i]%2==0){//even
                even++;
            }else{
                odd++;
            }
        }
        if(even>odd){
            cout<<"NO"<<endl;
        }else{
            cout<<"YES"<<endl;
        }

    }
    return 0;
}

 

问题 E: Shortest Distance (20)

时间限制: 1 Sec  内存限制: 32 MB

题目描述

The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits.

输入

Each input file contains one test case. For each case, the first line contains an integer N (in [3, 105]), followed by N integer distances D1 D2 ... DN, where Di is the distance between the i-th and the (i+1)-st exits, and DN is between the N-th and the 1st exits. All the numbers in a line are separated by a space. The second line gives a positive integer M (<=104), with M lines follow, each contains a pair of exit numbers, provided that the exits are numbered from 1 to N. It is guaranteed that the total round trip distance is no more than 107.

输出

For each test case, print your results in M lines, each contains the shortest distance between the corresponding given pair of exits.

样例输入

5 1 2 4 14 9
3
1 3
2 5
4 1

样例输出

3
10
7

代码:

#include <iostream>
#include<cstring>
using namespace std;
int D[100010];

int sum(int s,int e){
    int Sum=0;
    for(int i=s;i<e;i++){
        Sum+=D[i];
    }
    return Sum;
}

int main()
{
    int N,M;
    while(cin>>N){
        memset(D,0,sizeof(D));
        for(int i=1;i<=N;i++){
            cin>>D[i];
        }
        cin>>M;
        int a=0,b=0;
        int all=sum(1,N+1);//总路径长度
        int s1,s2;
        while(M--){
            cin>>a>>b;
            if(a>b){int t=a;a=b;b=t;}
            s1=sum(a,b);
            s2=all-s1;//走另一条路
            cout<<(s1<s2?s1:s2)<<endl;
        }


    }
    return 0;
}

 

 

问题 F: A+B和C (15)

时间限制: 1 Sec  内存限制: 32 MB

题目描述

给定区间[-231, 231]内的3个整数A、B和C,请判断A+B是否大于C。

输入

输入第1行给出正整数T(<=10),是测试用例的个数。随后给出T组测试用例,每组占一行,顺序给出A、B和C。整数间以空格分隔。

输出

对每组测试用例,在一行中输出“Case #X: true”如果A+B>C,否则输出“Case #X: false”,其中X是测试用例的编号(从1开始)。

样例输入

4
1 2 3
2 3 4
2147483647 0 2147483646
0 -2147483648 -2147483647

样例输出

Case #1: false
Case #2: true
Case #3: true
Case #4: false

代码:

#include <iostream>

using namespace std;
typedef long long ll;

int main()
{
    ll A,B,C;
    int count=1;
    short T;
    while(cin>>T){
        while(T--){
            cin>>A>>B>>C;
            if(A+B>C){
                cout<<"Case #"<<(count++)<<": true\n";
            }else{
                cout<<"Case #"<<(count++)<<": false\n";
            }
        }

    }

    return 0;
}

 

 

问题 G: 数字分类 (20)

时间限制: 1 Sec  内存限制: 32 MB

题目描述

给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字:

  • A1 = 能被5整除的数字中所有偶数的和;
  • A2 = 将被5除后余1的数字按给出顺序进行交错求和,即计算n1-n2+n3-n4...;
  • A3 = 被5除后余2的数字的个数;
  • A4 = 被5除后余3的数字的平均数,精确到小数点后1位;
  • A5 = 被5除后余4的数字中最大数字。

输入

每个输入包含1个测试用例。每个测试用例先给出一个不超过1000的正整数N,随后给出N个不超过1000的待分类的正整数。数字间以空格分隔。

输出

对给定的N个正整数,按题目要求计算A1~A5并在一行中顺序输出。数字间以空格分隔,但行末不得有多余空格。

若其中某一类数字不存在,则在相应位置输出“N”。

样例输入

13 1 2 3 4 5 6 7 8 9 10 20 16 18
8 1 2 4 5 6 7 9 16

样例输出

30 11 2 9.7 9
N 11 2 N 9

写此题时心情极度不爽,代码极其渣,哪天无聊再重写吧...

代码:

#include <iostream>
#include<string>
using namespace std;
int N;
int a[1010];
int C=0;

string A1(){
    int ans=0;
    for(int i=0;i<N;i++){
        if(a[i]%5==0&&a[i]%2==0){
            ans+=a[i];
        }
    }
   if(ans==0) return "N";
    char str[1010];
    sprintf(str,"%d",ans);
    string ss=str;
    return ss;
}

string A2(){
    int ans=0;
    int sign=1;
    int flag=0;
    for(int i=0;i<N;i++){
        if(a[i]%5==1){
            ans+=(a[i]*sign);
            sign*=-1;
            flag=1;
        }
    }

    if(flag==0) return "N";
    char str[1010];
    sprintf(str,"%d",ans);
    string ss=str;
    return ss;
}

string A3(){
    int ans=0;
    for(int i=0;i<N;i++){
        if(a[i]%5==2){
            ans++;
        }
    }

    if(ans==0) return "N";
    char str[1010];
    sprintf(str,"%d",ans);
    string ss=str;
    return ss;
}

int A4(){
    int ans=0;
    for(int i=0;i<N;i++){
        if(a[i]%5==3){
            ans+=a[i];
            C++;
        }
    }
    return ans;
}

string A5(){
    int ans=0;
    for(int i=0;i<N;i++){
        if(a[i]%5==4){
            if(ans<a[i])
                ans=a[i];
        }
    }
    if(ans==0) return "N";
    char str[1010];
    sprintf(str,"%d",ans);
    string ss=str;
    return ss;
}

int main()
{
    cin>>N;
    for(int i=0;i<N;i++){
        cin>>a[i];
    }
    cout<<A1()<<" "<<A2()<<" "<<A3()<<" ";
    int sum=A4();
    if(sum==0){
        cout<<"N";
    }else{
        printf("%.1f",sum*1.0/C);
    }
    cout<<" "<<A5();
    return 0;
}

 

 

问题 H: 部分A+B (15)

时间限制: 1 Sec  内存限制: 32 MB

题目描述

正整数A的“DA(为1位整数)部分”定义为由A中所有DA组成的新整数PA。例如:给定A = 3862767,DA = 6,则A的“6部分”PA是66,因为A中有2个6。

现给定A、DA、B、DB,请编写程序计算PA + PB。

输入

输入在一行中依次给出A、DA、B、DB,中间以空格分隔,其中0 < A, B < 1010。

输出

在一行中输出PA + PB的值。

样例输入

3862767 6 13530293 3
3862767 1 13530293 8

样例输出

399
0

代码:

#include<iostream>
#include<string>
using namespace std;
typedef long long ll;

ll countP(string s,char a){
	int x=a-'0';
	ll ans=0;
	for(int i=0;i<s.length();i++){
		if(s[i]==a){
			ans=ans*10+x;
		}
	}
	return ans;
}


int main()
{
	string A,B;
	char DA,DB;
	ll pa=0,pb=0;
	while(cin>>A>>DA>>B>>DB){
		pa=countP(A,DA);
		pb=countP(B,DB);
		cout<<pa+pb<<endl;
	}
	return 0;
}

 

 

问题 I: 锤子剪刀布 (20)

时间限制: 1 Sec  内存限制: 32 MB

题目描述

大家应该都会玩“锤子剪刀布”的游戏:两人同时给出手势,胜负规则如图所示:

算法笔记 3.1 codeup课后题-LMLPHP

现给出两人的交锋记录,请统计双方的胜、平、负次数,并且给出双方分别出什么手势的胜算最大。

 

输入

输入第1行给出正整数N(<=105),即双方交锋的次数。随后N行,每行给出一次交锋的信息,即甲、乙双方同时给出的的手势。C代表“锤子”、J代表“剪刀”、B代表“布”,第1个字母代表甲方,第2个代表乙方,中间有1个空格。

输出

输出第1、2行分别给出甲、乙的胜、平、负次数,数字间以1个空格分隔。第3行给出两个字母,分别代表甲、乙获胜次数最多的手势,中间有1个空格。如果解不唯一,则输出按字母序最小的解。

样例输入

10
C J
J B
C B
B B
B C
C C
C B
J B
B C
J J

样例输出

5 3 2
2 3 5
B B
#include<iostream>
using namespace std;
int win1,pin1,fail1;
int win2,pin2,fail2;
int c1,c2,j1,j2,b1,b2;//记录甲乙分别CJB胜的次数
char p1,p2;//甲乙出的是

int judge(){
	if(p1=='C'){
		switch(p2)
		{
			case 'C':
				return 0;//平
			case 'J':
				return 1;//p1胜
			case 'B':
				return 2;//p2胜
		}
	}

	if(p1=='J'){
		switch(p2)
		{
			case 'J':
				return 0;//平
			case 'B':
				return 1;//p1胜
			case 'C':
				return 2;//p2胜
		}
	}

	if(p1=='B'){
		switch(p2)
		{
			case 'B':
				return 0;//平
			case 'C':
				return 1;//p1胜
			case 'J':
				return 2;//p2胜
		}
	}

}

//胜率最高
char most1(){
	if(b1>=c1&&b1>=j1){
		return 'B';
	}
	if(c1>=b1&&c1>=j1){
		return 'C';
	}
	if(j1>=c1&&j1>=b1){
		return 'J';
	}

}

char most2(){
	if(b2>=c2&&b2>=j2){
		return 'B';
	}
	if(c2>=b2&&c2>=j2){
		return 'C';
	}
	if(j2>=c2&&j2>=b2){
		return 'J';
	}

}

int main(){

	int T;

	while(cin>>T){
		win1=pin1=fail1=win2=pin2=fail2=0;
		c1=c2=j1=j2=b1=b2=0;

		while(T--){
			cin>>p1>>p2;
			if(judge()==0){
				pin1++;
				pin2++;
			}else if(judge()==1){//p1胜
				if(p1=='C'){
					c1++;
				}else if(p1=='J'){
					j1++;
				}else{
					b1++;
				}
				win1++;
				fail2++;

			}else{//p2胜
				if(p2=='C'){
					c2++;
				}else if(p2=='J'){
					j2++;
				}else{
					b2++;
				}
				win2++;
				fail1++;
			}
		}
		cout<<win1<<" "<<pin1<<" "<<fail1<<endl;
		cout<<win2<<" "<<pin2<<" "<<fail2<<endl;
		cout<<most1()<<" "<<most2()<<endl;

	}
	return 0;
}

 

 

10-06 13:49