Problem ID :h176. A.A+B Problem

New Thread

User 測點 2,3 WA

nandemoi SendIMessage
nandemoi

h176. A.A+B Problem | From: 111.250.59.89 | Post Date:2020-02-16 13:16:03.0

//
// main.cpp
// h176
//
// Created by Nandemoi on 2/16/20.
// Copyright © 2020 Elton Huang. All rights reserved.
//

#include <iostream>
#include <vector>
// #include <string.h> // for c++14 (g++ 7.3.0)
using namespace std;

int num (char s [], int i, int l) {
int ret = s [i] - '0';
for (i++; i < l; i++) {
ret *= 10;
ret += s [i] - '0';
}
return ret;
}

void cast (char s [], int i, int l) {
for (; i < l; i++)
cout << s [i];
}

bool same (char s [], int i, int j) {
for (int k = 0; k < j - i; k++, (i++, j++))
if (s [i] != s [j])
return false;
return true;
}

int main () {
int t;
cin >> t;
while (t > 0) {
char s [31];
cin >> s;
int n = (int) strlen (s);
if (s [0] == '0') { // 測點 2,3 仍 WA -> 非 0 開頭
int half;
if (s [1] == '0' && s [2] == '0') { // 無此例之測點
cout << 1 << endl;
cout << "0 + 0 = 0" << endl;
} else if ((n & 0x1) == 1 && s [1] != '0' && same (s, 1, half = (n >> 1) + 1)) { // n % 1 == 1, 位數為奇數; n >> 1 即 n / 2; 測點 6; 無 00xx0xx... 之測資
cout << 1 << endl;
cout << "0 + "; // 0 + x = x
cast (s, 1, half);
cout << " = ";
cast (s, 1, half); // 或 cast (half, n)
cout << endl;
} else
cout << 0 << endl;
} else { // (加這個之前測點 2,3,5,10 WA, 之後 2,3,6 WA) -> 測點 6 為 0 開頭,正確答案為 k == 1
vector <pair <int, int> > combos; // 若用陣列,最多組合數: n-2 + ... + 1 < 500
for (int i = 1; i < n - 1; i++) // Todo: 可再根據加法位數法則減少迭代次數,不過線上測資並無對此有時間限制上的要求
for (int j = i + 1; j < n ; j++)
if ((j - i == 1 || s [i] != '0') && (n - j == 1 || s [j] != '0') && // 如果只有ㄧ位不必檢查前導 0 (測例 1023)
num (s, 0, i) + num (s, i, j) == num (s, j, n))
combos.push_back (make_pair (i, j));
cout << combos.size () << endl;
for (vector <pair <int, int> >::iterator iter = combos.begin (); iter != combos.end (); iter++) {
cast (s, 0, iter->first);
cout << " + ";
cast (s, iter->first, iter->second);
cout << " = ";
cast (s, iter->second, n);
cout << endl;
}
}
t--;
}
return 0;
}

 


文章類型: 一般 | Reply | Back
User Re:測點 2,3 WA

nandemoi SendIMessage
nandemoi

h176. A.A+B Problem | From: 111.250.59.89 | Post Date:2020-02-16 15:04:05.0

 
//
// main.cpp
// h176
//
// Created by Nandemoi on 2/16/20.
// Copyright © 2020 Elton Huang. All rights reserved.
//

#include
#include
// #include // for c++14 (g++ 7.3.0)
using namespace std;

int num (char s [], int i, int l) {
int ret = s [i] - '0';
for (i++; i < l; i++) {
ret *= 10;
ret += s [i] - '0';
}
return ret;
}

void cast (char s [], int i, int l) {
for (; i < l; i++)
cout << s [i];
}

bool same (char s [], int i, int j) {
for (int k = 0; k < j - i; k++, (i++, j++))
if (s [i] != s [j])
return false;
return true;
}

