首页 > 试题广场 >

三角形相加

[编程题]三角形相加
  • 热度指数:3340 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

写一个CTriangle 类,要求可以接受CTriangle(y,x)形式的构造表示含义如下:A(0,y) B(0,0) C(x,0)。要求能够处理若干个三角形的相加(点B保持不变,两直角边相加)。


输入描述:
输入有若干行,每行两个数y,x,读到0表示结束。


输出描述:
输出一行表示三点坐标,格式参见样例。
示例1

输入

10 20
1 31
0

输出

A(0,11),B(0,0),C(51,0)
#include <bits/stdc++.h>
using namespace std;
class CTriangle{
	public:
		int x,y;
		CTriangle(int yy,int xx):y(yy),x(xx){
		}
		CTriangle operator+(const CTriangle &a){
			//cout<<"A(0,"<<y+a.y<<"),B(0,0),C("<<x+a.x<<",0)";
			return CTriangle (y+a.y,x+a.x);
		}
};
int main(){
	int x,y;
	cin>>y>>x;
	CTriangle a(y,x);
	while(cin>>y>>x&&y!=0){
		CTriangle b(y,x);
		//CTriangle c(0,0);
		a=a+b;
		
	}
	if(y==0) cout<<"A(0,"<<a.y<<"),B(0,0),C("<<a.x<<",0)";
	
	return 0;
}


发表于 2020-03-11 19:10:15 回复(2)
这个也是没按要求好好做啊(*/ω\*),虽然测试点都能通过的说
#include<iostream>
using namespace std;
int main(){
    int x,y;
    int a=0,b=0;
    while(cin>>x){
        if(x==0)
            break;
        cin>>y;
        a+=x;
        b+=y;
    }
    cout<<"A(0,"<<a<<"),B(0,0),C("<<b<<",0)"<<endl;
}

发表于 2019-02-25 13:21:16 回复(1)
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <cmath>
#include <set>
#include <queue>
  
using namespace std;
class CTriangle{
        public:
            int x,y;
        CTriangle()
        {
    
            }    
        CTriangle(int x,int y)
        {
            this->x = x;
            this->y = y;
        }
        
        CTriangle operator + (const CTriangle A)
        {
            return CTriangle(x + A.x,y + A.y);
        }
        void output()
        {
            cout << "A(0," << y <<"),B(0,0),C(" << x << ",0)" << endl;
        }
};
int main()
{
    int x;
    CTriangle ans(0,0);
    while(cin >> x)
    {
        if(x == 0)
        ans.output();
        else
        {
            int y;
            cin >> y;
            swap(x,y);
            ans = ans + CTriangle(x,y);
        }
    }
}

发表于 2019-03-04 20:51:40 回复(1)
#include<stdio.h>
int main()
{
    int x,y,sumx=0,sumy=0;
    while(scanf("%d",&y)!=EOF&&y)
    {
        scanf("%d",&x);
        sumx+=x;sumy+=y;
    }
    printf("A(0,%d),B(0,0),C(%d,0)",sumy,sumx);
}

发表于 2020-05-10 12:47:00 回复(0)
我也逃避了构造CTriangle类和重载运算符...
#include <cstdio>

int main(){
    int y,x,sum_y = 0,sum_x = 0;
    while(scanf("%d %d",&y,&x) != EOF){
        if(y == 0) break;
        sum_y += y;
        sum_x += x;
    }
    printf("A(0,%d),B(0,0),C(%d,0)\n",sum_y,sum_x);
    return 0;
}


发表于 2023-03-23 10:16:15 回复(0)
#include <iostream>
#include <cstdio>
using namespace std;

class Triangle{

public:
    int ypoint;
    int xpoint;

    Triangle(int a, int b){
        this->ypoint = a;
        this->xpoint = b;
    }

};

Triangle operator +(const Triangle &m,const Triangle &n){
        return Triangle(m.ypoint+n.ypoint,m.xpoint+n.xpoint);
    }


int main() {
    int x1,y1,x2,y2;
    cin >> x1 >> y1;
    Triangle t1(x1,y1);
    while(cin >> x2){
        if(x2 == 0){
            break;
        }

        cin >> y2;
        Triangle t2(x2,y2);
        t1 = t1+t2;

        

    }
    cout << "A(0," << t1.ypoint << "),B(0,0),C(" << t1.xpoint << ",0)" <<endl;

}

发表于 2023-03-18 12:01:25 回复(0)
#include <iostream>
using namespace std;
class CTriangle {
  public:
    int x, y;
    CTriangle(int _y, int _x): y(_y), x(_x) {}
    CTriangle operator+(CTriangle& triangle) {
        return CTriangle(y + triangle.y, x + triangle.x);
    }
    void show() {
        printf("A(0,%d),B(0,0),C(%d,0)", y, x);
    }
};
int main() {
    int y, x;
    CTriangle triangle(0, 0);
    while (1) {
        cin >> y;
        if (y == 0)break;
        cin >> x;
        CTriangle triangle1(y, x);
        triangle = triangle + triangle1;
    }
    triangle.show();
}

