日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

**【POJ - 2389】 Bull Math (高精度乘法)

發布時間:2023/12/10 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 **【POJ - 2389】 Bull Math (高精度乘法) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

?

Bulls are so much better at math than the cows. They can multiply huge integers together and get perfectly precise answers ... or so they say. Farmer John wonders if their answers are correct. Help him check the bulls' answers. Read in two positive integers (no more than 40 digits each) and compute their product. Output it as a normal number (with no extra leading zeros).?

FJ asks that you do this yourself; don't use a special library function for the multiplication.

Input

* Lines 1..2: Each line contains a single decimal number.

Output

* Line 1: The exact product of the two input lines

Sample Input

11111111111111 1111111111

Sample Output

12345679011110987654321

?

解題報告:

?

? ? 水題模擬。

ac代碼:

#include<iostream> #include<cstdio> #include<cstring> using namespace std;const int MAX =1000 + 5 ; char s1[MAX],s2[MAX]; int a[MAX],b[MAX],ans[MAX]; int len1,len2; int p,c;//p代表當前位置,c代表進位 int main() { // freopen("in.txt","r",stdin);scanf("%s",s1);scanf("%s",s2);len1=strlen(s1);len2=strlen(s2);//for(int i = 1; i<=len1; i++) {a[len1-(i-1)]=s1[i-1]-'0';}for(int i = 1; i<=len2; i++) {b[len2-(i-1)]=s2[i-1]-'0';} // for(int i = len1; i>=1; i--) printf("%d",a[i]); // printf("\n"); // for(int i = len2; i>=1; i--) printf("%d",b[i]); // printf("\n");for(int i = 1; i<=len2; i++) {for(int j = 1; j<=len1; j++) {ans[i+j-1] += b[i]*a[j];} }for(int i = 1; i<=len1+len2; i++) {p=i;if(ans[i]<=9) continue;c=ans[i]/10;while(c>0) {ans[++p] += c;ans[p-1]=ans[p-1]%10;c= ans[p]/10;}//或者用下面的代碼: // while(ans[i]>9) { // ans[i]-=10; // ans[i+1]++; // } }// printf("p = %d\n",p);for(int i = len1+len2; i>=1; i--) {if(ans[i]!=0) {p=i;break;}}for(int i = p; i>=1; i--) {printf("%d",ans[i]);}printf("\n");return 0 ;} //bzoj 1754 [Usaco2005 qua]Bull Math//11111111111111//1111111111

?

錯誤代碼:(有空看看哪里有問題)
?

?

Bull Math

?)

#include<bits/stdc++.h>using namespace std;const int MAX =1000 + 5 ; char s1[MAX],s2[MAX]; int a[MAX],b[MAX],ans[MAX]; int len1,len2; int p,c;//p代表當前位置,c代表進位 int main() {freopen("in.txt","r",stdin);scanf("%s",s1);scanf("%s",s2);len1=strlen(s1);len2=strlen(s2);//for(int i = 1; i<=len1; i++) {a[len1-(i-1)]=s1[i-1]-'0';}for(int i = 1; i<=len2; i++) {b[len2-(i-1)]=s2[i-1]-'0';} // for(int i = len1; i>=1; i--) printf("%d",a[i]); // printf("\n"); // for(int i = len2; i>=1; i--) printf("%d",b[i]); // printf("\n");for(int i = 1; i<=len2; i++) {p=i;c=0;for(int j = 1; j<=len1; j++) {ans[p]+=b[i]*a[j];c=ans[p]/10;ans[p]%=10;while(c>0) {ans[++p] += c%10;//這里是+=!! c/=10;}} for(int i = p; i>=1; i--) {printf("%d",ans[i]);}printf("\n");}for(int i = p; i>=1; i--) {printf("%d",ans[i]);}printf("\n");return 0 ;} //bzoj 1754 [Usaco2005 qua]Bull Math//11111111111111//1111111111

?

總結:

?

? ? 你在搞笑嗎這么簡單的模擬你搞了一個小時。。。。邏輯關系能不能理清楚了再敲啊。。。

?

// printf("p = %d\n",p);

?

總結

以上是生活随笔為你收集整理的**【POJ - 2389】 Bull Math (高精度乘法)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。