zoukankan      html  css  js  c++  java
  • herizai_CD2所做答案

     1 //herizai_CD1第一题
     2 #include<iostream>
     3 #include<iomanip>
     4 using namespace std;
     5 
     6 void print1(int n)//输出一个正三角形
     7 {
     8  for(int i=0;i<=n;i++)//输出n行,第一行时i=1,第二行时i=2…对应下面每行*的个数
     9   {
    10    cout<<setw(30-i)<<" ";//在*前打出30-i个空格来占位置,从而达到使*居中的目的,与for(k=0;k<30-i;k++) cout<<" "; 一样的效果。
    11    for(int m=1;m<2*i;m++)//输出一行*, *的个数由循环次数i决定
    12    {
    13    cout<<"*";
    14    }
    15    cout<<endl;  //输完一行后换行
    16   }
    17 }
    18 
    19 void print2(int n)//输出一个倒三角形
    20 {
    21  for(int i=n;i>0;i--)//输出n行,第一行时i=1,第二行时i=2…对应下面每行*的个数
    22    {
    23     cout<<setw(30-i)<<" ";//在*前打出30-i个空格来占位置,从而达到使*居中的目的,与for(k=0;k<30-i;k++) cout<<" "; 一样的效果。
    24     for(int m=1;m<2*i;m++)//输出一行*, *的个数由循环次数i决定
    25     {
    26     cout<<"*";
    27     }
    28     cout<<endl;  //输完一行后换行
    29    }
    30 }
    31 
    32 void print3(int n)//输出一个空心四边形
    33 {
    34 int i,j; 
    35 cout<<setw(n+1);             //录入字符个数a
    36 for( i=0;i<n;i++)     //打印第一行a个‘*’
    37 cout<<"*";
    38 cout<<endl;
    39 for(i=0;i<n-2;i++)
    40 {
    41 cout<<setw(n-i)<<"*";                   //打印中间a-2行最左边的'*'
    42 for(j=0;j<n-2;j++)       //打印中间a-2行,第一行中的n-2个空白符
    43 cout<<" ";
    44 cout<<"*";                   //打印中间a-2行最右边的'*'
    45 cout<<endl;
    46 }
    47 cout<<" ";
    48 for(i=0;i<n;i++) 
    49     cout<<"*";  //打印最后一行a个‘*’
    50 cout<<endl;
    51 }
    52 
    53 void main()
    54 { char k;
    55  int m=5,choice;//m为三角形边长或是菱形的短对角线长
    56  do
    57  {
    58   cout<<"请选择(1/2/3)
     1 输出正三角形
     2 输出倒三角形 
     3 输出空心的平行四边形 
     4返回";
    59   cin>>choice;
    60   switch(choice)
    61   {
    62   case 1:print1(m);break;
    63   case 2:print2(m);break;
    64   case 3:print3(m);break;//正三角形和倒三角形拼成的菱形
    65   case 4:exit(1);
    66   }
    67   cout<<"是否继续(y/n)
    ";
    68   cin>>k;
    69  }while(k=='y'||k=='Y');
    70 }
    View Code
     1 //herizai_CD2第二题
     2 #include<iostream>
     3 using namespace std;
     4 void chengji(int Matrix[3][3])
     5 {
     6     int s=1;
     7     for(int i=0;i<3;i++)
     8         for(int j=0;j<3;j++)
     9             if(i==j)
    10             s*=Matrix[i][j];
    11     cout<<"chengji is:"<<s<<endl;
    12 }
    13 
    14 void sum(int Matrix[3][3])
    15 {
    16     int s=0;
    17     for(int i=0;i<3;i++)
    18         for(int j=0;j<3;j++)
    19             if(i==j)
    20             s+=Matrix[i][j];
    21     cout<<"The sum is:"<<s<<endl;
    22 }
    23 
    24 void input(int Matrix[3][3])
    25 {
    26     for(int i=0;i<3;i++)
    27         for(int j=0;j<3;j++)
    28             cin>>Matrix[i][j];
    29 }
    30 void main()
    31 {
    32     int Matrix1[3][3],Matrix2[3][3],Matrix3[3][3];
    33     int m;
    34     cout<<"请选择(1/2/3)
     1 输出Matrix1的主对角线乘积及和:
     2 输出Matrix1的主对角线乘积及和:
     3 输出Matrix1的主对角线乘积及和:
     4返回";
    35     cin>>m;
    36     switch (m)
    37     {
    38     case 1:
    39         input(Matrix1);
    40         chengji(Matrix1);
    41         sum(Matrix1);break;
    42     case 2:
    43         input(Matrix2);
    44         chengji(Matrix2);
    45         sum(Matrix2);break;
    46     case 3:
    47         input(Matrix3);
    48         chengji(Matrix3);
    49         sum(Matrix3);break;
    50     default:
    51         break;
    52     }
    53 }
    View Code
     1 //herizai_CD2 题目4
     2 #include<iostream>
     3 #include<fstream>
     4 using namespace std;
     5 
     6 int main()
     7 {
     8     ifstream in("Sfile.txt");
     9     ofstream out("Dfile.txt");
    10     string str;
    11     for(int i=0;i<str.length();i++)
    12         in>>str[i];
    13     int s=str.length();
    14     for(int i=0;i<s-1;i++)
    15         for(int j=i+1;j<s;j++)
    16             if(str[i>str[j]])
    17             {
    18                 int temp=str[i];
    19                 str[i]=str[j];
    20                 str[j]=temp;
    21             }
    22             for(int k=0;k<str.length();k++)
    23                 out<<str[k];
    24 }
    View Code
     1 //herizai_CD2_题目18
     2 #include<iostream>
     3 #include<string>
     4 using namespace std;
     5 char a(char b)
     6 {
     7     if(b>='a' && b<='z')
     8     {
     9         b=b-32;
    10     }   
    11     else if(b>='A' && b<='Z')
    12     {
    13         b=b+32;
    14     }
    15     return b;
    16 }
    17 int main(void)
    18 { 
    19     char s[10];
    20     char c;
    21     int i;
    22     gets(s);
    23     for(i=0;i<strlen(s);i++)
    24     {
    25         c=a(s[i]);
    26         printf("%c",c);
    27     }
    28     
    29     return 0;
    30 }
    View Code
     1 //herizai_CD2_题目17
     2 #include <iostream>
     3 using namespace std;
     4 int max(int a,int b)
     5 {
     6     if(b==0)
     7         return a;
     8     else
     9         return max(b,a%b);
    10 }
    11 void main()
    12 {
    13     int a,b;
    14     cout<<"Input two numbers:"<<endl;
    15     do
    16     {
    17     cin>>a>>b;
    18     if(b<a)
    19         cout<<"最大公约数为:"<<max(a,b)<<endl;
    20     else
    21         cout<<"最大公约数为:%d
    "<<max(b,a)<<endl;
    22     cout<<"Input two numbers or input -999 to end:"<<endl;
    23 }while(b!=(-999)&&a!=(-999));
    24 }
    View Code
     1 //herizai_CD2_题16
     2 #include<iostream>
     3 using namespace std;
     4 int days(int nums); 
     5 void main()
     6 { 
     7 int nums = 1020; 
     8 int day;
     9 day = days(nums);
    10 cout<<"卖完需要天数:"<<day<<endl;
    11 }
    12 int days(int nums)
    13 { 
    14 int day=0;
    15 while(nums>0)
    16 { 
    17 int numsPerDay = nums/2+2 ;
    18 nums=nums-numsPerDay;
    19 day++;
    20 } 
    21 return day;
    22 }
    View Code
     1 //herizai_CD2_题15
     2 #include<iostream>
     3 #include<string>
     4 using namespace std;
     5 int sumsquare(int n){
     6     char s[20],i;
     7     int sum=0;
     8     sprintf(s,"%d",n);
     9     for(i=0;i<strlen(s);i++)//传过来的数使用sprintf()以字符串s体现,就是计算每一位数平方和,
    10       sum += (s[i]-'0')*(s[i]-'0');//sum这个得到的就是这个传过来的数的平方和
    11     return sum;
    12 }
    13 int hasamenum(int n){
    14     char s[20],i,j;
    15     sprintf(s,"%d",n);
    16     for(i=0;i<strlen(s);i++)
    17     for(j=i+1;j<strlen(s);j++)
    18     if(s[i]==s[j])
    19         return 1;
    20     return 0;
    21 }
    22 main()
    23 {
    24     int i;
    25     for(i=100;i<=999;i++)
    26     if(i / 11 == sumsquare(i) && hasamenum(i))
    27     printf("%d
    ",i);
    28 }
    View Code
     1 //herizai_CD2_题目12
     2 #include<iostream>
     3 using namespace std;
     4 int totalday(int year,int month,int day)//计算天数函数
     5 {
     6 int leap(int);//声明//一个函数的声明在另一个非main函数之中
     7 int judge;
     8 judge=leap(year);//调用
     9 int total_day=0;
    10 month-=1;
    11 switch(month)
    12 {
    13 case 11:total_day+=30;
    14 case 10:total_day+=31;
    15 case 9:total_day+=30;
    16 case 8:total_day+=31;
    17 case 7:total_day+=31;
    18 case 6:total_day+=30;
    19 case 5:total_day+=31;
    20 case 4:total_day+=30;
    21 case 3:total_day+=31;
    22 case 2:judge?total_day+=29:total_day+=28;
    23 case 1:total_day+=31;
    24 case 0:total_day+=day;break;
    25 default :break;
    26 }
    27 return total_day;
    28 }
    29 
    30 int leap(int year)//判断闰年函数
    31 {
    32 if(year%4==0&year%100!=0||year%400==0)
    33 return 1;
    34 else
    35 return 0;
    36 }
    37 
    38 int main()
    39 {
    40     int year,month,day;
    41     cout<<"Please input year month day:"<<endl;
    42     cin>>year>>month>>day;
    43     cout<<"totalday is:"<<totalday(year,month,day)<<endl;
    44 }
    View Code
      1 //herizai_CD2_题目10
      2 #include "stdio.h"
      3  #include "time.h"
      4  #include "dos.h"
      5  #include "windows.h"
      6  #include "string.h"
      7  #include "ctype.h"
      8  
      9  int year_r();  //显示年
     10  int month_h();  //
     11  int date_e();  //
     12  int time_e();  //时间
     13  char * time_ta(); //将日期时间转换成字符串
     14  int wait_t(); //延时1秒
     15  
     16  char * time_ta()  //将日期时间转换成字符串
     17  {
     18   char *q;
     19   time_t t;
     20    t=time(NULL);
     21     q=ctime(&t);
     22     //printf("*q_address = 0x%x
    ",q);
     23        return (q);
     24  }
     25  
     26  int wait_t() //延时1秒
     27  {
     28   long  temp_total=0;
     29   time_t time_temp;
     30   time_temp=time(NULL);
     31   temp_total=time_temp;
     32   for(;;)
     33      {
     34     time_temp=time(NULL);
     35     if(abs(time_temp - temp_total) >=1)
     36     break;
     37   }
     38   
     39    return (0);
     40  }
     41  
     42  int main()
     43  {  
     44      int temp;
     45  
     46       system("cls");   //清屏
     47    printf("
    
    
    
    
    
    
    
    			");
     48    year_r();
     49    printf("");
     50    temp = month_h();
     51    if (temp != 13)
     52    {
     53     printf("%d",temp);
     54     printf("");
     55    }
     56    else printf("month error!
    ");
     57    date_e();
     58    printf("");
     59       time_e();
     60    printf("
    ");
     61      for(;;)    //显示年月日时分秒
     62   {    
     63    wait_t(); //  1秒钟到显示年月日时分秒
     64       system("cls");   
     65    printf("
    
    
    
    
    
    
    
    			");
     66    year_r();
     67    printf("");
     68    temp = month_h();
     69    if (temp != 13)
     70    {
     71     printf("%d",temp);
     72     printf("");
     73    }
     74    else printf("month error!
    ");
     75    
     76    date_e();
     77    printf("");
     78       time_e();
     79    }
     80       getchar();  
     81  
     82  }
     83  
     84  int  year_r() //显示年
     85  {   
     86   
     87   char *p;
     88     int i; 
     89      p=time_ta();
     90       for(i=0;i<24;i++,p++) //ctime函数返回字符为24个
     91      if(i>19&&i<24)
     92       printf("%c",*p); 
     93     return (0);
     94  }
     95  
     96  int month_h() //显示月
     97  {   
     98   char month_n[12][5]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};    
     99   char *p;
    100      char month[4];   //存放三个字符
    101   int i,j=0;
    102      p=time_ta();
    103   for(i=0;i<24;i++,p++)  //i<24因为ctime()函数返回有24个字符
    104   {
    105    if(i>=4 && i<7)   //取ctime函数返回值的第5--8共三个字符.
    106    {
    107      month[j++]=*p;
    108      if(j==3)
    109      {
    110       month[j]='';
    111       break;
    112  
    113      }
    114    }
    115   }
    116   for (i=0;i<12;i++)
    117   {
    118    if (strcmp(month_n[i],month) == 0)
    119    {
    120     return (i+1);
    121     
    122    }
    123  
    124   }
    125  
    126   return (13);
    127  
    128  }
    129  
    130  int date_e()  //
    131  {   
    132   int j=0,i=0;
    133   char date[2];
    134   char *p;
    135   p=time_ta();
    136   for(i=0;i<24;i++,p++)  
    137        if(i>=8&&i<10)
    138     {  date[j]=*p;
    139     printf("%c",date[j++]);}
    140     return 0;
    141  }
    142  
    143  int time_e() //时间
    144  {    int i;
    145   char *p;
    146      p=time_ta();
    147      for(i=0;i<24;i++,p++)
    148         if(i>10&&i<19)
    149     printf("%c",*p); 
    150      printf("
    ");
    151      return (0);
    152  }
    View Code
     1 //herizai_CD2_题目9
     2 #include<iostream>
     3 #include<cstdlib>
     4 using namespace std;
     5 int main()
     6 {
     7     int sum1=1,sum2=1,n=0;
     8     int month;
     9     cin>>month;
    10     if(month==1||month==2)
    11     {
    12         cout<<1;
    13         system("pause");
    14         return 0;
    15     }
    16     if(month%2==1)
    17     {
    18         month=month+1;
    19         n++;
    20     }
    21     for(int i=1;i<month/2;i++)
    22     {
    23         sum1=sum1+sum2;
    24         sum2=sum1+sum2;
    25     }
    26     if(n==0)
    27         cout<<sum2;
    28     if(n==1)
    29         cout<<sum1;
    30     system("pause");
    31     return 0;
    32 }
    View Code
    函数功能:把格式化的数据写入某个字符串
    函数原型:int sprintf( char *buffer, const char *format [, argument] … );
    返回值:字符串长度(strlen)

    例子:
    char* who = "I";
    char* whom = "CSDN";
    sprintf(s, "%s love %s.", who, whom); //产生:"I love CSDN. " 这字符串写到s中

    sprintf(s, "%10.3f", 3.1415626); //产生:" 3.142"

  • 相关阅读:
    [LeetCode] 174. Dungeon Game 地牢游戏
    [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
    [LeetCode] Excel Sheet Column Title 求Excel表列名称
    [LeetCode] Excel Sheet Column Number 求Excel表列序号
    [LeetCode] 179. Largest Number 最大组合数
    Camera Calibration and 3D Reconstruction
    [LeetCode] 172. Factorial Trailing Zeroes 求阶乘末尾零的个数
    [LeetCode] Find Peak Element 求数组的局部峰值
    Medical Image Processing Conference and Journal 医学图像处理会议与期刊
    [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历
  • 原文地址:https://www.cnblogs.com/herizai/p/3155936.html
Copyright © 2011-2022 走看看