浙大PAT 1049
来源:互联网 发布:海航进出口公司 知乎 编辑:程序博客网 时间:2024/05/16 10:26
//此题在编程之美上出现,微软的一道面试题目,如果用暴力枚举方法,时间太慢,应该使用数学归纳,总结出1出现的数学规律
#include<iostream>
#include<fstream>#include<algorithm>
#include<vector>
#include<queue>
#include<math.h>
#include<string>
using namespace std;
int N;
int length=0;
int *data;
void len(int n)
{
while(n)
{
length++;
n=n/10;
}
}
void DATA(int n)
{
int i=0;
while(n)
{
data[i++]=n%10;
n=n/10;
}
}
int main()
{
/*ifstream fin("d:\in.txt" );
if (! fin.is_open())
{
cout << "Error opening file";
system("pause");
exit (1);
}*/
cin>>N;
len(N);
data=new int[length];
DATA(N);
int sum=0;
for(int i=1;i<=length;i++)
{
int left=0,middle=0,right=0;
for(int j=1;j<i;j++)
{
right=right+data[j-1]*pow(10.0,j-1);
}
middle=data[i-1];
for(int j=i+1;j<=length;j++)
{
left=left+data[j-1]*pow(10.0,j-i-1);
}
//cout<<left<<" "<<middle<<" "<<right<<endl;
if(middle==0)
{
sum=sum+left*pow(10.0,i-1);
}
else if(middle==1)
{
sum=sum+left*pow(10.0,i-1)+right+1;
}
else
{
sum=sum+(left+1)*pow(10.0,i-1);
}
}
cout<<sum;
system("PAUSE");
return 0;
}
0 0
- 浙大PAT 1049
- *浙大PAT甲级 1049
- 浙大pat 1017
- 浙大pat Advance 1003
- 浙大PAT目录
- 浙大PAT 1051
- 浙大PAT 1058
- 浙大PAT 1063
- 浙大PAT 1102
- 浙大PAT 1099
- 浙大PAT 1033
- 浙大pat甲级 1023
- 浙大pat甲级 1024
- 浙大pat甲级 1025
- 浙大PAT甲级 1026
- 浙大PAT甲级 1028
- 浙大PAT甲级 1029
- 浙大PAT甲级 1030
- 【智能路由器】C代码调用uci的API读openwrt配置文件指南
- hdoj1028 Ignatius and the Princess III(整数分解)
- Java开发中的23种设计模式详解(转)
- java 小知识点HashMap
- web小游戏 2048制作(四):过场动画 showanimation2048.js
- 浙大PAT 1049
- zoj 3471 Most Powerful 状压dp(简单)
- 安卓图片框架:universal-image-loader的快速使用
- leetcode79 word serach 解题报告
- C语言学习笔记6-数组
- Eclipse 技巧
- 文件操作
- 【JAVA】21、方法的重载
- 半年收获的点点滴滴