发表于 2023-01-17 12:36:49 回复(0)
#include<iostream>
using namespace std;


class CTriangle
{
public:

    friend CTriangle operator +( const CTriangle& c1, const CTriangle& c2);
    CTriangle( double x, double y ){
        m_x = x;
        m_y = y;
    }
    void display();
private:
    double m_x;
    double m_y;
};

CTriangle operator +( const CTriangle& c1, const CTriangle& c2)
{
    return CTriangle( c1.m_x + c2.m_x, c1.m_y + c2.m_y );
}

void CTriangle::display()
{
    cout << "A(0," << m_y << "),B(0,0),C(" << m_x << ",0)" << endl;
}

void test()
{
    double a, b;
    CTriangle C( 0, 0 );
    while( cin >> a >> b && a != 0 )
    {
        CTriangle A( b, a );
        C = A + C;
    }
    C.display();
}

int main()
{
    test();
    return 0;
}
发表于 2021-01-28 14:58:54 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        CTriangle c0 = new CTriangle(0, 0);
        while (true){
            int x = sc.nextInt();
            if (x == 0){
                break;
            }
            int y = sc.nextInt();
            CTriangle c1 = new CTriangle(x, y);
            c0.add(c1);
        }
        System.out.println(c0);
        sc.close();
    }
}
class CTriangle{
    private int x;
    private int y;

    public CTriangle(int x, int y){
        this.x = x;
        this.y = y;
    }

    @Override
    public String toString(){
        return "A(0," + x + "),B(0,0),C(" + y + ",0)";
    }

    public void add(CTriangle c){
        x = x + c.getX();
        y = y + c.getY();
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }
}

发表于 2020-05-01 11:30:18 回复(0)
#include <iostream>
using namespace std;
class Triangal
{
public:
	Triangal(int y, int x);
	int X, Y;
	Triangal operator+(Triangal&);
private:

};
Triangal::Triangal(int y, int x)
{
	X = x;
	Y = y;
}
Triangal Triangal::operator+(Triangal &a)
{
	return Triangal(a.Y + Y, a.X + X);
}
int main()
{


	int x, y;
	cin >> y >> x;
	Triangal a(y, x);
	while (cin >> y && y != 0&&cin>> x ) {
		Triangal b(y, x);
		a = a + b;	
	}
	
	cout << "A(0," << a.Y << "),B(0,0),C(" << a.X << ",0)";
	return 0;
}

编辑于 2020-04-03 14:26:43 回复(0)
#include <stdio.h>
int main()
{
	int i, j;
	int count1=0, count2=0;
	int y, x;
	while (1)
	{
		scanf("%d %d", &y, &x);
		getchar();//用于去掉输入缓冲流中的回车
		if (y == 0)
			break;
		count1 = count1 + y;
		count2 = count2 + x;
	}
	printf("A(0,%d),B(0,0),C(%d,0)\n", count1, count2);
		return 0;
	
}


编辑于 2020-03-20 23:51:59 回复(0)
#include<iostream>
using namespace std;

class CTriangle{
    public:
    CTriangle(int y=0,int x=0){a[1]=y;c[0]=x;}
    public:
    CTriangle operator+(const CTriangle &);
    void display() const;
    private:
    int a[2]={0},b[2]={0},c[2]={0};
};
CTriangle CTriangle::operator+(const CTriangle &ct)
{

    return CTriangle(ct.a[1]+this->a[1],ct.c[0]+this->c[0]);
}
void CTriangle::display() const
{
    cout<<"A(0,"<<a[1]<<")"<<",B(0,0),C("<<c[0]<<",0)";
}
int main()
{
    int y,x;
    cin>>y>>x;
    CTriangle ct;
    while(y)
    {
        CTriangle ctr(y,x);
        ct=ct+ctr;
        cin>>y>>x;
    }
    ct.display();
}
发表于 2019-06-16 13:03:37 回复(0)
public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int a = 0;
        int b = 0;
        while(true){
            String str = br.readLine();
            if("0".equals(str)){
                break;
            }
            String[] strs = str.split(" ");
            a+=Integer.valueOf(strs[0]);
            b+=Integer.valueOf(strs[1]);
        }
        System.out.println("A(0,"+a+"),B(0,0),c("+b+",0)");
    }
发表于 2019-05-30 10:40:53 回复(1)
import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        int x = 0;
        int y = 0;
        while(sc.hasNext())
        {
            String[] strs = sc.nextLine().split(" ");
            if(strs[0].equals("0"))
            {
                System.out.println("A(0,"+x+"),B(0,0),C("+y+",0)");
                return;
            }
            else
            {
                x += Integer.parseInt(strs[0]);
                y += Integer.parseInt(strs[1]);
            }
        }
    }
}

