C++程序 求解答

\u6c42C\u6216\u8005C++\u7a0b\u5e8f

\u5c06m=0,1,2,3,4,5,6\u7684\u6240\u6709\u53ef\u80fd\u7f57\u5217\u51fa\u6765\uff0c\u5171\u6709110\u4e07\u7ec4\u5408\u3002
\u7a0b\u5e8f\u89c1\u9644\u4ef6t8.exe\uff0c\u5404\u79cd\u7ec4\u5408\u89c1\u9644\u4ef61.txt\uff0c\u6e90\u7a0b\u5e8f\u89c1t8.cpp\u3002

// \u539f\u5f0f= (21*22*23*...*40)/(1*2*3*...*20)// \u7b97\u6cd5\u7b80\u8ff0\uff1a\u62ff\u5206\u6bcd\u4e2d\u768420\u4e2a\u6570\u6765\u5faa\u73af\uff0c\u62c6\u5f00\u6210\u8d28\u6570\u76f8\u4e58\uff0c//\u5e76\u62ff\u6b64\u8d28\u6570\u548c\u5206\u5b50\u4e2d\u7684\u6570\u7ea6\u5206\uff0c \u76f4\u81f3\u5206\u6bcd\u4e2d\u768420\u4e2a\u6570\u5168\u662f1\u4e3a\u6b62
#include
int isPrime(int n) { int flag = 1; int i; for (i = 2; i <= sqrt(n); i++) { if (n % i == 0) { return 0; } } return flag;}
int getLeastPrimeFactor(int n) { int i; for (i = 2; i <= n / 2; i++) { if (isPrime(i)) { if (n % i == 0) { return i; } } } return n;}
int main() { int i, j; int bigNum[20]; int smallNum[20]; for (i = 0; i < 20; i++) { bigNum[i] = 21 + i; smallNum[i] = 1 + i; } for (i = 0; i < 20; i++) { while (smallNum[i] != 1) { int leastPrimeFactor = getLeastPrimeFactor(smallNum[i]); for (j = 0; j < 20; j++) { if (bigNum[j] % leastPrimeFactor == 0) { bigNum[j] = bigNum[j] / leastPrimeFactor; smallNum[i] = smallNum[i] / leastPrimeFactor; break; } } } } long long ans = 1; for (i = 0; i < 20; i++) { ans = ans * bigNum[i]; } printf("%lld\n", ans); return 0;}

/*
name : zhangsan, number = 1
name : Jenny, number = 2
name : Randy, number = 3
Press any key to continue
*/
#include <iostream>
#include <string.h>
using namespace std;

class Student {
static int number;
char name[20]; // string的实例是受保护的,所以用char []
// char *name;
public:
Student() {}
Student(const char *str) { strcpy(name,str); ++number; } // 转换构造函数
void set(const char *str) { strcpy(name,str); ++number; }
void show() const { cout<<"name : "<<name<<", number = "<<number<<endl; }
};

void fn() {
Student s1 = Student("Jenny");
Student s2;
s1.show();
s2.set("Randy");
s2.show();
}

int Student::number = 0; // 类的静态数据成员初始化,很奇怪的!

int main() {
Student s = Student("zhangsan");
s.show();
fn();
return 0;
}

#include<iostream>
#include<string>
using namespace std;

class Student{
static int number;
string name;
// char *name;
public:
void set(string str){
name=str;
++number;
}
void show(){
cout<<"The name is"<<name<<",the number is"<<number<<endl;
}
};

void fn(){

Student s1;
s1.set("Jenny");
Student s2;
s2.set("Randy");
s1.show();
}
int Student::number=0;

int main(){
Student s;
s.set("zhangsan");
fn();
s.show();
return 0;

}
问题比较多,主要是以下两个:
1.程序中要用到string类时,头文件应该是#include<string>
2.静态数据成员需要在类外初始化,就是这一句:int Student::number=0;
注意,在类外访问类的静态变量时,需要加Student::限定符。
还有,用了int main()时,要有返回值0.

#include<iostream>
#include<string>
//#include<string.h>
using namespace std;

class Student
{
static int number;
string name;
// char *name;
public:
void set(string str)
{
name=str;
++number;
}
void show()
{
cout<<"The name is"<<name.c_str()<<",the number is"<<number<<endl;
}
};

void fn()
{

Student s1;
s1.set("Jenny");
Student s2;
s2.set("Randy");
s1.show();
}

int main()
{
Student s;
s.set("zhangsan");
fn();
s.show();
return 0;
}
这样可以了!

添加一句初始化
可以在Class Student后面
int Student::number;
或者
int Student::number = 0;

扩展阅读:c++编程软件 ... c十十入门编程 ... c++编程入门自学 ... c++下载 ... c入门教程视频 ... c++编程题经典100例 ... c++教程 ... c++入门编程题目 ... c#简单小游戏大全 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网