1. 程式人生 > >NYOJ 棋盤覆蓋

NYOJ 棋盤覆蓋

ios -- 數字 i++ sin turn ring str nyoj

數字很大,要用大數乘法。

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
char s1[1000];
char s2[10];
char* bignum(char *num1, char *num2)
{
    if(strcmp(num1,"0")==0)
        return "0";
    int length1 = strlen(num1);
    
int length2 = strlen(num2); int i, l; char *res = (char *)malloc(sizeof(char)*(length1 + length2)); //開辟相應內存 memset(res, 0, sizeof(char)*(length1 + length2)); for (i = length1 - 1; i >= 0; i--) for (l = length2 - 1; l >= 0; l--) { res[i + l + 1] += (num1[i] - 0)*(num2[l] -
0); res[i + l] += res[i + l + 1] / 10; //馬上進行進位 res[i + l + 1] %= 10; } int count = 0; while (res[count] == 0) //由於保存的結果是從右向左的,所以要消除左部分的0; { count++; } char* ret = (char *)malloc(sizeof(char)*(length1 + length2 + 2)); memset(ret, 0, sizeof(char)*(length1 + length2 + 2
)); for (l = 0, i = count; i < length1 + length2; l++, i++) //非0部分賦給ret { ret[l] = res[i] + 0; } //printf("Ret=%s\n", ret); return ret; free(res); free(ret); } int main() { int t; cin>>t; while(t--) { int k; cin>>k; strcpy(s1,"0"); strcpy(s2,"4"); //printf("%s\n",s1); while(k--) { //printf("%s\n",s1); strcpy(s1,bignum(s1,s2)); for(int i=strlen(s1)-1;i>=0;i--) { if(s1[i] == 9) { s1[i] = 0; continue; } else { s1[i] = s1[i] + 1; break; } } } printf("%s\n",s1); } return 0; }

NYOJ 棋盤覆蓋