发表于 2019-05-29 15:16:06 回复(0)
#include<iostream>
class Point
{
    int y,x;
    public:
    Point() {x=0;y=0;}
    Point(int sx, int sy){x=sx; y=sy;}
    void Gen_Point(int t, int s) {x=t; y=s;}
    Point operator+(Point& p1);
    int Y() { return y; } int X() { return x; }
};
Point Point::operator+(Point& p1)
{
    Point res; res.x = x+p1.x;
    res.y = y+p1.y; return res;
}
class CTriangle
{
    Point A; 
    Point B; 
    Point C;
    public:
    CTriangle(int yy, int xx)
    {
        A.Gen_Point(0, yy);
        B.Gen_Point(0, 0);
        C.Gen_Point(xx,0);
    }
    CTriangle operator+(CTriangle& T1);
    void output() { printf("A(0,%d),B(0,0),C(%d,0)\n",A.Y(),C.X());}
};
CTriangle CTriangle::operator+(CTriangle& T1)
{
    CTriangle result(0,0); result.A = A + T1.A; result.B = B+T1.B;result.C = C+T1.C;
    return result;
}
int main()
{
    int y,x;
    CTriangle tend(0,0);
    while(true)
    {
        scanf("%d %d",&y,&x); 
        if(y==0) {tend.output(); break;}
        CTriangle T1(y,x);
        tend = tend + T1;
        scanf("%d %d",&y,&x); 
        if(y==0) {tend.output(); break;}
        CTriangle T2(y,x);
        tend = tend + T2;
    }
    return 0;
}

发表于 2019-04-15 19:33:20 回复(1)
data_list = []
while True:
    temp = list(map(int,input().split()))
    if temp[0] == 0:
        break
    else:
        data_list.append(temp)

y = 0
x = 0
for data in data_list:
    y += data[0]
    x += data[1]
print('A(0,%d),B(0,0),C(%d,0)'%(y,x))

发表于 2019-04-08 22:33:32 回复(2)
//运行时间为3ms,占用内存376k
#include<iostream>
using namespace std;
class CTriangle{
    public :
    double x,y;
    CTriangle(){}
    CTriangle(double a,double b){
        x=a;
        y=b;
    }
};
int main(){
    double a,b;
    double A=0,B=0;
    while(cin>>a>>b){
        if(a!=0){
            CTriangle point(a,b);
            A+=point.x;
            B+=point.y;
        }
        else{
            cout<<"A(0,"<<A<<"),B(0,0),C("<<B<<",0)"<<endl;
        }
    }
}

发表于 2019-03-30 16:58:11 回复(0)

import java.util.Scanner;
public class Main{
    public static void main(String[] args)
    {
        CTriangle t=new CTriangle();
        Scanner input=new Scanner(System.in);
        while(input.hasNextInt())
        {
            int y=input.nextInt();
            if(y==0)
                break;
            int x=input.nextInt();
//            System.out.println("y="+y);
//            System.out.println("x="+x);

//            System.out.println("y="+t.getY());
//            System.out.println("x="+t.getX()); 
            t.setY(y+t.getY());
            t.setX(x+t.getX());
//            System.out.println("y="+t.getY());
//            System.out.println("x="+t.getX());
        }
        System.out.printf("A(0,%d),B(0,0),C(%d,0)",t.getY(),t.getX());
        input.close();
    }
}

class CTriangle
{
    private int x;
    private int y;
    public void setX(int a)
    {
        this.x=a;
    }
    public int getX()
    {
        return this.x;
    }
        public void setY(int b)
    {
        this.y=b;
    }
    public int getY()
    {
        return this.y;
    }
    public CTriangle(){};
    public CTriangle(int b,int a)
    {
        this.y=b;
        this.x=a;
    }
}

发表于 2019-03-29 20:33:54 回复(0)
import java.util.Scanner;
public class Main {
 static class CTriangle {   int x;   int y;
  public int getX() {    return x;   }
  public void setX(int x) {    this.x = x;   }
  public int getY() {    return y;   }
  public void setY(int y) {    this.y = y;   }
  public CTriangle(int y, int x) {    super();    this.x = x;    this.y = y;
  }
  public CTriangle() {   }  }
 public static void main(String[] args) {   Scanner sc = new Scanner(System.in);      int n = 0;   int m = 0;   while (true) {    int a = sc.nextInt();    if (a == 0) {     break;    } else {                 n = n+a;     m = m+sc.nextInt();    }   }   CTriangle ctr = new CTriangle(n,m);   System.out.println("A(0,"+ctr.getY()+"),B(0,0),C("+ctr.getX()+",0)");  } }
感觉这种题目好没意思,,完全搞不清楚要干嘛
发表于 2019-03-25 16:05:45 回复(0)
 
class CTriangle:  def __init__(self,y,x):  self.a=y  self.c=x  def __add__(self, other):  self.a=self.a+other.a  self.c=self.c+other.c if __name__ == '__main__':
    sum=CTriangle(0,0)  while(True):
        zb=list(map(int,input().split())) i
        f(zb[0]==0 or zb[1]==0):  break    temp=CTriangle(zb[0],zb[1])
        sum.__add__(temp)  print('A(0,{}),B(0,0),C({},0)'.format(sum.a,sum.c))

发表于 2019-03-22 22:53:48 回复(0)

问题信息

上传者:小小
难度:
23条回答 2628浏览

热门推荐

通过挑战的用户

查看代码