xml地图|网站地图|网站标签 [设为首页] [加入收藏]
查找元素习题集,构造方法
分类:编程

 

/*

B1004. 成绩排名 (20)

/*

 构造方法:

Description:

 if语句基本使用

 

读入n名学生的姓名、学号、成绩,分别输出成绩最高和成绩最低学生的姓名和学号。

 OC:

 作用: 对实例对象的内容进行初始化

Input:

 int age1 = 10;

 Swift要求类或者结构体中的存储属性(非lazy在)在对象构造完毕后要有初始化值

每个测试输入包含1个测试用例,格式为:

 int age2 = 20;

 

第1行:正整数n
  第2行:第1个学生的姓名 学号 成绩
  第3行:第2个学生的姓名 学号 成绩
  ... ... ...
  第n+1行:第n个学生的姓名 学号 成绩

 int max;

 语法:

其中姓名和学号均为不超过10个字符的字符串,成绩为0到100之间的一个整数,这里保证在一组测试用例中没有两个学生的成绩是相同的。

 max = age2;

 init(参数列表){ 初始化代码 }

Output:

 if (age1 > age2) {

 

对每个测试用例输出2行,第1行是成绩最高学生的姓名和学号,第2行是成绩最低学生的姓名和学号,字符串间有1空格。

 max = age1;

 注意: 1.在Swift中类/结果提/枚举都需要构造方法;

Sample Input:

 }

 2.构造方法的作用仅仅是用语初始化属性, 而不是分配内容, 分配内存是系统帮我们做的;

3
Joe Math990112 89
Mike CS991301 100
Mary EE990830 95

 NSLog(@"%d", max);

 3.构造方法是隐式调用的, 通过 类名称() 形成创建一个对象就会隐式调用 init() 构造方法;

Sample Output:

 

 4.如果所有的存储属性都有默认值, 可以不提供构造方法, 系统会提供一个隐式的构造方法;

Mike CS991301
Joe Math990112

 if (age1 > age2) {

 5.如果存储属性可以提供缺省, 那么提倡大家使用设置缺省值的方法, 这样可以简化代码(不用自定义构造方法, 不用写存储属性类型)

 1 #include <cstdio>
 2 
 3 struct Student{
 4     char name[15];
 5     char id[15];
 6     int score;
 7 }temp, ans_max, ans_min;
 8 
 9 int main()
10 {
11     int n;
12     scanf("%d", &n);
13     ans_max.score = -1;
14     ans_min.score = 101;
15     for(int i=0; i<n; ++i) {
16         scanf("%s%s%d", temp.name, temp.id, &temp.score);
17         if(temp.score > ans_max.score)  ans_max = temp;
18         if(temp.score < ans_min.score)  ans_min = temp;
19     }
20 
21     printf("%s %sn%s %sn", ans_max.name, ans_max.id, ans_min.name, ans_min.id);
22 
23     return 0;
24 }

 max = age1;

 */

 

 }else

 

B1028. 人口普查 (20)

 {

class Person {

Description:

 max = age2;

    var name: String = "hjq"

某城镇进行人口普查,得到了全体居民的生日。现请你写个程序,找出镇上最年长和最年轻的人。

 }

//    var age: Int = 20

这里确保每个输入的日期都是合法的,但不一定是合理的——假设已知镇上没有超过200岁的老人,而今天是2014年9月6日,所以超过200岁的生日和未出生的生日都是不合理的,应该被过滤掉。

 NSLog(@"%d", max);

    var age:Int

Input:

 

    func description() -> String {

输入在第一行给出正整数N,取值在(0, 105];随后N行,每行给出1个人的姓名(由不超过5个英文字母组成的字符串)、以及按“yyyy/mm/dd”(即年/月/日)格式给出的生日。题目保证最年长和最年轻的人没有并列。

 如果只有一条指令if后面的大括号可以省略

        return "name = (name) age = (age)"

Output:

 

    }

在一行中顺序输出有效生日的个数、最年长人和最年轻人的姓名,其间以空格分隔。

 Swift:

    init() {

Sample Input:

 if 条件表达式 {指令}   if 条件表达式 {指令} else{指令}

        print("init")

5
John 2001/05/12
Tom 1814/09/06
Ann 2121/01/30
James 1814/09/05
Steve 1967/11/20

 0.if后的圆括号可以省略

        age = 30

Sample Output:

 1.只能以bool作为条件语句

    }

3 Tom John

 2.如果只有条指令if后面的大括号不可以省略

}

 1 #include <cstdio>
 2 #include <cstring>
 3 
 4 #define MaxSize 11
 5 struct birthday {
 6     char name[MaxSize];
 7     char date[MaxSize];
 8 }temp, maxn, minn;
 9 char floor[MaxSize] = "1814/09/06", upper[MaxSize] = "2014/09/06";
