ZOJ 3504 P-norm 字符串处理问题
来源:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3504
题意:题意不太好理解。就是给你一些复数,肯定是偶数个,然后让求两个N维复向量的差的P-范数。话说这个名词还是看shi哥的解题报告才知道的。题目其实很水了,就是输入的时候字符串处理有点麻烦。。。
思路:处理好输入后,直接用pow函数就可以了。
附上我的搓代码:
#include <iostream>#include <string>#include <cstdio>#include <cmath>#include <math.h>using namespace std;struct com1{double real,image;}cc1[1010];struct com2{double real,image;}cc2[1010];struct com3{double real,image,ans;}cc3[1010];int main(){//freopen("1.txt","r",stdin);string s1;int cnt = 0;while(cin>>s1){ cnt = 0; double ff[4040]; int len1 = s1.size(),pos1 = 0,ll1 = 0; char ch; double num1 = 0; bool flag1= 0; while(pos1 < len1){ if(s1[pos1] != '(' && s1[pos1] != ',' && s1[pos1] != ')'){ sscanf(&s1[pos1],"%lf%n",&num1,&ll1);pos1 += ll1;ff[cnt++] = num1; } else{ sscanf(&s1[pos1],"%c%n",&ch,&ll1);pos1 += ll1; } } string s2; double p;int tt = 0; while(cin>>s2){ bool flag2 = 0; if(s2[0] <= '9' && s2[0] >= '0'){ sscanf(&s2[0],"%lf%n",&p,&tt);break; } int len2 = s2.size(),pos2 = 0,ll2 = 0; double num2 = 0;char ch2; while(pos2 < len2){ if(s2[pos2] != '(' && s2[pos2] != ',' && s2[pos2] != ')'){ sscanf(&s2[pos2],"%lf%n",&num2,&ll2); pos2 += ll2; ff[cnt++] = num2; } else{ sscanf(&s2[pos2],"%c%n",&ch2,&ll2); pos2 += ll2; } } } cnt--; int cnt1 = 0,cnt2 = 0; for(int i = 0;i <= cnt/2;++i){ if(i % 2 == 0)cc1[cnt1].real = ff[i];elsecc1[cnt1++].image = ff[i]; } for(int i = cnt/2+1;i <= cnt;++i){ if(i % 2 == 0){cc2[cnt2].real = ff[i];}elsecc2[cnt2++].image = ff[i]; } for(int i = 0;i < cnt1;++i){ cc3[i].real = cc1[i].real - cc2[i].real; cc3[i].image = cc1[i].image - cc2[i].image; cc3[i].ans = cc3[i].real * cc3[i].real + cc3[i].image * cc3[i].image; } double sum = 0; for(int i = 0;i < cnt1;++i) { sum += pow(pow(cc3[i].ans,0.5),p); } printf("%lf\n",pow(sum,1.0/p));}return 0;}