我正在阅读 Cracking the Coding Interview 并做练习题,但我一直坚持这个问题:
“实现一种算法来查找单向链表的第 k 个到最后一个元素。”
我的递归函数没有返回任何东西,我不知道为什么。在递归函数中,我采用了 3 个参数。K将是我们想要找出的最后一个元素的位置。Node* temp将是头节点, int i 将保留最后一个节点的元素计数。
#include<bits/stdc++.h>
using namespace std;
class Node
{
int data;
Node* next;
public:
Node(){
data=0;
next=NULL;
}
friend class LinkedList;
};
class LinkedList
{
public:
Node* head;
public:
LinkedList(){
head=NULL;
}
void append(int data){
Node* temp= new Node();
temp->data=data;
temp->next=NULL;
if (head==NULL){
head=temp;
}
else{
Node* ptr=head;
while(ptr->next!=NULL){
ptr=ptr->next;
}
ptr->next=temp;
}
}
void display(){
Node* ptr=head;
if(head==NULL){
cout<<"List is empty"<<endl; …Run Code Online (Sandbox Code Playgroud) 我知道在 C++ 中扩展内置类已被弃用,但由于某些原因我仍然想这样做。
我想将自定义方法添加到一个类 ( str) 中,该类扩展/继承自该类std::string(这是一个示例)
std::string但是,当我这样做时,类中已有的方法(内置方法)会遇到一些问题,例如,
#include <bits/stdc++.h>
using namespace std;
class str : public string {
public:
string s;
str(string s1) {
s = s1; //ig the problem is here
}
};
int main() {
str s("hello world");
cout << s.size(); //prints out 0 not 11 => length of "hello world"
}
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
考虑下面的代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<int> a(n);
int sum = 0;
for (auto &it : a) {
cin >> it;
sum += it;
}
cout << sum << "\n";
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
cout << endl;
}
Run Code Online (Sandbox Code Playgroud)
输入类似(或任何大于 INT_MAX 的值到 k 中)
5 1234567891564
1 2 3 4 …Run Code Online (Sandbox Code Playgroud) 我编写这段代码是为了获取用户输入的数字的素因数。
#include<bits/stdc++.h>
using namespace std;
void prime_Factors(int);
bool isPrime(int);
int main()
{
int num;
cout << "Enter the number to find it's prime factors: ";
cin >> num;
prime_Factors(num);
}
void prime_Factors(int n1)
{
for(int i = 2; i<n1; i++)
{
if(isPrime(i))
{
int x = i;
while(n1%x==0)
{
cout << i << " ";
x *= i;
}
}
}
}
bool isPrime(int n0)
{
if(n0==1)
return false;
for(int i = 0; i*i <= n0; i++)
{
if(n0%i==0)
return …Run Code Online (Sandbox Code Playgroud) 给定一个包含 N 个整数的 A 数组。如果一对索引 (i,j) (i < j) 的元素的乘积是完全平方,则这些索引对称为“完美对”。完全平方是一个正整数,它是一个整数与其自身的乘积。求 A 中完美对的数量。
我尝试解决这个问题。最初我在地图上记录了数字。我的方法是,如果数组中有 1 和一个完全平方数,我就会计算它们。然后我会检查数组中的数字,如 (3,3,3),这将产生 3 对,这是我从(数字计数(数字计数 - 1))/2 * 公式中得到的。但这种方法对于某些测试用例给出了错误的 ans,我不知道为什么。
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
vector<int> v(n,0);
for(int i=0;i<n;i++){
cin>>v[i];
}
int cnt=0;
int cnt1=0;
unordered_map<int,int> count;
for(int i=0;i<n;i++){
count[v[i]]++;
// count for 1's
if(v[i]==1){
cnt1++;
}
}
for(auto it: count){
//check how many perfect squares can be formed--> eg(3,3,3,3,3)
if(it.second!=0){
int n=it.second;
cnt+=(n*(n-1))/2;
}
//check for pair of 1 and …Run Code Online (Sandbox Code Playgroud) 我试图得到所有偶数斐波纳契数的总和.我可以打印出数字,但我无法得到它们的总和.这是在java中.
class driver {
public static void main(String [] args) {
int a;
int b = 0;
int c = 1;
for (int i = 0; i < 10; i++) { // Finds fibonacci sequence
a = b;
b = c;
c = a + b;
if ( c % 2 == 0) { // Check if it's even
int sum = 0;
sum = sum + c;
System.out.println(sum);
}
else {
}
}
}
}
Run Code Online (Sandbox Code Playgroud) #include <bits/stdc++.h>
Run Code Online (Sandbox Code Playgroud)
如果我将以上行放在program.cpp文件的顶部,则会显示以下错误消息:
无法打开源文件“ bits / stdc ++。h”
我怎样才能解决这个问题?
这是我的代码,它采用4个字符串的1和0,并使用bitset函数转换为十进制.它返回所有组合的正确值,除了那些涉及11和10的组合{1110,1010,1011,1111}.对于这些数字,它返回忽略MSB的结果.这是因为1010它给出了2作为答案.
#include<bits/stdc++.h>
using namespace std;
#define ul unsigned long
int main(int argc, char const *argv[])
{
int bin1=0,bin2=0,choice=0;
ul x1=0,x2=0;
//string binary;
cin>>bin1;
x1=bitset<4>(bin1).to_ulong();
cin>>bin2;
x2=bitset<4>(bin2).to_ulong();
cout<<x1<<" "<<x2<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编辑这里是我的结果的快照
读取另一组输入的同一程序的另一个快照,但这次它给出了正确的输出.顺便说一句的1101和1001是输入和接下来的两个酸橙在输出
如果条件块内部没有打印任何值。逻辑错误在哪里?谢谢。
#include<bits/stdc++.h>
using namespace std;
int fx[]= {-1,-1,-1,0,1,1,1,0};
int fy[]= {-1,0,1,1,1,0,-1,-1};
int ar[20][20];
int n;
int v1, v2;
void fun(int a, int b)
{
for(int i=0; i<8; i++)
{
v1 = a+fx[i];
v2 = b+fy[i];
//cout<<v1<<" "<<v2<<endl;
if(v1>=0 && v1<n)
{
if(v2>=0 && v2<n)
{
// Not executing
cout<<"----------"<<endl;
cout<<v1<<" "<<v2<<endl;
}
}
}
}
int main()
{
int n;
cin>> n;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
cin>> ar[i][j];
}
fun(0,1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
样本输入:
4 …
(据我所知)C++ 不接受头文件上的“.h”扩展名(因为它通常不存在于其他包含语句中)所以包含如何<bits/stdc++.h>在 C++ 中工作以及为什么它有“.h”延期?
c++ ×9
arrays ×1
binary ×1
bitset ×1
class ×1
debugging ×1
fibonacci ×1
header-files ×1
java ×1
linked-list ×1
math ×1
oop ×1
overflow ×1
string ×1
visual-c++ ×1