10 
11 int main()
12 {
13     //freopen("E:\Temp\input.txt", "r", stdin);
14 
15     for(int i=0; i<MaxSize; ++i) {
16         maxn.date[i] = floor[i];
17         minn.date[i] = upper[i];
18     }
19     int N;
20     scanf("%d", &N);
21     int counter = N;
22     for(int i=0; i<N; ++i) {
23         scanf("%s %s", temp.name, temp.date);
24         if(strcmp(temp.date, floor)<0 || strcmp(temp.date, upper)>0)
25             --counter;
26         else {
27             if(strcmp(temp.date, maxn.date) >= 0)   maxn = temp;
28             if(strcmp(temp.date, minn.date) <= 0)   minn = temp;
29         }
30     }
31 
32     if(counter != 0)
33         printf("%d %s %sn", counter, minn.name, maxn.name);
34     else
35         printf("0n");
36 
37     return 0;
38 }

 1 #include <cstdio>
 2 
 3 struct person {
 4     char name[10];
 5     int yy, mm, dd;
 6 }oldest, youngest, left, right, temp;
 7 
 8 bool LessEqu(person a, person b)
 9 {
10     if(a.yy != b. yy)   return a.yy <= b.yy;
11     else if(a.mm != b.mm)   return a.mm <= b.mm;
12     else return a.dd <= b.dd;
13 }
14 bool MoreEqu(person a, person b)
15 {
16     if(a.yy != b. yy)   return a.yy >= b.yy;
17     else if(a.mm != b.mm)   return a.mm >= b.mm;
18     else return a.dd >= b.dd;
19 }
20 void init()
21 {
22     youngest.yy = left.yy = 1814;
23     oldest.yy = right.yy = 2014;
24     youngest.mm = oldest.mm = left.mm = right.mm = 9;
25     youngest.dd = oldest.dd = left.dd = right.dd = 6;
26 }
27 
28 int main()
29 {
30     init();
31     int n, num = 0;
32     scanf("%d", &n);
33     for(int i=0; i<n; ++i) {
34         scanf("%s %d/%d/%d", temp.name, &temp.yy, &temp.mm, &temp.dd);
35         if(MoreEqu(temp, left) && LessEqu(temp, right)) {
36             num++;
37             if(LessEqu(temp, oldest))  oldest = temp;
38             if(MoreEqu(temp, youngest)) youngest = temp;
39         }
40     }
41 
42     if(num == 0)    printf("0n");
43     else printf("%d %s %sn", num, oldest.name, youngest.name);
44 
45     return 0;
46 }

 */

// 1.分配内存; 2.初始化name和age; 3.构造方法是隐式调用的

 

var age1 = 10

var p = Person()

B1032. 挖掘机技术哪家强 (20)

var age2 = 20

var descripString: String = p.description() //显示调用

Description:

var max:Int

print(p.age)

为了用事实说明挖掘机技术到底哪家强,PAT组织了一场挖掘机技能大赛。现请你根据比赛结果统计出技术最强的那个学校。

max = age2

 

Input:

if age1 > age2

print("================================")

输入在第1行给出不超过105的正整数N,即参赛人数。随后N行,每行给出一位参赛者的信息和成绩,包括其所代表的学校的编号(从1开始连续编号)、及其比赛成绩(百分制),中间以空格分隔。

