Hang electrical re-examination written examination 2010-2012

Topic 1: The guessing game is not too difficult. Topic: then generates a 3-digit positive integers, let you carry on guessing, guess if small, output: "Guess small, please continue." If you guess the big, output: "Guess big, please continue." If you guessed right. Output: "Congratulations, you guessed it." But I can only guess 10 times, 10 times if guess not guess right, you exit the program, output: "Unfortunately."

Is a three-digit randomly generated 100 + rand (% 900);

#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
using namespace std;
//题目1:猜数字的游戏,不太难的。题目:随即产生一个3位的正整数,让你进行猜数字,如果猜小了,输出:“猜小了,请继续”
//。如果猜大了,输出:“猜大了,请继续”。
//如果猜对了。输出:“恭喜你,猜对了”。不过最多只能猜10次,如果猜了10次还没有猜对,就退出程序,输出:“很遗憾”。

int main()
{
    int num;//这个是随机生成的数字,并且是三位数的
    //猜十次的话,就用一个while循环吧,并且一个判断位置,他是猜对了跳出循环还是猜错了跳出循环
    int i=10;
    int flag=0;
    int a;//这是每次用来猜的数字
    num=(100+rand()%900);//这招不错
    printf("%d\n",num);
    while(i){
        scanf("%d",&a);
        if(a==num){
            flag=1;
            break;
        }
        else if(a>num)
          printf("猜大了,请继续\n");
        else if(a<num)
            printf("猜小了,请继续\n");
        i--;
    }
    if(flag==1)
        printf("恭喜你,猜对了\n");
    else
        printf("很遗憾\n");


}

Problem 2: The number string is extracted, and the sum. The "No Signal", "123, and456", "12, 123and124"
operation results are 0,579,259

#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
using namespace std;
//题目2:将字符串中的数字提取出来,并加和。如“No       Signal ”,“123,and456”,“12, 123and124”
//运行结果分别是0,579,259
int main()
{
    //创建一个字符串数组
    char num[100];
    scanf("%s",num);//输入,注意这个不需要写&
    int le=strlen(num);
    int i,sum=0;
    //开始遍历
    for(i=0;i<le;i++){
        if(num[i]>='0'&&num[i]<='9'){
            sum+=(num[i]-'0');//从字符串变成数字
        }
    }
    printf("%d",sum);
}

Topic 3: processing a file student.txt, then the message text is sorted out, which means that the text of each column information: Name student number English Language Mathematical Sciences
Zhang 2,010,060,178,896,275
John Doe 20100602785498 86
king five 2,010,060,378,698,575

This question is the problem that how to read the file contents

#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
using namespace std;
//题目3:
//处理一个文件 student.txt,然后将文本中的信息按总分排序,其中文本每列信息的意思是:
//姓名 学号 英语 语文 数学 科学
//张三 20100601 78 89 62 75
//李四 20100602 78 54 98 86
//王五 20100603 78 69 85 75
//首先肯定是要 创建一个结构体数组来存储
struct M{
    char name[10];
    char id[10];
    int english;
    int yuwen;
    int math;
    int kexue;
    int sum;
}stu[10];
//写方法,按照总分从大到小
bool cmp(M a,M b){
    a.sum=a.english+a.kexue+a.math+a.yuwen;
    b.sum=b.english+b.kexue+b.math+b.yuwen;
    return a.sum>b.sum;
}
int main()
{
    int i;
    int c=0;
    char str[20];
    //然后就是最关键的读取文件了
    FILE *fpread=fopen("student.txt","r");//打开并读取文件
    if(fpread==NULL)
        return 0;

    for(i=0;i<6;i++)
    {
        fscanf(fpread,"%s",&str);//这是第一行嘛
        printf("%s ",str);
    }
    printf("\n");
    while(fscanf(fpread,"%c",&stu[c].name)!=EOF){
        fscanf(fpread,"%c",&stu[c].id);
        fscanf(fpread,"%d",&stu[c].english);
        fscanf(fpread,"%d",&stu[c].yuwen);
        fscanf(fpread,"%d",stu[c].math);
        fscanf(fpread,"%d",&stu[c].kexue);
        c++;
    }
    //开始排序
    sort(stu,stu+c,cmp);
    //开始输出
    for(i=0;i<c;i++)
        printf("%c %c %d %d %d %d",stu[i].name,stu[i].id,stu[i].english,stu[i].yuwen,stu[i].math,stu[i].kexue);
}


