hdu,hduacm

來源:互聯網
上載者:User

hdu,hduacm

題意:求兩個n x n的矩陣相乘後模3的結果,n <= 800。

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=4920

——>>呀呀。。

1、3層計算的for進行緩衝最佳化,根據CPU的L1級緩衝的實現原理,減少緩衝的變更。如果每次都計算完一個單元格的結果再計算下一個單元格的結果,那麼被乘矩陣的訪問就會頻繁地更新緩衝,使效率很低。。

2、輸入開掛,G++提效500ms+。。

3、對乘法進行剪枝。。

沒有第1個操作,後果是嚴重的。。

n^3的複雜度A過,但,此不是正解。。

#include <cstdio>#include <cstring>const int MAXN = 800 + 10;int n;int A[MAXN][MAXN], B[MAXN][MAXN], mtRet[MAXN][MAXN];int ReadInt(){    int nRet = 0;    char cIn;    while ((cIn = getchar()) >= '0' && cIn <= '9')    {        nRet = nRet * 10 + cIn - '0';    }    return nRet % 3;}void Read(){    getchar();    for (int i = 1; i <= n; ++i)    {        for (int j = 1; j <= n; ++j)        {            A[i][j] = ReadInt();        }    }    for (int i = 1; i <= n; ++i)    {        for (int j = 1; j <= n; ++j)        {            B[i][j] = ReadInt();        }    }}void Solve(){    memset(mtRet, 0, sizeof(mtRet));    for (int i = 1; i <= n; ++i)    {        for (int k = 1; k <= n; ++k)        {            if(!A[i][k]) continue;            for (int j = 1; j <= n; ++j)            {                mtRet[i][j] += A[i][k] * B[k][j];            }        }    }}void Print(){    for (int i = 1; i <= n; ++i)    {        for (int j = 1; j < n; ++j)        {            printf("%d ", mtRet[i][j] % 3);        }        printf("%d\n", mtRet[i][n] % 3);    }}int main(){    while (scanf("%d", &n) == 1)    {        Read();        Solve();        Print();    }    return 0;}



hdu acm 2032

我發我的ac代碼給你看吧,肯定是輸出時多了什麼空格什麼的,好好看看吧,加油
#include<stdio.h>
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int a[31][31];
int i,j,k;
for(i=1;i<=30;i++)
a[i][1]=a[i][i]=1;
if(n==1)printf("1\n");
if(n>=2)printf("1\n1 1\n");
for(i=3;i<=n;i++)
{
printf("1 ");
for(j=2;j<i;j++)
{
a[i][j]=a[i-1][j]+a[i-1][j-1];
printf("%d ",a[i][j]);
}
printf("%d\n",a[i][i]);
}
printf("\n");
}
}
 
簡單題hdu 1004 Runtime Error(ACCESS_VIOLATION)

問題真不少...下面代碼能ac
STACK_OVERFLOW是棧空間溢出,什麼是棧請自行google,你這裡表現為a開得太大了,將大數組開到外面就可以解決。
ACCESS_VIOLATION一般是數組越界了,你這裡表現為b開的太小,當n>100時,i、j下標就會越界

#include<stdio.h>
#include<string.h>
char a[1001][20]; //500改小就成WA,改大就STACK_OVERFLOW
int main()
{

int b[1001],i,maxi,max,j,n;
while(scanf("%d",&n)!=EOF)
{
if(n==0) break; //題幹給出的結束條件,請不要忽略
for(i=0;i<n;i++)
scanf("%s",a[i]);
for(i=0;i<n;i++)
{
b[i]=0;
for(j=i+1;j<n;j++)
if(strcmp(a[j],a[i])==0)
b[i]++;
}
max=b[0];
for(i=0;i<n;i++)
if(b[i]>=max)
{ max=b[i];
maxi=i;
}
printf("%s\n",a[maxi]);
}
return 0;
}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.