{

 

Output:

    max = age1

 

在一行中给出总得分最高的学校的编号、及其总分,中间以空格分隔。题目保证答案唯一,没有并列。

}

/** 带有参数的构造方法 **/

Sample Input:

print(max)

class Person2 {

6
3 65
2 80
1 100
2 70
3 40
3 0

 

    var name:String

Sample Output:

if age1 > age2

    var age:Int

2 150

{

    func description() -> String {

 1 #include <cstdio>
 2 
 3 const int maxn = 100010;
 4 int school[maxn];
 5 
 6 int main()
 7 {
 8     int n, schID, score;
 9     scanf("%d", &n);
10     for(int i=0; i<n; ++i) {
11         scanf("%d%d", &schID, &score);
12         school[schID] += score;
13     }
14 
15     int k = 1, MAX = -1;
16     for(int i=1; i<=n; ++i) {
17         if(school[i] > MAX) {
18             MAX = school[i];
19             k = i;
20         }
21     }
22 
23     printf("%d %dn", k, MAX);
24 
25     return 0;
26 }

    max = age1

        return "name = (name) age = (age)"

 

}else

    }

A1011. World Cup Betting (20)

{

    //1.构造方法的内部参数, 默认也是外部参数;

Description:

    max = age2

    //2.而函数的内部参数默认不会当做外部参数;

With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excited as the best players from the best teams doing battles for the World Cup trophy in South Africa. Similarly, football betting fans were putting their money where their mouths were, by laying all manner of World Cup bets.

}

    //3.而方法的内部参数, 从第二个开始才会当做外部参数;

Chinese Football Lottery provided a "Triple Winning" game. The rule of winning was simple: first select any three of the games. Then for each selected game, bet on one of the three possible results -- namely W for win, T for tie, and L for lose. There was an odd assigned to each result. The winner's odd would be the product of the three odds times 65%.

print(max)

    //4.构造方法对属性的顺序没有要求, 只要保证对象构造完时所有存储属性被初始化即可.

For example, 3 games' odds are given as the following:

 

    init(age:Int, name:String)

 W    T    L
1.1  2.5  1.7
1.2  3.0  1.6
4.1  1.2  1.1

/*

    {

To obtain the maximum profit, one must buy W for the 3rd game, T for the 2nd game, and T for the 1st game. If each bet takes 2 yuans, then the maximum profit would be (4.1*3.0*2.5*65%-1)*2 = 37.98 yuans (accurate up to 2 decimal places).

 多分支

        self.name = name

Input:

 OC:

        self.age = age

Each input file contains one test case. Each case contains the betting information of 3 games. Each game occupies a line with three distinct odds corresponding to W, T and L.

 float score = 99.9;

    }

Output:

 if (score >= 90) {

    func setName(name:String, age:Int)

For each test case, print in one line the best bet of each game, and the maximum profit accurate up to 2 decimal places. The characters and the number must be separated by one space.

 NSLog(@"优秀");

    {

Sample Input:

 }else

        self.name = name

1.1 2.5 1.7
1.2 3.0 1.6
4.1 1.2 1.1

 {

        self.age = age

Sample Output:

 if (score >= 60) {

    }

T T W 37.98

 NSLog(@"良好");

}

 1 #include <cstdio>
 2 
 3 char S[3] = {'W', 'T', 'L'};
 4 
 5 int main()
 6 {
 7     double ans = 1, temp, a;
 8     int idx;
 9     for(int i=0; i<3; ++i) {
10         temp = 0;
11         for(int j=0; j<3; ++j) {
12             scanf("%lf", &a);
13             if(a > temp) {
14                 temp = a;
15                 idx = j;
16             }
17         }
18         ans *= temp;
19         printf("%c ", S[idx]);
20     }
21 
22     printf("%.2fn", (ans*0.65-1)*2);
23 
24     return 0;
25 }

 }else

var p2 = Person2(age: 25, name: "hjq")

 

 {

p2.setName(name: "hjq", age: 30)

A1006. Sign In and Sign Out (25)

 NSLog(@"不给力");

print(p2.description())

Description:

 }

 

At the beginning of every day, the first person who signs in the computer room will unlock the door, and the last one who signs out will lock the door. Given the records of signing in's and out's, you are supposed to find the ones who have unlocked and locked the door on that day.

 }

p2.setName(name: "hjq", age: 23)

Input:

 

print(p2.description())

Each input file contains one test case. Each case contains the records for one day. The case starts with a positive integer M, which is the total number of records, followed by M lines, each in the format:

 if (score >= 90) {

 

ID_number Sign_in_time Sign_out_time

 NSLog(@"优秀");

print("================================")

where times are given in the format HH:MM:SS, and ID number is a string with no more than 15 characters.

 }else if (score >= 60)

 

Output:

 {

/*

For each test case, output in one line the ID numbers of the persons who have unlocked and locked the door on that day. The two ID numbers must be separated by one space.

 NSLog(@"良好");

 常量存储属性与构造方法

Note: It is guaranteed that the records are consistent. That is, the sign in time must be earlier than the sign out time for each person, and there are no two persons sign in or out at the same moment.

 }else

 常量存储属性只能通过缺省值或在构造方法中被修改, 其它任何地方都不能修改

Sample Input:

 {

 */

3
CS301111 15:30:28 17:00:10
SC3021234 08:00:00 11:25:25
CS301133 21:45:00 21:58:40