Problem 1: Input three positive integers A, B, C. Analyzing these three numbers can not form a triangle.

#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
using namespace std;
//题目1:输入三个正整数A、B、C。判断这三个数能不能构成一个三角形。
int main()
{
    int a,b,c;
    scanf("%d%d%d",&a,&b,&c);//输入三个边长
    if(a+b>c&&a+c>b&&c+b>a)
        printf("YES");
    else
        printf("No");
}

Topic 2: someone from January 1, 2003 start, in fits and starts, enter the month, day, and asked him one day in the year or in the fishing nets drying.

#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
using namespace std;
//题目2:有个人从2003年1月1日开始,三天打鱼两天晒网,请输入月份、日期,问在当年的某一天他是在打鱼还是在晒网。
int main()
{
    //首先要知道,2003年不是闰年,即二月只有28天
    //创建一个数组,来存每个月的天数
    int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    int m,d;//存储输入的月份和天数
    int i;
    scanf("%d%d",&m,&d);
    int sum=0;//记录总天数
    for(i=0;i<m;i++){
        sum+=a[i];
    }
    sum+=d;//当前该月的总天数
    //然后与5进行判断,求余数
    sum%=5;
    if(sum>=1&&sum<=3)
        printf("打鱼");
    else
        printf("晒网");
}

Problem 3: ugly number is defined as: If a positive integer prime factor contains only 2,3,5,7 four kinds, it is called ugly number. The number of columns 1, 2, 3, 4,
5,6,7,8,9, 10,12,14,15,16,18, 20, 21,24,25, 27 .......... 20 is displayed before a number of ugly.
Gives a positive integer N, the number is a number determined ugly.

This problem is fine

#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
using namespace std;
//题目3:
//丑数是这样定义的:如果一个正整数的素因子只包含 2、3、5、7四种,则它被称为丑数。以下数列 1, 2, 3,4, 5,6,7,8,9, 10,12,14,15,16,18, 20, 21,24,25, 27………. 就显示了前20个丑数。
//给出一个正整数N,判断这个数是否为丑数。
//参考了别人的代码,因为这道题用正常的方法写会超时,所以可以用动态规划
//写一个方法来找最小值
int minn(int a,int b,int c,int d){
    return min(a,min(b,min(c,d)));
}
int main()
{
    int i,flag=0;
    int dp[1000];//这个是用来存储丑数的
    dp[1]=1;//第一个丑数是1 而根据因子有2,3,5,7这几个。所以之后是2,3,2*2,5,
    int f2=1,f3=1,f5=1,f7=1;//这些用来存 那些由对应的因子生成的系数
    for(i=2;i<1000;i++){
        dp[i]=minn(dp[f2]*2,dp[f3]*3,dp[f5]*5,dp[f7]*5);
        if(dp[i]==dp[f2]*2) f2++;//这个数组多存了一个
        if(dp[i]==dp[f3]*3) f3++;
        if(dp[i]==dp[f5]*5) f5++;
        if(dp[i]==dp[f7]*7) f7++;
    }
    int num;//输入该正整数
    scanf("%d",&num);
    for(i=1;i<=num;i++)
        if(dp[i]==num)
            flag=1;
    if(flag==1)
        printf("YES");
    else
        printf("NO");


}

Problem 1: enter a decimal number, turn it into hexadecimal.

#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
using namespace std;
//题目1:输入一个十进制的数,把它转成十六进制。
//需要注意的是 当余数大于等于10的时候是需要-10+'A'
int main()
{
    int num;//要输入的正整数
    int temp;
    char a[100];//存输入的值,因为存在字符,所以不适合用int 类型
    scanf("%d",&num);
    int j,i=0;
    while(num){
        temp=num%16;
        if(temp<10)
            a[i]=temp+'0';//将整数转换为字符
        else
            a[i]=temp-10+'A';
        num/=16;
        i++;
    }
    //然后开始输出
    for(j=i-1;j>=0;j--){
        printf("%c",a[j]);
    }
}

The second title in 2012, virtually no one to write, so I do not write it

Published 72 original articles · won praise 5 · Views 2806

Guess you like

Origin blog.csdn.net/qq_41115379/article/details/104945792