Recommended Posts

是濟城論壇 SRX 版主貼的

應該是大數之類的吧:P

Background :

在一個星球 , 人們都在玩數字遊戲 , 其中最簡單的就是把兩個數加起來在求

這兩個數的和除以某數的餘數

Problem :

有一天 , 地球帶來了大批軍隊 , 向這星球的人說 , 若你們不能在1分鐘之內

回答10000 組數字的和除以某數的餘數 , 你們就要被殺光了

你剛好是這星球唯一會寫程式的人 , 現在你的任務就是救這個星球

Input :

第一個數字n是指有多少組數字 1<n<10001

接下來每組有三個數字 a , b , k

-8000000000000000000 <= ( a , b ) <= 8000000000000000000

1<=k<= 2147483647

當 n = 0 結束此程式

Output :

對每組數字輸出 a+b mod k

鏈接文章
分享到其他網站

請問一下

有提供 sample input 嗎

這樣比較好測~XD


#include<iostream>
#include<cstdlib>

using namespace std;
int main(){
long long int a,b,k;
cin>>a>>b>>k;
cout<<a<<" "<<b<<endl;
if((a>=0 && b>=0) || (a<=0 && b<=0)){
cout<<((a%k)+(b%k))%k<<endl;
}
else if((a<=0 && b>=0) || ((a>=0)&&(b<=0))){
cout<<(a+b)%k<<endl;
}
system("pause");
}

鏈接文章
分享到其他網站
這個應該用long long就可以了吧?

型別轉換我不熟說(我還是新手 xd)

可以的話請前輩寫個範例看看

謝謝~

請問一下

有提供 sample input 嗎

這樣比較好測~XD

原本就沒有附sample

我是自己寫程式產生的

可能有點問題:$


#include <iostream>
#include <fstream>
#define N 5
#define M 2147483647

using namespace std;

void createRandNumForArray(int* ,int);
void writeRandNum(int*, int);
fstream fileOUT("PA.in", ios::out);

int main(){
int iA[N] = {0,0,0,0};
int iB[N] = {0,0,0,0};
int iC[N] = {0,0,0,0};

srand(time(NULL));
int a = (rand()%10000)+2;
fileOUT << a << endl;

for(int j = 0;j < a;j++){
createRandNumForArray(iA, N);
createRandNumForArray(iB, N);
writeRandNum(iA, N);
writeRandNum(iB, N);
fileOUT << (rand()%M)+1 << endl;
}

//system("pause");
return 0;
}

void createRandNumForArray(int* iA, int Q){
iA[0] = (rand()%800)+1;
for(int i = 1;i < Q;i++){
iA[i] = (rand()%9999)+1;
}
}

void writeRandNum(int* iA, int Q){
for(int i = 0;i < Q;i++){
if(iA[i] != 0)
fileOUT << iA[i];
}
fileOUT << " ";
}

鏈接文章
分享到其他網站
請問一下

有提供 sample input 嗎

這樣比較好測~XD


#include<iostream>
#include<cstdlib>

using namespace std;
int main(){
long long int a,b,k;
cin>>a>>b>>k;
cout<<a<<" "<<b<<endl;
if((a>=0 && b>=0) || (a<=0 && b<=0)){
cout<<((a%k)+(b%k))%k<<endl;
}
else if((a<=0 && b>=0) || ((a>=0)&&(b<=0))){
cout<<(a+b)%k<<endl;
}
system("pause");
}

非常謝謝前輩!

我原本不知道有long long這種東西

還在用大數搞半天 xd

鏈接文章
分享到其他網站
  • 1 month later...

請登入後來留意見

在登入之後,您才能留意見



立即登入