1 条题解

  • 0
    @ 2025-4-12 21:47:19

    C :

    #include<stdio.h>
    #include<string.h>
    typedef struct
    {
    	int xishu;
    	int zhishu;
    }elemtype;
    int main()
    {
    	elemtype a[100],b[100],c[200];
    	int lena,lenb,lenc;
    	char s1[101],s2[101],*t;
    	int i,j;
    	while(gets(s1) && strlen(s1))
    	{
    		gets(s2);
    		lena=lenb=lenc=0;
    		t=strtok(s1," \n\t");
    		while(t)
    		{
    			sscanf(t,"%d",&a[lena].xishu);
    			t=strtok(NULL," \n\t");
    			sscanf(t,"%d",&a[lena].zhishu);
    			t=strtok(NULL," \t\n");
    			lena++;
    		}
    		t=strtok(s2," \n\t");
    		while(t)
    		{
    			sscanf(t,"%d",&b[lenb].xishu);
    			t=strtok(NULL," \n\t");
    			sscanf(t,"%d",&b[lenb].zhishu);
    			t=strtok(NULL," \t\n");
    			lenb++;
    		}
    		i=j=0;
    	while(i<lena && j<lenb)
    	{
    		if(a[i].zhishu>b[j].zhishu)
    		{
    			c[lenc].zhishu=a[i].zhishu;
    			c[lenc].xishu=a[i].xishu;
    			++i;++lenc;
    		}
    		else if(a[i].zhishu<b[j].zhishu)
    		{
    			c[lenc].zhishu=b[j].zhishu;
    			c[lenc].xishu=b[j].xishu;
    			++j;++lenc;
    		}
    		else if(a[i].zhishu==b[j].zhishu)
    		{
    			if(a[i].xishu+b[j].xishu)
    			{
    			c[lenc].zhishu=a[i].zhishu;
    			c[lenc].xishu=a[i].xishu+b[j].xishu;
    			++lenc;
    			}
    			++i;++j;
    		}
    	}
    	while(i<lena)
    	{
    		c[lenc].xishu=a[i].xishu;
    		c[lenc].zhishu=a[i].zhishu;
    		++lenc;
    		++i;
    	}
    	while(j<lenb)
    	{
    		c[lenc].xishu=b[j].xishu;
    		c[lenc].zhishu=b[j].zhishu;
    		++lenc;
    		++j;
    	}
    	for(i=0;i<lenc;i++)
    	    printf("%d %d ",c[i].xishu,c[i].zhishu);
    	printf("\n");
    	}
    	return 0;
    }
    

    C++ :

    #include <stdio.h>
    #include <string.h>
    
    struct ElemType{		// 定义多项式的每一项类型
    	int coef;			// 系数
    	int expn;			// 指数
    };
    
    int main(){
    
    	ElemType a[110], b[110], r[220];	// 定义两个多项式以及他们的结果
    	int lenA, lenB, lenR;				// 定义多项式 a,b,r 的项数
    	char strA[1000], strB[1000], *tmp;	// 定义用来读取多项式的字符串,待会分解为整数
    	int i, j;
    	while(gets(strA) && strlen(strA)){	// 注意判断字符串 strA 的长度,防止有空行
    		gets(strB);						// 读入字符串 strB
    		lenA = lenB = lenR = 0;			// 首先令各多项式的项数为 0
    
    		// 以下将字符串 a 放置到多项式 a 中
    		tmp = strtok(strA, " \t\n");			// 注意这里传入的是字符串 strA
    		while(tmp){
    			sscanf(tmp, "%d", &a[lenA].coef);	// 从字符串中读取一个系数
    			tmp = strtok(NULL, " \t\n");		// 注意这里传入的是 NULL
    			sscanf(tmp, "%d", &a[lenA].expn);	// 从字符串中读取一个指数
    			tmp = strtok(NULL, " \t\n");
    			lenA++;								// 多项式项数加 1
    		}
    
    		// 以下将字符串 b 放置到多项式 b 中,操作类似上面的步骤
    		tmp = strtok(strB, " \t\n");
    		while(tmp){
    			sscanf(tmp, "%d", &b[lenB].coef);
    			tmp = strtok(NULL, " \t\n");
    			sscanf(tmp, "%d", &b[lenB].expn);
    			tmp = strtok(NULL, " \t\n");
    			lenB++;
    		}
    
    		// 下面是进行多项式的加法,算法类同有序序列的有序合并
    		i = j = 0;								// 注意这里每次循环时值要赋为0,否则下次循环就值就会有问题
    		while(i<lenA && j<lenB){
    			if(a[i].expn > b[j].expn){			// 如果 a 的某一项比 b 的某一项指数高,则结果应加入 a 这一项
    				r[lenR].coef = a[i].coef;
    				r[lenR].expn = a[i].expn;
    				i++;
    				lenR++;
    			}else if(a[i].expn < b[j].expn){	// 如果 a 的某一项比 b 的某一项指数低,则结果应加入 b 这一项
    				r[lenR].coef = b[j].coef;
    				r[lenR].expn = b[j].expn;
    				j++;
    				lenR++;
    			}else if(a[i].expn == b[j].expn){	// 如果 a 的某一项和 b 的某一项指数相等,
    				if(a[i].coef + b[j].coef){		// 如果两项的系数和不为 0,则结果加上它们的和,否则就忽略
    					r[lenR].coef = a[i].coef + b[j].coef;
    					r[lenR].expn = a[i].expn;
    					lenR++;
    				}
    				i++;
    				j++;
    			}
    		}
    
    		// 结果加入多项式 a 中剩下的部分
    		while(i < lenA){
    			r[lenR].coef = a[i].coef;
    			r[lenR].expn = a[i].expn;
    			i++;
    			lenR++;
    		}
    		// 结果加入多项式 b 中剩下的部分
    		while(j < lenB){
    			r[lenR].coef = b[j].coef;
    			r[lenR].expn = b[j].expn;
    			j++;
    			lenR++;
    		}
    
    		// 显示多项式的结果
    		for(int i=0;i<lenR;i++){
    			printf("%d %d ", r[i].coef, r[i].expn);
    		}
    		// 换行
    		putchar('\n');
    	}
    
    	return 0;
    }
    
    

    Java :

    import java.util.*;
    class D {
    	int a,e;
    	D(int i,int j) {a=i;e=j;}
    };
    public class Main 
    {
    	public static void main(String[] args)
    	{
    		Scanner cin = new Scanner(System.in);
    		int a,e;
    		String t;
    		D tD;
    		List<D> L=new LinkedList<D>();
    		while(cin.hasNext())
    		{
    			t=cin.nextLine();
    			Scanner str=new Scanner(t);
    			while(str.hasNext())
    			{
    				a=str.nextInt();
    				e=str.nextInt();
    				L.add(new D(a,e));
    			}
    			t=cin.nextLine();
    			str.close();
    			str=new Scanner(t);
    			while(str.hasNext())
    			{
    				boolean f=true;
    				a=str.nextInt();
    				e=str.nextInt();
    				ListIterator<D> p=L.listIterator();
    				while(p.hasNext())
    				{
    					tD=p.next();
    					if(e>tD.e)
    					{
    						p.previous();
    						p.add(new D(a,e));
    						f=false;
    						break;
    					}
    					else if(e==tD.e)
    					{
    						if(a+tD.a==0)
    							p.remove();
    						else
    							p.set(new D(a+tD.a,e));
    						f=false;
    						break;
    					}
    				}
    				if(f)
    				{
    					p.add(new D(a,e));
    				}
    			}
    			ListIterator<D> p=L.listIterator();
    			while(p.hasNext())
    			{
    				tD=p.next();
    				System.out.print(tD.a+" "+tD.e+" ");
    			}
    			L.clear();
    			System.out.println();
    			str.close();
    		}
    		cin.close();
    	}
    }
    
    • 1

    信息

    ID
    1664
    时间
    1000ms
    内存
    32MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者