- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
编程题是:
given a grid of numbers, how many distinct primes can you find embedded in the matrix, regarding that you can read the lines or part of them, in form vertical, horizontal or diagonal orientation, in both directions?
大编辑:对于阅读本文的任何可怜的灵魂,我通过将 if (n > INT_MAX) return false;
添加到我的函数下的第一行来解决这个问题!
只要 isPrime
的参数不是 const ll &n
,我的程序就不会崩溃,我不知道为什么。是因为你永远不应该将 long long
作为参数传递,因为它太大了?还是因为如果将 int
添加到 long long
整数,它就不再是 long long
?
我用来测试我的工作的输入是:
40
0251677085866837460317708637021446063144
8812262220360202463050064531874436437682
5251855367278508848642345043775871434078
0042675865438283025822603307175060748288
5672321632434878440388701468545837465571
3448326728143606881852187616524878044060
8876415778774852362710315274652021065556
1406474838287088561242126854006826771778
7827443331184330371521043472218803550383
6318874838447276075161123302780187880165
0884752758538865306583258450386821283658
1260362124615176735303563717773657467333
2580363145201308707655341168610513145546
4142635386876777348215436708254351251288
5301330463217260626047132625527161775404
8620446353006857360714856156584322276288
0813375760405334773480860674772272733638
6715558007108501053612008324501255710425
8840634327383685827335506853781648727036
8827728873376824454871536655067801443735
0664640563836487481174816586572628815173
7186752536147276768154002317573417465332
4438770023402783205544064640821537621225
4162442401558771474140203865162080237721
5008757506737224070577338578644664641338
2155803687408638660278862273674652462840
2118148017744113203720114756276821067158
4838003412436782114402742024145245437315
5161343527676283186170466281455700086618
7723886261287175705152273086317588317188
6653360024271146551000054710768617586846
0050014847531086708661266564560614115164
3351156208161708784441387827072734346251
0457546342466313073230326436563643506534
3837451141488371231210888733717540046582
3334248265835234158638343058444640886465
0173240266426385002380821305357684721128
0437020214873361352055818843664073456138
3858604586068245520287541000014334760058
5840781588142205318614583635575571714673
我的代码在这里:
#include <iostream>
#include <set>
#include <vector>
typedef long long ll;
const int DIRECTIONS[][2] = {{1, 0}, {0, 1}, {1, 1}, {-1, 0}, {0, -1}, {-1, -1}, {1, -1}, {-1, 1}};
bool isPrime(const int &n) {
if (n != 2) {
if (n < 2 || !(n & 1)) return false;
for (ll i = 3; i * i <= n + 1; i += 2) {
if (n % i == 0) return false;
}
}
return true;
}
int main() {
int length = 0;
std::set<ll> primes{};
char c;
std::cin >> length;
std::vector<std::vector<ll>> grid;
for (int i = 0; i < length; i++) {
std::vector<ll> row;
for (int j = 0; j < length; j++) {
std::cin >> c;
row.push_back(c - '0');
}
grid.push_back(row);
}
for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++) {
for (auto d : DIRECTIONS) {
int tempi = i, tempj = j;
ll base10 = 0;
while (i < length && j < length && i >= 0 && j >= 0) {
base10 *= 10;
base10 += grid[i][j];
if (isPrime(base10)) primes.insert(base10);
i += d[0];
j += d[1];
}
i = tempi;
j = tempj;
}
}
}
std::cout << "Number of primes: " << primes.size() << std::endl;
/*std::cout << "List of primes: " << std::flush;
for (auto prime : primes) {
std::cout << prime << " " << std::flush;
}*/
return 0;
}
最佳答案
程序在以下部分导致未定义的行为:
base10 *= 10;
base10 += grid[i][j]
因为这些计算有时会超过 long long
的最大值。
long long
不能包含超过 19 位十进制数字。如果目的是验证最多 40 位的素数,那么您将不得不切换到不同的方法。
关于c++ - 从不传递 long long 作为参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54818272/
我找到了long int long和 int long long可以编译为变量类型。 long int long有什么区别吗, int long long , long long和 long long
我无法找出为什么“加密”函数仍然将“消息”读取为字符串,尽管我已经使用不同的方法将数据类型更改为字节。 错误消息是“Prince 类型中的方法 Encrypt(long, long, long, lo
这个问题在这里已经有了答案: Is "long long" = "long long int" = "long int long" = "int long long"? (4 个答案) 关闭 6 年
我正在从 Java 过渡到 C++,并且对 long 数据类型有一些疑问。在 Java 中,要保存大于 232 的整数,您只需编写 long x;。但是,在 C++ 中,long 似乎既是数据类型又是
clang-tidy 12.0.1 报告了一个相当奇怪的警告。在以下代码中: #include int main() { std::vector v1; const auto a =
我创建了一个 pair 和 long long int 的映射 - map,long long int >; 和一个交互器 - map, long long int >::iterator it1;
我想知道 unsigned long long 和 unsigned long long int 的主要区别。它们可以互换使用吗? 对于像 9223372036854775807 这样的大十进制数的计
我看到的大多数代码都使用缩写类型来声明变量,例如 long long x; // long long int x short y; // short int y 我浏览了 C++11 标准(第 3.9
common_type::type是 unsigned long因为关于积分提升后的操作数,标准说... [...] if the operand that has unsigned integer
long long int A = 3289168178315264; long long int B = 1470960727228416; double D = sqrt(5); long lon
这些新数据类型的目的是什么?我通常只使用“int”或“long”,但为什么会存在这些呢?它们带来了什么新功能或用途? 最佳答案 long int一直是long的全称,只是很少用而已。 long lon
我正在运行以下for循环 for(unsigned long long int i = N-1; i >= 0; i--){ cin>>L[i]; } 当程序到达这个代码段时,它停止响应。但是
最近问了一个关于递归导致这个问题的问题 注意-> count() 函数返回键 K 在 map 容器中出现的次数。如果键存在于容器中,则返回 1,因为映射仅包含唯一键。如果 map 容器中不存在键,则返
好的,所以我正在尝试实现客户端 - 服务器程序(套接字编程)。 我的客户发送一个嵌入字符串中的 long long int,如下所示: char copy[10]; sprintf(send_data
如果我有任务 Long c = a + b; 有没有一种简单的方法来检查 a + b 不大于/小于 Long.MAX_VALUE/Long.MIN_VALUE? 最佳答案 使用 Guava , 就这么
我需要制作一个 Comparator 来根据它的 long 类型的变量之一对我的对象列表进行排序。 public class ParticipantIndexComparator implements
假设我有这两种类型: typedef unsigned long long uint64; typedef signed long long sint64; 我有这些变量: uint64 a = ..
long long 和 long 有什么区别?而且它们都不适用于 12 位数字 (600851475143),我是不是忘记了什么? #include using namespace std; int
当结果将大于 C 中的 long long int 时,是否有可能对两个不同的 long long int 变量求和? 最佳答案 由于 OP 想要“在屏幕上打印结果”,因此将数字分成两部分:Most-
实际上我必须找到从源顶点到所有其他顶点的最短路径。为此,我获得了下面给出的代码模板。我想实现“Bellman–Ford algorithm”。 #include #include #include
我是一名优秀的程序员,十分优秀!