发表于: 2017-09-26 23:20:55
2 774
一、今天完成的事情
1.做了一白天。多项式的加法解决了,,但是乘法就死都解决不了。。。
先把代码放上来,明天继续!!
#include <stdio.h>
#include <stdlib.h>
typedef struct PolyNode *Polynomial;
struct PolyNode {
int coef;
int expon;
Polynomial link;
};
Polynomial ReadPoly();
void Attach(int c, int e,Polynomial *pRear);
Polynomial Add(Polynomial P1,Polynomial P2);
void PrintPoly(Polynomial P);
Polynomial Mult(Polynomial P1,Polynomial P2);
Polynomial ReadPoly() {
Polynomial P,Rear,tmp;
int c , e ,N;
scanf("%d",&N);
P = (Polynomial)malloc(sizeof(struct PolyNode));//其实是一个类型转换。
P -> link = NULL;
Rear = P;
while(N--) {
scanf("%d %d",&c,&e);//一对一对读。。
Attach(c,e,&Rear);//Rear是当前结果多项式的最后一项。Rear值要改变,所以要传进去指针。Rear的初值一种是设为null 一种是指向一个空节点。
}
tmp = P;
P = P -> link;
free(tmp);
return P;
}
void Attach(int c , int e ,Polynomial *pRear) {
Polynomial P;
P = (Polynomial)malloc(sizeof(struct PolyNode));//这是一个类型的转换。
P-> coef = c;
P -> expon = e;
P -> link = NULL;
(*pRear )-> link= P;
*pRear = P;
}
Polynomial Add(Polynomial P1 ,Polynomial P2) {
Polynomial P ,Rear,t1 , t2;
t1 = P1;
t2 = P2;
P = (Polynomial)malloc(sizeof(struct PolyNode));
P -> link = NULL;
Rear = P;
while(t1 && t2) {
if(t1->expon == t2 -> expon) {
Rear -> link = t1;
Rear = Rear->link;
Rear -> coef = t1->coef + t2->coef;
t1 = t1->link;
t2 = t2 -> link;
}
else if(t1 ->expon > t2 -> expon) {
Rear ->link = t1;
Rear = Rear -> link;
t1 = t1->link;
}
else {
Rear -> link = t2;
Rear = Rear -> link;
t2 = t2->link;
}
}
while(t1) {
Rear -> link = t1;
}
while(t2) {
Rear -> link = t2 ;
}
return P->link;
}
void PrintPoly(Polynomial P) {
while(P) {
if(P)
printf("%d %d ",P->coef,P->expon);
else
printf("%d %d",P->coef,P->expon);
P = P ->link;
}
}
Polynomial Mult(Polynomial P1,Polynomial P2) {
Polynomial P,P3,Rear1,Rear2,t1,t2,t3;
bool flag = 1;
t1 = P1;
t2 = P2;
P = (Polynomial)malloc(sizeof(struct PolyNode));
P ->link =NULL;
Rear1 = P;
P3 = (Polynomial)malloc(sizeof(struct PolyNode));//用来存最后的结果
P3 ->link =NULL;
Rear2 = P3;
//将乘法转化为加法
//申请一个空节点,用第一个里面的每个元素,依次乘以第二个里面的每个元素。
while(t1) {
while(t2) {
if(flag ) {
Attach(t1->coef*t2->coef, t1->expon +t2->expon,&Rear1);
}
else {
Attach(t1->coef*t2->coef, t1->expon +t2->expon,&Rear2);
}
t2 = t2->link;
}
// PrintPoly(P->link);
printf("------");
if(P3->link) {
t3 = Add(P->link,P3->link);
// PrintPoly(t3);
printf("------");
// PrintPoly(P3);
}
flag = !flag;
t1 = t1->link;
t2 = P2;//重新从P2的第一个元素开始乘。
}
return t3;
}
int main() {
Polynomial P1,P2,PP,PS;
P1 = ReadPoly();
P2 = ReadPoly();
PP = Mult(P1,P2);
PrintPoly(PP);
printf("\n");
PS = Add(P1,P2);
PrintPoly(PS);
return 0;
}
2.关于twosum的问题 用java 语言用四个方法 分别实现
3.期间复习了一下java的容器知识。
/*
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
*/
//此题首先想到的是暴力搜索,虽然暴力,复杂度也很高,但是。。。简单啊
//所以我立刻就用了这个方法实现成功了。。。。。
//代码如下
//另一种在下面。。
class Solution {
public int[] twoSum(int[] nums, int target) {
int[] num = new int[2];
for(int i = 0 ;i < nums.length ;i++) {
for(int j = i+1;j<nums.length;j++) {
if(nums[i] + nums[j] == target) {
num[0] = i;
num[1] = j;
break;
}
}
}
return num;
}
}
//第二种,就是继续改进,上面的代码
class Solution {
public int[] twoSum(int[] nums, int target) {
for(int i = 0 ;i < nums.length ;i++) {
for(int j = i+1;j<nums.length;j++) {
if(nums[i] + nums[j] == target) {
return new int[]{i,j};//不用新建一个数组 ,节省空间
}
}
}
throw new IllegalArgumentException("no such two nums");//在没有找到,就抛出一个错误。
}
}
//第三种方法,用HashMap,因为从集合中找值,Hash Table 能够很好的进行查找,而且给定键来找值,几乎是线性时间的。
//因为要想更加快速的找到元素所在的位置,就必须将键设为值,值设为键。所以导入hashmap里的时候,要反着导,,,这就避免了for循环查找的繁琐
//因为一定会有两个操作。1.找到两个元素,2、通过元素查找位置。
//有一个疑问,如果输入是3 3 2 6 这样的 为什么能成功呢?? hashmap是键值唯一的啊。。。插不进去。。
//懂了 能插进去 而且相同的元素 会替换掉。。因为键是相同的 所以值每次都取最大的。。。
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer,Integer> m = new HashMap<Integer,Integer>();
//利用一个循环将元素导入
for(int i = 0;i<nums.length;i++) {
m.put(nums[i],i);//小套路!!!!!
}
for(int j = 0;j<nums.length;j++) {
if(m.containsKey(target - nums[j]) && m.get(target - nums[j]) != j) {//map只有包含containsKey或者containsValue的这两个方法。没有contains()方法。另外 get方法只能查找值。
return new int[] {j ,m.get(target - nums[j])};
}
}
throw new IllegalArgumentException("no such argument");
}
}
//第四种方法。更加简便。
//想一下,我又没有可能在一个循环中做完呢??
//那必须得遍查边导。
//这个想法是先找到最后一个元素。
//然后根据最后一个元素和HashMap 配对。不会出现同一个元素相加的情况,因为hashMap里面永远都是比他少一个元素。
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer,Integer> m = new HashMap<Integer,Integer>();
for(int i = 0;i<nums.length;i++) {
int f = target - nums[i];
if(m.containsKey(f) ) {//
return new int[] {m.get(f),i};
}
m.put(nums[i],i);
}
throw new IllegalArgumentException("no such argument");
}
}
4.关于修真院的任务。没做。。。。做完这些已经是现在了。。。明天抽时间做!!!
二、明天完成的事情
1.还是刷刷题,把多项式的乘法想办法给解决掉
2.做修真院的任务。明天要学
三、问题
1.保持心态 。
2. 要开心。、、、
3.为啥交不了日报了。。。。。我网络没问题啊
四、收获
渐渐的学吧。。
评论