[关闭]
@rg070836rg 2017-06-17T15:03:56.000000Z 字数 3421 阅读 1067

0617编程

未分类


  1. // 01
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <iostream>
  5. using namespace std;
  6. int main(){
  7. int n;
  8. cin>>n;
  9. long long sum=0;
  10. for(int i=1;i<=n;i++)
  11. {
  12. sum+=i*i;
  13. }
  14. cout<<sum<<endl;
  15. return 0;
  16. }
  1. // 02
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <iostream>
  6. using namespace std;
  7. bool judge(int x)
  8. {
  9. //判断整除
  10. if(x%4==0||x%7==0)
  11. return true;
  12. //int转string
  13. char c[10];
  14. snprintf(c, sizeof(c), "%d", x);
  15. if (strstr(c,"44")!=NULL ||strstr(c,"77")!=NULL )
  16. return true;
  17. return false;
  18. }
  19. int main(){
  20. int n;
  21. cin>>n;
  22. int sum = 0;
  23. for(int i=1;i<=n;i++)
  24. if(judge(i))
  25. sum++;
  26. cout<<sum<<endl;
  27. return 0;
  28. }
  1. // 03
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <iostream>
  5. using namespace std;
  6. int main(){
  7. int n,k,number;
  8. cin>>n>>k>>number;
  9. int a[n],b[n];
  10. for(int i=0;i<n;i++)
  11. a[i]=i+1;
  12. for(int i=0;i<k;i++)
  13. {
  14. int num = 0;
  15. for(int j=0;j<n/2;j++)
  16. {
  17. b[num++] = a[j];
  18. b[num++] = a[j+n/2];
  19. }
  20. for(int m=0;m<n;m++)
  21. a[m]=b[m];
  22. }
  23. cout<<a[number-1]<<endl;
  24. return 0;
  25. }
  1. // 04-1
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <iostream>
  5. using namespace std;
  6. int main(){
  7. int n,s;
  8. cin>>n>>s;
  9. int a[26],b[26];
  10. for(int i = 0;i<26;i++)
  11. a[i]=b[i]=0;
  12. for(int i = 0;i<n;i++)
  13. {
  14. s=(s*345) %19997;
  15. a[s %26] +=1;
  16. }
  17. for(int i = 0;i<n;i++)
  18. {
  19. s=(s*345) %19997;
  20. b[s %26] +=1;
  21. }
  22. for(int i = 0;i<26;i++)
  23. {
  24. if(a[i]>0)
  25. {
  26. if(a[i]>b[i])
  27. {
  28. a[i]-=b[i];
  29. b[i]-=b[i];
  30. }
  31. else
  32. {
  33. a[i]-=a[i];
  34. b[i]-=a[i];
  35. }
  36. }
  37. }
  38. int sum =0;
  39. for(int i=0;i<26;i++)
  40. sum+=a[i];
  41. cout<<sum<<endl;
  42. return 0;
  43. }
  1. //04-2
  2. #include<iostream>
  3. using namespace std;
  4. int main()
  5. {
  6. int n,s;
  7. cin>>n>>s;
  8. char *a=new char[n];
  9. char *b=new char[n];
  10. for(int i = 0;i<n;i++)
  11. {
  12. s=(s*345) %19997;
  13. a[i]=char(97 + (s %26));
  14. }
  15. for(int i = 0;i<n;i++)
  16. {
  17. s=(s*345) %19997;
  18. b[i]=char(97 + (s %26));
  19. }
  20. int per = n;
  21. cout<<a<<endl;
  22. cout<<b<<endl;
  23. for(int j=0;j<n;j++){
  24. for(int i=0;i<n;i++){
  25. if(a[j] == b[i]){
  26. b[j] = '#';
  27. per--;
  28. break;
  29. }
  30. }
  31. }
  32. /*
  33. for(int j=0;j<n;j++){
  34. for(int i=0;i<n;i++){
  35. if(a[i] == b[j]){
  36. a[i] = '#';
  37. per--;
  38. break;
  39. }
  40. }
  41. }
  42. */
  43. cout<<a<<endl;
  44. cout<<b<<endl;
  45. cout<<per<<endl;
  46. return 0;
  47. }
  1. //加法
  2. #include<iostream>
  3. #include<string>
  4. #include<stack>
  5. using namespace std;
  6. int main()
  7. {
  8. string x,y;
  9. cin>>x>>y;
  10. int xlen=x.length();
  11. int ylen=y.length();
  12. stack<int> answer;
  13. int c=0;
  14. while(xlen>0 || ylen>0)
  15. {
  16. int a = xlen>0 ? x[xlen-- -1]-'0' : 0;
  17. int b = ylen>0 ? y[ylen-- -1]-'0' : 0;
  18. int tmp = a+b+c;
  19. answer.push(tmp%10);
  20. c = tmp/10;
  21. }
  22. if(c!=0)
  23. cout<<c;
  24. while(!answer.empty())
  25. {
  26. cout<<answer.top();
  27. answer.pop();
  28. }
  29. return 0;
  30. }
  1. //减法
  2. #include<iostream>
  3. #include<string>
  4. #include<stack>
  5. using namespace std;
  6. int main()
  7. {
  8. string x,y;
  9. cin>>x>>y;
  10. int xlen=x.length();
  11. int ylen=y.length();
  12. bool flag = true;
  13. if(xlen<ylen || (xlen==ylen && x<y))
  14. {
  15. flag=false;
  16. swap(x,y);
  17. swap(xlen,ylen);
  18. }
  19. stack<int> answer;
  20. int c=0;
  21. while(xlen>0 || ylen>0 )
  22. {
  23. int a = xlen>0 ? x[xlen-- -1]-'0' : 0;
  24. int b = ylen>0 ? y[ylen-- -1]-'0' : 0;
  25. int tmp = (a-b+c+10)%10;
  26. c=a-b+c>=0?0:-1;
  27. answer.push(tmp);
  28. }
  29. if(!flag)
  30. cout<<"-";
  31. bool isout = false;
  32. while(!answer.empty())
  33. {
  34. int x = answer.top();
  35. answer.pop();
  36. if(x==0 && !isout)
  37. continue;
  38. isout = true;
  39. cout<<x;
  40. }
  41. return 0;
  42. }
  1. //乘法
  2. #include<iostream>
  3. #include<string>
  4. #include<stack>
  5. using namespace std;
  6. string add(string x,string y)
  7. {
  8. int xlen=x.length();
  9. int ylen=y.length();
  10. stack<int> answer;
  11. int c=0;
  12. while(xlen>0 || ylen>0)
  13. {
  14. int a = xlen>0 ? x[xlen-- -1]-'0' : 0;
  15. int b = ylen>0 ? y[ylen-- -1]-'0' : 0;
  16. int tmp = a+b+c;
  17. answer.push(tmp%10);
  18. c = tmp/10;
  19. }
  20. string res="";
  21. if(c!=0)
  22. res+=char('0'+c);
  23. while(!answer.empty())
  24. {
  25. res+=char('0'+answer.top());
  26. answer.pop();
  27. }
  28. return res;
  29. }
  30. string mul(string x,char y)
  31. {
  32. string res;
  33. int b = y-'0';
  34. int c = 0;
  35. for(int i=x.length()-1;i>=0;i--)
  36. {
  37. int a = x[i]-'0';
  38. int tmp = a * b + c;
  39. res = char(tmp%10+'0') +res;
  40. c = tmp/10;
  41. }
  42. if(c!=0)
  43. res = char(c+'0') +res;
  44. return res;
  45. }
  46. int main(){
  47. string x,y;
  48. cin>>x>>y;
  49. int xlen=x.length();
  50. int ylen=y.length();
  51. string res="0";
  52. for(int i=y.length()-1;i>=0;i--)
  53. {
  54. string tmp = mul(x,y[i]);
  55. for(int j=0;j<ylen-i-1;j++)
  56. {
  57. tmp+="0";
  58. }
  59. res = add(res,tmp);
  60. }
  61. cout<<res<<endl;
  62. return 0;
  63. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注