2-1 Add Two Polynomials (20分) Write a function to add two polynomials. Do not destroy the input. Use a linked list implementation with a dummy head node. Note: The zero polynomial is represented by an empty list with only the dummy head node.
输入格式:
Polynomial Add( Polynomial a, Polynomial b );
where Polynomial is defined as the following:
typedef struct Node PtrToNode; struct Node { int Coefficient; int Exponent; PtrToNode Next; }; typedef PtrToNode Polynomial; / Nodes are sorted in decreasing order of exponents.*/
The function Add is supposed to return a polynomial which is the sum of a and b.
Sample program of judge: 在一行中输出第一次出现的“250”是对方扔过来的第几个数字(计数从1开始)。题目保证输出的数字在整型范围内。
#include <stdio.h> #include <stdlib.h> typedef struct Node PtrToNode; struct Node { int Coefficient; int Exponent; PtrToNode Next; }; typedef PtrToNode Polynomial; Polynomial Read(); / details omitted / void Print( Polynomial p ); / details omitted / Polynomial Add( Polynomial a, Polynomial b ); int main() { Polynomial a, b, s; a = Read(); b = Read(); s = Add(a, b); Print(s); return 0; } / Your function will be put here */
易难点:
抄作业必备代码,童嫂无欺
#include<iostream> #include<string> using namespace std; int main(){ string str; int count=0;// 当前的数字个数 int first=0,second=0;// 空格位置 getline(cin,str); // 输入字符串(带空格) for(int i=0;i<str.length();i++){ if(str[i]==' '){ second=i; count++; if(str.substr(first,second-first)=="250"){ cout << count << endl; return 0; } first=second+1; } } if(str.substr(first,str.length()-first)=="250"){ cout << count+1 << endl; return 0; } cout << -1 << endl; return 0; }