int main () {
int t;
cin >> t;
while (t > 0) {
char s [31];
cin >> s;
int n = (int) strlen (s);
if (s [0] == '0') { // 測點 2,3 仍 WA -> 非 0 開頭
int half;
if (s [1] == '0' && s [2] == '0') { // 無此例之測點
cout << 1 << endl;
cout << "0 + 0 = 0" << endl;
} else if ((n & 0x1) == 1 && s [1] != '0' && same (s, 1, half = (n >> 1) + 1)) { // n % 1 == 1, 位數為奇數; n >> 1 即 n / 2; 測點 6; 無 00xx0xx... 之測資
cout << 1 << endl;
cout << "0 + "; // 0 + x = x
cast (s, 1, half);
cout << " = ";
cast (s, 1, half); // 或 cast (half, n)
cout << endl;
} else
cout << 0 << endl;
} else { // (加這個之前測點 2,3,5,10 WA, 之後 2,3,6 WA) -> 測點 6 為 0 開頭,正確答案為 k == 1
vector int, int> > combos; // 若用陣列,最多組合數: n-2 + ... + 1 < 500
for (int i = 1; i < n - 1; i++) // Todo: 可再根據加法位數法則減少迭代次數,不過線上測資並無對此有時間限制上的要求
for (int j = i + 1; j < n ; j++)
if ((j - i == 1 || s [i] != '0') && (n - j == 1 || s [j] != '0') && // 如果只有ㄧ位不必檢查前導 0 (測例 1023)
num (s, 0, i) + num (s, i, j) == num (s, j, n))
combos.push_back (make_pair (i, j));
cout << combos.size () << endl;
for (vector <pair <int, int> >::iterator iter = combos.begin (); iter != combos.end (); iter++) {
cast (s, 0, iter->first);
cout << " + ";
cast (s, iter->first, iter->second);
cout << " = ";
cast (s, iter->second, n);
cout << endl;
}
}
t--;
}
return 0;
}


剩測點 2

 //

// main.cpp
// h176
//
// Created by Nandemoi on 2/16/20.
// Copyright © 2020 Elton Huang. All rights reserved.
//

#include <iostream>
#include <vector>
// #include <string.h> // for c++14 (g++ 7.3.0)
using namespace std;

unsigned long long num (char s [], int i, int l) { // 測點 3 為長位數
unsigned long long ret = s [i] - '0';
for (i++; i < l; i++) {
ret *= 10;
ret += s [i] - '0';
}
return ret;
}

void cast (char s [], int i, int l) {
for (; i < l; i++)
cout << s [i];
}

bool same (char s [], int i, int j) {
for (int k = 0; k < j - i; k++, (i++, j++))
if (s [i] != s [j])
return false;
return true;
}

int main () {
int t;
cin >> t;
while (t > 0) {
char s [31];
cin >> s;
int n = (int) strlen (s);
if (s [0] == '0') { // 測點 2,3 仍 WA -> 非 0 開頭
int half;
if (s [1] == '0' && s [2] == '0') { // 無此例之測點
cout << 1 << endl;
cout << "0 + 0 = 0" << endl;
} else if ((n & 0x1) == 1 && s [1] != '0' && same (s, 1, half = (n >> 1) + 1)) { // n % 1 == 1, 位數為奇數; n >> 1 即 n / 2; 測點 6; 無 00xx0xx... 之測資
cout << 1 << endl;
cout << "0 + "; // 0 + x = x
cast (s, 1, half);
cout << " = ";
cast (s, 1, half); // 或 cast (half, n)
cout << endl;
} else
cout << 0 << endl;
} else { // (加這個之前測點 2,3,5,10 WA, 之後 2,3,6 WA) -> 測點 6 為 0 開頭,正確答案為 k == 1
vector <pair <int, int> > combos; // 若用陣列,最多組合數: n-2 + ... + 1 < 500
for (int i = 1; i < n - 1 && i <= 20; i++) // 測點 3 為長位數
for (int j = i + 1; j < n && j <= 20; j++) // Todo: 可再根據加法位數法則減少迭代次數,不過線上測資並無對此有時間限制上的要求
if ((j - i == 1 || s [i] != '0') && (n - j == 1 || s [j] != '0') && // 如果只有ㄧ位不必檢查前導 0 (測例 1023)
num (s, 0, i) + num (s, i, j) == num (s, j, n))
combos.push_back (make_pair (i, j));
cout << combos.size () << endl;
for (vector <pair <int, int> >::iterator iter = combos.begin (); iter != combos.end (); iter++) {
cast (s, 0, iter->first);
cout << " + ";
cast (s, iter->first, iter->second);
cout << " = ";
cast (s, iter->second, n);
cout << endl;
}
}
t--;
}
return 0;
}

 

 


文章類型: 一般 | Reply | Back

New Thread

ZeroJudge Forum