 NSLog(@"不给力");

class Person3 {

Sample Output:

 }

    var name:String = "hjq"

SC3021234 CS301133

 */

    var age:Int

 1 #include <cstdio>
 2 #include <cstring>
 3 
 4 #define MaxSize 20
 5 struct Person {
 6     char id[MaxSize];
 7     char in[MaxSize];
 8     char out[MaxSize];
 9 }temp, In, Out;
10 
11 int main()
12 {
13     //freopen("E:\Temp\input.txt", "r", stdin);
14 
15     char a[] = "00:00:00", b[] = "23:59:59";
16     for(int i=0; i<9; ++i) {
17         In.in[i] = Out.in[i]  = b[i];
18         In.out[i] = Out.out[i]  = a[i];
19     }
20     int N;
21     scanf("%d", &N);
22     for(int i=0; i<N; ++i) {
23         scanf("%s %s %s", temp.id, temp.in, temp.out);
24         if(strcmp(temp.in, In.in) < 0) In = temp;
25         if(strcmp(temp.out, Out.out) > 0) Out = temp;
26     }
27 
28     printf("%s %sn", In.id, Out.id);
29 
30     return 0;
31 }

 1 #include <cstdio>
 2 
 3 struct pNode {
 4     char id[20];
 5     int hh, mm, ss;
 6 }temp, ans1, ans2;
 7 
 8 bool great(pNode node1, pNode node2) {
 9     if(node1.hh != node2.hh)    return node1.hh > node2.hh;
10     if(node1.mm != node2.mm)    return node1.mm > node2.mm;
11     return node1.ss > node2.ss;
12 }
13 
14 int main()
15 {
16     int n;
17     scanf("%d", &n);
18     ans1.hh = 24, ans1.mm = 60, ans1.ss = 60;
19     ans2.hh = 0, ans2.mm = 0, ans2.ss = 0;
20     for(int i=0; i<n; ++i) {
21         scanf("%s %d:%d:%d", temp.id, &temp.hh, &temp.mm, &temp.ss);
22         if(great(temp, ans1) == false)  ans1 = temp;
23         scanf("%d:%d:%d", &temp.hh, &temp.mm, &temp.ss);
24         if(great(temp, ans2) == true)   ans2 = temp;
25     }
26 
27     printf("%s %sn", ans1.id, ans2.id);
28 
29     return 0;
30 }

 

    init(age:Int, name:String)

 

var score = 99.9;

    {

A1036. Boys vs Girls (25)

if score >= 90

        self.name = name

Description:

本文由澳门新葡亰手机版发布于编程,转载请注明出处:查找元素习题集,构造方法

上一篇:第五十三题,打家劫舍 下一篇:运算符的优先级顺序
猜你喜欢
热门排行
精彩图文